Created
May 18, 2015 17:53
-
-
Save tweekmonster/86f043a07d68eaa062de to your computer and use it in GitHub Desktop.
A script to truncate the_silver_searcher's output in Vim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
'''Truncates matches from ag | |
Place this script somewhere in your $PATH, like ~/bin and pipe ag into it. | |
Vim could be setup like this: | |
if executable('ag') | |
set grepprg=ag\ --vimgrep\ -w\ $* | |
if executable('agtrunc') | |
set grepprg+=\ \\\|\ agtrunc | |
endif | |
set grepformat=%f:%l:%c:%m | |
command! -nargs=+ Ag exec 'silent! grep! <args>' | copen | exec 'silent /<args>' | redraw! | |
nnoremap <leader>a :Ag <c-r>=expand('<cword>')<CR><CR> | |
endif | |
''' | |
import sys | |
MAX_CHARS = 80 | |
def truncline(line): | |
filename, lineno, col, text = line.split(':', 3) | |
if len(text) > MAX_CHARS: | |
cs = max(0, int(col) - 10) | |
ce = cs + MAX_CHARS | |
text = text[cs:ce] | |
print(':'.join((filename, lineno, col, text.strip('\r\n')))) | |
if __name__ == '__main__': | |
for line in sys.stdin.readlines(): | |
truncline(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment