Skip to content

Commit 484435b

Browse files
committed
display search results statistics in the command line
1 parent 0e0785a commit 484435b

File tree

5 files changed

+138
-25
lines changed

5 files changed

+138
-25
lines changed

autoload/ags.vim

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ function! s:show(lines, ...)
7272

7373
call ags#buf#openViewResultsBuffer()
7474
call s:execw(obj)
75+
call s:printStats(0, 0, 1)
7576
endfunction
7677

7778
" Prepares the search {data} for display
@@ -101,6 +102,35 @@ function! s:processSearchData(data)
101102
return results
102103
endfunction
103104

105+
" Gathers statistics about the search results from {lines}
106+
"
107+
function! s:gatherStatistics(lines)
108+
if len(a:lines) > g:ags_stats_max_ln | return {} | endif
109+
110+
let stats = {}
111+
let resultPat = s:pat('^:lineStart:\s\{}\d\{1,}\s\{}:lineColEnd:')
112+
let filePat = s:pat('^:file:$')
113+
let resultCount = 0
114+
let fileCount = 0
115+
let index = 0
116+
let llines = len(a:lines)
117+
118+
while index < llines
119+
let line = a:lines[index]
120+
121+
if line =~ filePat
122+
let fileCount = fileCount + 1
123+
elseif line =~ resultPat
124+
let resultCount = resultCount + 1
125+
endif
126+
127+
let index = index + 1
128+
let stats[index] = { 'file': fileCount, 'result': resultCount }
129+
endw
130+
131+
return { 'data': stats, 'files': fileCount, 'results': resultCount }
132+
endfunction
133+
104134
" Returns the cursor position when opening a file
105135
" from the {lineNo} in the search results window
106136
"
@@ -125,6 +155,41 @@ function! s:resultPosition(lineNo)
125155
return [0, row, col, 0]
126156
endfunction
127157

158+
" Prints search results statistics
159+
"
160+
" {r} - print result info
161+
" {f} - print file info
162+
" {t} - print totals info
163+
function! s:printStats(r, f, t)
164+
if empty(s:stats) | return | endif
165+
166+
let result = s:stats.data[line('.')].result
167+
let file = s:stats.data[line('.')].file
168+
let resultMsg = 'Result ' . result . '/' . s:stats.results . ' '
169+
let fileMsg = 'File ' . file . '/' . s:stats.files
170+
171+
if a:r
172+
echohl None
173+
redraw | echon resultMsg
174+
endif
175+
176+
if !a:r
177+
redraw
178+
endif
179+
180+
if a:f
181+
echohl MoreMsg
182+
echon fileMsg
183+
endif
184+
185+
if a:t
186+
echohl Underlined
187+
redraw | echom s:stats.results . ' results found in ' . s:stats.files . ' files'
188+
endif
189+
190+
echohl None
191+
endfunction
192+
128193
" Performs a search with the specified {args} and according to {cmd}.
129194
"
130195
" {cmd|add} the new results will be added to previous results in the search window
@@ -144,7 +209,8 @@ function! ags#search(args, cmd)
144209
let data = ags#run#ag(args)
145210
endif
146211

147-
let lines = s:processSearchData(data)
212+
let lines = s:processSearchData(data)
213+
let s:stats = s:gatherStatistics(lines)
148214
if empty(lines)
149215
call ags#log#warn("No matches for " . string(a:args))
150216
elseif len(lines) == 1
@@ -291,6 +357,7 @@ function! ags#navigateResults(...)
291357
call setpos('.', pos)
292358

293359
let s:hlpos = pos
360+
call s:printStats(1, 1, 0)
294361
endfunction
295362

296363
" Navigates the search results file paths
@@ -302,6 +369,7 @@ function! ags#navigateResultsFiles(...)
302369
let file = s:pat(':file:')
303370
call search(file, flags)
304371
exec 'normal zt'
372+
call s:printStats(0, 1, 0)
305373
endfunction
306374

307375
function! ags#quit()

autoload/ags/log.vim

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,58 @@ function! s:log(message, hl)
66
echohl None
77
endfunction
88

9+
" Writes a {message} highlighted with {hl}
10+
"
11+
function! s:write(message, hl)
12+
exec 'echohl ' . a:hl
13+
redraw | echo a:message
14+
echohl None
15+
endfunction
16+
917
" Logs an error with {message}
1018
"
1119
function! ags#log#error(message)
1220
call s:log(a:message, 'Error')
1321
endfunction
1422

15-
" Log an info {message}
23+
" Logs an info {message}
1624
"
1725
function! ags#log#info(message)
1826
call s:log(a:message, 'MoreMsg')
1927
endfunction
2028

21-
" Log a warning {message}
29+
" Logs a warning {message}
30+
"
2231
function! ags#log#warn(message)
2332
call s:log(a:message, 'WarningMsg')
2433
endfunction
34+
35+
" Logs a plain {message}
36+
"
37+
function! ags#log#plain(message)
38+
call s:log(a:message, 'None')
39+
endfunction
40+
41+
" Writes an error with {message}
42+
"
43+
function! ags#log#errorw(message)
44+
call s:write(a:message, 'Error')
45+
endfunction
46+
47+
" Writes an info {message}
48+
"
49+
function! ags#log#infow(message)
50+
call s:write(a:message, 'MoreMsg')
51+
endfunction
52+
53+
" Writes a warning {message}
54+
"
55+
function! ags#log#warnw(message)
56+
call s:write(a:message, 'WarningMsg')
57+
endfunction
58+
59+
" Writes a plain {message}
60+
"
61+
function! ags#log#plainw(message)
62+
call s:write(a:message, 'None')
63+
endfunction

autoload/ags/run.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function! s:cmd(args)
3030

3131
if value =~ '^g:'
3232
let value = substitute(value, '^g:', '', '')
33-
let value = has_key(g:, value) ? g:[value] : arg[2]
33+
let value = get(g:, value, 0)
3434
endif
3535

3636
let op = strlen(value) == 0 ? '' : '='

doc/ags.txt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ OPTIONS *ags-options*
5050
|ags_agexe|......................The ag executable path
5151
|ags_agmaxcount|.................The --max-count ag param
5252
|ags_agcontext|..................The --context ag param
53+
|ags_stats_max_ln|...............The maximum number of lines
54+
before disabling statistics
5355
|ags_agargs|.....................The predefined ag args
5456
|ags_edit_show_line_numbers|.....Show line numbers in edit mode
5557
|ags_edit_skip_if_file_changed|..In edit mode skip write of any
@@ -58,30 +60,39 @@ OPTIONS *ags-options*
5860
-------------------------------------------------------------
5961
Detailed options:~
6062

61-
*ags_agexe*
63+
*ags_agexe*
6264
This is the ag executable path >
6365
let g:ags_agexe = 'ag'
6466
<
6567

66-
*ags_agmaxcount*
68+
*ags_agmaxcount*
6769
This is the max-count parameter to be passed to ag >
6870
let g:ags_agmaxcount = 2000
6971
<
7072

71-
*ags_agcontext*
73+
*ags_agcontext*
7274
This is the context parameter to be passed to ag >
7375
let g:ags_agcontext = 3
7476
<
7577

76-
*ags_edit_show_line_numbers*
78+
*ags_stats_max_ln*
79+
This is the maximum number of lines before disabling the
80+
statistics info that shows when navigating results in the
81+
search results window. When the number of lines returned by
82+
ag is larger than the value of this variable the statistics
83+
are disabled to improve performance.
84+
let g:ags_stats_max_ln = 5000
85+
<
86+
87+
*ags_edit_show_line_numbers*
7788
This enables showing file line numbers in edit mode as visual
7889
cues. The file numbers as well as file paths in edit mode are
7990
not meant to be edited and could result in unwanted changes if
80-
removed
91+
removed.
8192
let g:ags_edit_show_line_numbers = 0
8293
<
8394

84-
*ags_edit_skip_if_file_changed*
95+
*ags_edit_skip_if_file_changed*
8596
When writing files from edit mode this allows to skip writing
8697
lines that have changed from outside vim. This could have
8798
mixed results as some lines will be changed and others not.
@@ -90,7 +101,7 @@ file lines exactly as they appear in the file.
90101
let g:ags_edit_skip_if_file_changed = 0
91102
<
92103

93-
*ags_agargs*
104+
*ags_agargs*
94105
This variable contains the predefined search arguments
95106
used to ensure that the search results are highlighted
96107
properly in Vim. Any of these arguments can be overwritten

plugin/ags.vim

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,28 @@ if exists('g:ags_loaded') || &cp || v:version < 700 | finish | endif
22

33
let g:ags_loaded = 1
44

5-
if !exists('g:ags_agexe') | let g:ags_agexe = 'ag' | endif
6-
if !exists('g:ags_agmaxcount') | let g:ags_agmaxcount = 2000 | endif
7-
if !exists('g:ags_agcontext') | let g:ags_agcontext = 3 | endif
8-
9-
if !exists('g:ags_edit_skip_if_file_changed')
10-
let g:ags_edit_skip_if_file_changed = 0
11-
endif
12-
13-
if !exists('g:ags_edit_show_line_numbers')
14-
let g:ags_edit_show_line_numbers = 0
15-
endif
5+
let g:ags_agexe = get(g:, 'ags_agexe', 'ag')
6+
let g:ags_agmaxcount = get(g:, 'ags_agmaxcount', 2000)
7+
let g:ags_agcontext = get(g:, 'ags_agcontext', 3)
8+
let g:ags_stats_max_ln = get(g:, 'ags_stats_max_ln', 5000)
9+
let g:ags_edit_skip_if_file_changed = get(g:, 'ags_edit_skip_if_file_changed', 0)
10+
let g:ags_edit_show_line_numbers = get(g:, 'ags_edit_show_line_numbers', 0)
1611

1712
if !exists('g:ags_agargs')
1813
" Predefined search arguments
19-
" arg : [ value, short-name, default ]
14+
" arg : [ value, short-name ]
2015
let g:ags_agargs = {
2116
\ '--break' : [ '', '' ],
2217
\ '--color' : [ '', '' ],
2318
\ '--color-line-number' : [ '"1;30"', '' ],
2419
\ '--color-match' : [ '"32;40"', '' ],
2520
\ '--color-path' : [ '"1;31"', '' ],
2621
\ '--column' : [ '', '' ],
27-
\ '--context' : [ 'g:ags_agcontext', '-C', '3' ],
22+
\ '--context' : [ 'g:ags_agcontext', '-C' ],
2823
\ '--filename' : [ '', '' ],
2924
\ '--group' : [ '', '' ],
3025
\ '--heading' : [ '', '-H' ],
31-
\ '--max-count' : [ 'g:ags_agmaxcount', '-m', '2000' ],
26+
\ '--max-count' : [ 'g:ags_agmaxcount', '-m' ],
3227
\ '--numbers' : [ '', '' ]
3328
\ }
3429
endif

0 commit comments

Comments
 (0)