-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathquery.v
More file actions
835 lines (777 loc) · 23.1 KB
/
query.v
File metadata and controls
835 lines (777 loc) · 23.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license
// that can be found in the LICENSE file.
module main
import os
import gg
import time
/*
const txt_cfg = gx.TextCfg{
size: 18
}
*/
enum QueryType {
ctrlp = 0
search = 1
cam = 2
open = 3
ctrlj = 4
task = 5
grep = 6
open_workspace = 7
run = 8
alert = 9 // e.g. "running git pull..."
search_in_folder = 10
}
fn (mut ved Ved) key_query(key gg.KeyCode, super bool) {
match key {
.backspace {
ved.gg_pos = -1
ved.just_switched = true
if ved.query_type != .search && ved.query_type != .grep {
if ved.query.len == 0 {
return
}
ved.query = ved.query[..ved.query.len - 1]
// Re-filter ctrlp results on backspace to update the list immediately
if ved.query_type == .ctrlp {
ved.filter_ctrlp_results()
}
} else {
if ved.search_query.len == 0 {
return
}
ved.search_query = ved.search_query[..ved.search_query.len - 1]
}
return
}
.enter {
match ved.query_type {
.ctrlp {
ved.ctrlp_open()
}
.ctrlj {
ved.ctrlj_open()
}
.cam {
ved.git_commit()
}
.open {
ved.view.open_file(ved.query, 0)
}
.task {
ved.insert_task() or {}
if !ved.timer.pom_is_started && !ved.cur_task.starts_with('@') {
// Start pomodoro with a new task if it's not already running
ved.timer.pom_start = time.now().local_unix()
ved.timer.pom_is_started = true
}
ved.cur_task = ved.query
ved.task_start_unix = time.now().local_unix()
ved.save_timer()
}
.run {
ved.run_zsh()
}
.grep {
// Key down was pressed after typing, now pressing enter opens the file
if ved.gg_pos > -1 && ved.gg_lines.len > 0 && ved.gg_pos < ved.gg_lines.len {
line := ved.gg_lines[ved.gg_pos]
path := line.all_before(':')
pos := line.index(':') or { 0 }
pos2 := line.index_after(':', pos + 1) or { -1 }
// line_nr := line[path.len + 1..].int() - 1
line_nr := line[pos + 1..pos2].int() - 1
ved.view.open_file(ved.workspace + '/' + path, line_nr)
// ved.view.move_to_line(line_nr)
ved.view.zz()
ved.mode = .normal
} else {
// Otherwise just do a git grep on a submitted query
ved.git_grep()
}
return
}
else {
// println('CALLING SEARCH ON ENTER squery=$ved.search_query')
ved.search(.forward)
}
}
ved.mode = .normal
return
}
.escape {
ved.mode = .normal
return
}
.down {
if ved.mode == .query {
match ved.query_type {
.grep {
// Going thru git grep results
ved.gg_pos++
if ved.gg_pos >= ved.gg_lines.len {
ved.gg_pos = ved.gg_lines.len - 1
}
// Scroll down if selection goes below visible area
if ved.gg_pos >= ved.gg_scroll + max_grep_lines {
ved.gg_scroll = ved.gg_pos - max_grep_lines + 1
}
}
.ctrlp {
ved.gg_pos++
// Use ctrlp_results length for boundary check
if ved.gg_pos >= ved.ctrlp_results.len {
ved.gg_pos = ved.ctrlp_results.len - 1
}
if ved.gg_pos < 0 && ved.ctrlp_results.len > 0 { // Handle empty case
ved.gg_pos = 0
}
// Scroll down if selection goes below visible area
if ved.gg_pos >= ved.gg_scroll + nr_ctrlp_results {
ved.gg_scroll = ved.gg_pos - nr_ctrlp_results + 1
}
}
.search {
if ved.search_history.len > 0 {
// History search
ved.search_idx++
if ved.search_idx >= ved.search_history.len {
ved.search_idx = ved.search_history.len - 1
}
ved.search_query = ved.search_history[ved.search_idx]
}
}
else {}
}
}
}
.up {
if ved.mode == .query {
match ved.query_type {
.grep, .ctrlp { // Apply same logic to ctrlp
ved.gg_pos--
if ved.gg_pos < 0 {
ved.gg_pos = 0
}
// Scroll up if selection goes above visible area
if ved.gg_pos < ved.gg_scroll {
ved.gg_scroll = ved.gg_pos
}
}
.search {
if ved.search_history.len > 0 {
ved.search_idx--
if ved.search_idx < 0 {
ved.search_idx = 0
}
ved.search_query = ved.search_history[ved.search_idx]
}
}
else {}
}
}
}
.tab {
// TODO COPY PASTA - adapt for ctrlp if needed
if ved.mode == .query {
match ved.query_type {
.grep {
ved.gg_pos++
if ved.gg_pos >= ved.gg_lines.len {
ved.gg_pos = 0 // wrap around? or stop?
}
}
.ctrlp {
ved.gg_pos++
if ved.gg_pos >= ved.ctrlp_results.len {
ved.gg_pos = 0 // wrap around? or stop?
}
}
else {}
}
}
ved.just_switched = true
}
.v {
if super {
clip := ved.cb.paste()
ved.query += clip
// Re-filter ctrlp results after paste
if ved.query_type == .ctrlp {
ved.filter_ctrlp_results()
}
}
}
else {}
}
}
fn (mut ved Ved) char_query(s string) {
if int(s[0]) < 32 {
return
}
// println('char q(${s}) ${ved.query_type}')
if ved.query_type in [.search, .search_in_folder, .grep] {
ved.search_query += s
println('new sq=${ved.search_query}')
} else if ved.query_type == .ctrlp {
ved.query += s
ved.filter_ctrlp_results() // Filter results as user types
} else {
ved.query += s
}
}
// Loads files for the *current* workspace into `all_git_files`.
fn (mut ved Ved) load_git_tree() {
ved.query = '' // Reset query when loading tree
ved.ctrlp_results = [] // Reset ctrlp results as well
ved.gg_pos = -1
ved.gg_scroll = 0 // Reset scroll offset
mut dir := ved.workspace
if dir == '' {
dir = '.' // Should not happen if workspace is managed correctly
}
if ved.is_git_tree() {
// Cache all git files for the current workspace
s := os.execute('git -C ${dir} ls-files')
if s.exit_code == -1 {
ved.all_git_files = []
return
}
ved.all_git_files = s.output.split_into_lines()
} else {
/*
// Get all files if not a git repo
mut files := []string{}
os.walk_with_context(dir, &files, fn (mut fs []string, f string) {
if f == '.' || f == '..' {
return
}
full_path := os.join_path(dir, f) // Need full path for is_file check
if os.is_file(full_path) {
// Store relative path
fs << f.replace(dir + os.path_separator, '')
}
})
ved.all_git_files = files
*/
}
ved.all_git_files.sort_by_len()
// Also filter results initially when Ctrl+P is pressed
if ved.query_type == .ctrlp {
ved.filter_ctrlp_results()
}
}
// Filters files for Ctrl+P based on the current query.
// Searches current workspace first, then others if no results found.
fn (mut ved Ved) filter_ctrlp_results() {
ved.ctrlp_results = [] // Clear previous results
ved.gg_pos = -1 // Reset selection
ved.gg_scroll = 0 // Reset scroll offset
query_lower := ved.query.to_lower()
// 1. Search current workspace
current_ws_path := ved.workspace
for file_ in ved.all_git_files { // all_git_files should hold current workspace files
file_path := file_.trim_space()
if file_path.to_lower().contains(query_lower) {
ved.ctrlp_results << CtrlPResult{
file_path: file_path
workspace_path: current_ws_path
display_name: file_path
}
}
}
// 2. If no results in current workspace and query is not empty, search others
if ved.ctrlp_results.len == 0 && query_lower != '' {
for ws_path in ved.workspaces {
if ws_path == current_ws_path {
continue // Skip current workspace, already searched
}
// Get files for this other workspace (might be slow if not cached)
other_files := ved.get_files_for_workspace(ws_path)
short_ws_name := short_space(ws_path)
for file_path in other_files {
file_path_trimmed := file_path.trim_space()
if file_path_trimmed.to_lower().contains(query_lower) {
ved.ctrlp_results << CtrlPResult{
file_path: file_path_trimmed
workspace_path: ws_path
display_name: '${file_path_trimmed} (${short_ws_name})'
}
}
}
}
}
// Sort results by open count (most opened first)
ved.ctrlp_results.sort_with_compare(fn [ved] (a &CtrlPResult, b &CtrlPResult) int {
// Build full paths for lookup
path_a := os.join_path(a.workspace_path, a.file_path)
path_b := os.join_path(b.workspace_path, b.file_path)
count_a := ved.file_open_count[path_a]
count_b := ved.file_open_count[path_b]
// Sort descending by count (higher count first)
if count_a > count_b {
return -1
} else if count_a < count_b {
return 1
}
// If same count, sort by path length (shorter first)
if a.file_path.len < b.file_path.len {
return -1
} else if a.file_path.len > b.file_path.len {
return 1
}
return 0
})
ved.gg_file_locs = map[string]int{}
}
fn (mut ved Ved) is_git_tree() bool {
path := if ved.workspace == '' { '.' } else { ved.workspace }
out := os.execute('git -C "${path}" rev-parse --is-inside-work-tree')
if out.exit_code != -1 {
return out.output.trim_space() == 'true' // Ensure comparison is robust
}
return false
}
fn (q QueryType) str() string {
return match q {
.search { 'find' }
.search_in_folder { 'find in folder' }
.ctrlp { 'ctrl p (files)' } // Updated title
.open { 'open' }
.open_workspace { 'open workspace' }
.cam { 'git commit -am' }
.ctrlj { 'ctrl j (opened files)' }
.task { 'new task/activity' }
.grep { 'git grep' }
.run { 'run a zsh command' }
.alert { '' }
}
}
const small_queries = [QueryType.search, .cam, .open, .run, .alert] //.grep
const max_grep_lines = 20
const query_width = 700
const nr_ctrlp_results = 20 // Max results to show for Ctrl+P
const line_padding = 5
// Search, commit, open, ctrl p
fn (mut ved Ved) draw_query() {
// println('DRAW Q type=$ved.query_type')
mut width := query_width
mut height := 360 // Default height
// Determine fixed height based on query type
if ved.query_type in small_queries {
height = 70
} else if ved.query_type == .grep {
width *= 2 // Keep grep wide
// Use fixed height based on max_grep_lines
height = (max_grep_lines + 2) * (ved.cfg.line_height + line_padding) + 15
} else if ved.query_type in [.ctrlp, .ctrlj] {
// Use fixed height based on nr_ctrlp_results
height = (nr_ctrlp_results + 2) * (ved.cfg.line_height + line_padding) + 15
}
// Ensure minimum height
if height < 70 {
height = 70
}
x := (ved.win_width - width) / 2
y := (ved.win_height - height) / 2
ved.gg.draw_rect_filled(x, y, width, height, gg.white)
// query window title
ved.gg.draw_rect_filled(x, y, width, ved.cfg.line_height, ved.cfg.title_color)
ved.gg.draw_text(x + 10, y, ved.query_type.str(), ved.cfg.file_name_cfg)
// Draw counter for ctrlp and grep (e.g., "1/25")
if ved.query_type == .ctrlp || ved.query_type == .grep {
mut total := 0
mut current := 0
if ved.query_type == .ctrlp {
total = ved.ctrlp_results.len
current = ved.gg_pos + 1
} else if ved.query_type == .grep {
total = ved.gg_lines.len
current = ved.gg_pos + 1
}
// Only show if we have results
if total > 0 && current > 0 {
counter_text := '${current}/${total}'
counter_x := x + width - 10 - counter_text.len * ved.cfg.char_width
ved.gg.draw_text2(
x: counter_x
y: y
text: counter_text
color: gg.white
)
}
}
query_to_draw := if ved.query_type in [.search, .search_in_folder, .grep] {
ved.search_query
} else {
ved.query
}
ved.gg.draw_text(x + 10, y + ved.cfg.line_height, query_to_draw, ved.cfg.txt_cfg)
// Draw cursor
cursor_x := x + 10 + query_to_draw.len * ved.cfg.char_width + 1 // cursor
cursor_y := y + ved.cfg.line_height + 2
ved.gg.draw_rect(x: cursor_x, y: cursor_y, w: 2, h: ved.cfg.line_height - 4)
// Draw "LoC" column header for ctrlp and grep
if ved.query_type in [.ctrlp, .grep] {
loc_header := 'LoC'
loc_hx := x + width - 10 - loc_header.len * ved.cfg.char_width
ved.gg.draw_text2(
x: loc_hx
y: y + ved.cfg.line_height
text: loc_header
color: ved.cfg.comment_color
)
}
// Draw separator between query and files
if ved.query_type !in [.search, .cam, .run, .alert] { // Exclude alert too
ved.gg.draw_rect(
x: x
y: y + ved.cfg.line_height * 2
w: width
h: 1
color: ved.cfg.comment_color
)
}
// Draw files/results list
ved.draw_query_results(ved.query_type, x, y, width) // Pass width
}
// Renamed and generalized function to draw results list
fn (mut ved Ved) draw_query_results(kind QueryType, x int, y int, width int) {
mut j := 0 // Index for visible item count
line_y_start := y + ved.cfg.line_height * 2 + line_padding // Start drawing below separator
match kind {
.ctrlp {
// Iterate over the pre-filtered results starting from scroll offset
for i := ved.gg_scroll; i < ved.ctrlp_results.len; i++ {
// Stop drawing if we exceed the display limit
if j >= nr_ctrlp_results {
break
}
result := ved.ctrlp_results[i]
yy := line_y_start + (ved.cfg.line_height + line_padding) * j
if i == ved.gg_pos { // Use index `i` for selection highlight
ved.gg.draw_rect_filled(x, yy, width, ved.cfg.line_height + line_padding,
ved.cfg.vcolor)
}
ved.gg.draw_text(x + 10, yy + line_padding / 2, result.display_name, ved.cfg.txt_cfg)
// Draw file LOC count on the right
full_path := os.join_path(result.workspace_path, result.file_path)
if full_path !in ved.gg_file_locs {
content := os.read_file(full_path) or { '' }
ved.gg_file_locs[full_path] = content.count('\n') + 1
}
loc := ved.gg_file_locs[full_path]
if loc > 0 {
loc_str := '${loc}'
loc_x := x + width - 10 - loc_str.len * ved.cfg.char_width
ved.gg.draw_text2(
x: loc_x
y: yy + line_padding / 2
text: loc_str
color: ved.cfg.comment_color
)
}
j++
}
}
.grep {
// Start from scroll offset
for i := ved.gg_scroll; i < ved.gg_lines.len; i++ {
if j >= max_grep_lines { // Use grep limit
break
}
s := ved.gg_lines[i]
yy := line_y_start + (ved.cfg.line_height + line_padding) * j
if i == ved.gg_pos {
ved.gg.draw_rect_filled(x, yy, width, ved.cfg.line_height + line_padding,
ved.cfg.vcolor) // Use passed width
}
pos := s.index(':') or {
j++
continue
}
path := s[..pos].limit(55)
pos2 := s.index_after(':', pos + 1) or { -1 }
if pos2 == -1 || pos2 >= s.len - 1 {
j++
continue
}
text := s[pos2 + 1..].trim_space().limit(100)
line_nr := s[pos + 1..pos2]
// Draw path and line number
ved.gg.draw_text2(
x: x + 10
y: yy + line_padding / 2
text: path.limit(50) + ':${line_nr}'
color: gg.purple
)
// Draw matching text part (adjust x position)
text_x := x + 10 + (path.limit(50).len + 1 + line_nr.len + 2) * ved.cfg.char_width // Approximate position
ved.gg.draw_text(text_x, yy + line_padding / 2, text, ved.cfg.txt_cfg)
// Draw file LOC count on the right
file_key := s[..pos]
if file_key !in ved.gg_file_locs {
full_path := ved.workspace + '/' + file_key
content := os.read_file(full_path) or { '' }
ved.gg_file_locs[file_key] = content.count('\n') + 1
}
loc := ved.gg_file_locs[file_key]
if loc > 0 {
loc_str := '${loc}'
loc_x := x + width - 10 - loc_str.len * ved.cfg.char_width
ved.gg.draw_text2(
x: loc_x
y: yy + line_padding / 2
text: loc_str
color: ved.cfg.comment_color
)
}
j++
}
}
.ctrlj {
// TODO: Implement drawing for ctrlj if needed, similar to ctrlp
// using ved.open_paths[ved.workspace_idx]
}
else {
// No list for other query types
}
}
}
// Open file on enter for Ctrl+P
fn (mut ved Ved) ctrlp_open() {
println('ctrlpopen gg_pos=${ved.gg_pos}')
if ved.gg_pos < 0 || ved.gg_pos >= ved.ctrlp_results.len {
println(1)
// Attempt to open if only one result and selection is invalid (e.g., -1)
// if ved.ctrlp_results.len == 1 {
println('set to 0')
ved.gg_pos = 0
//} else {
// println('invalid index')
// return // Invalid selection index
//}
}
// Get the selected result
selected_result := ved.ctrlp_results[ved.gg_pos]
// Construct the full path using the result's workspace and file path
full_path := os.join_path(selected_result.workspace_path, selected_result.file_path)
// Open the file in the current view
// This might open a file from another workspace in the current view's split.
// Consider if a workspace switch is desired later.
ved.view.open_file(full_path, 0)
// Reset state after opening
ved.gg_pos = -1
ved.query = ''
ved.ctrlp_results = []
ved.save_session() // Save session as a file was potentially opened/switched
}
// TODO merge with fn above
fn (mut ved Ved) ctrlj_open() {
// Ensure gg_pos is valid for the open_paths list
current_open_paths := ved.open_paths[ved.workspace_idx]
if ved.gg_pos < 0 || ved.gg_pos >= current_open_paths.len {
// Attempt to open if only one result and selection is invalid
if current_open_paths.len == 1 && ved.query == '' { // Assuming only one open file if query is empty
ved.gg_pos = 0
} else {
// Need filtering logic here if query is used for ctrlj
return
}
}
// Filter open paths based on query to find the actual selected path
// (gg_pos relates to the *filtered* list shown, not the full list)
// This part needs careful implementation matching how ctrlj filtering works.
// Simple filtering for demonstration:
mut filtered_paths := []string{}
for p in current_open_paths {
if p.to_lower().contains(ved.query.to_lower()) {
filtered_paths << p
}
}
if ved.gg_pos < 0 || ved.gg_pos >= filtered_paths.len {
return
}
selected_relative_path := filtered_paths[ved.gg_pos].trim_space()
if selected_relative_path == '' {
return
}
// Construct full path (assuming ctrlj shows files relative to current workspace)
mut space := ved.workspace
if space == '' {
space = '.'
}
full_path := os.join_path(space, selected_relative_path)
ved.view.open_file(full_path, 0)
// Reset state
ved.gg_pos = -1
ved.query = ''
ved.save_session()
}
fn (mut ved Ved) git_grep() {
ved.gg_pos = 0 // select the first result for faster switching to the right file =
// (especially if there's only one result)
ved.gg_scroll = 0 // Reset scroll offset
// Parse extension filter: ^.ext query
mut query := ved.search_query
mut ext_filter := ''
if query.starts_with('^.') {
// Extract extension and query
parts := query.split(' ')
if parts.len >= 2 {
ext_filter = parts[0][2..] // Remove ^.
query = parts[1..].join(' ') // Rest is the actual query
}
}
query = query.replace('$', '\\\$')
// Build git grep command with optional extension filter
mut cmd := 'git -C "${ved.workspace}" grep -F -n "${query}"'
if ext_filter != '' {
cmd += ' -- "*.${ext_filter}"'
}
s := os.execute(cmd)
if s.exit_code == -1 {
return
}
lines := s.output.split_into_lines()
ved.gg_lines = []
top_loop: for line in lines {
if line.contains('thirdparty/') {
continue
}
if line.contains('LICENSE:') {
continue
}
if line.contains('Binary file ') && line.contains(' matches') {
continue
}
// Handle grep file extensions to filter (defined in .ved file per workspace)
if ved.grep_file_exts[ved.workspace].len > 0 {
for ext in ved.grep_file_exts[ved.workspace] {
if !line.contains('.${ext}:') {
continue top_loop
}
}
}
ved.gg_lines << line
}
ved.gg_file_locs = map[string]int{}
}
enum SearchType {
backward
forward
}
fn (mut ved Ved) search(search_type SearchType) {
println('search() query=${ved.search_query}')
if ved.search_query == '' {
return
}
mut view := ved.view
mut to := view.lines.len
mut di := 1
goback := search_type == .backward
start_line := if goback { view.y - 1 } else { view.y + 1 } // Start search from next/prev line
if search_type == .backward {
to = -1 // Go down to index 0
di = -1
}
// First pass: from start_line to end/beginning of file
for i := start_line; true; i += di {
if (goback && i <= to) || (!goback && i >= to) {
break // Reached end/beginning
}
if i >= view.lines.len || i < 0 { // Bounds check
continue
}
line := view.lines[i]
if pos := line.index(ved.search_query) {
// Found
if i >= view.from && i < view.from + ved.page_height { // Check < page_height
ved.prev_y = view.y
view.set_y(i)
} else {
ved.move_to_line(i)
}
view.x = pos
ved.add_to_search_history() // Add to history upon successful find
return
}
}
// Second pass (wrap around): from beginning/end to original line
start_wrap := if goback { view.lines.len - 1 } else { 0 }
end_wrap := if goback { view.y } else { view.y } // Search up to original line
for i := start_wrap; true; i += di {
if (goback && i < end_wrap) || (!goback && i > end_wrap) {
break // Reached original line
}
if i >= view.lines.len || i < 0 { // Bounds check
continue
}
line := view.lines[i]
if pos := line.index(ved.search_query) {
// Found on wrap
if i >= view.from && i < view.from + ved.page_height {
ved.prev_y = view.y
view.set_y(i)
} else {
ved.move_to_line(i)
}
view.x = pos
ved.add_to_search_history() // Add to history upon successful find
return
}
}
// If still not found and searching in folder is enabled
if ved.search_dir != '' && search_type == .forward { // Only support forward search in folder for now
ext := '.' + ved.view.path.after('.')
// Ensure search_dir exists and is a directory
if !os.is_dir(ved.search_dir) {
println('Search directory "${ved.search_dir}" not found or is not a directory.')
ved.search_dir = '' // Reset search dir if invalid
return
}
files := os.walk_ext(ved.search_dir, ext) // Use the specified search_dir
current_file_index := files.index(ved.view.path) // Find index of current file
for i := current_file_index + 1; i < files.len; i++ { // Start from file *after* current
file_path := files[i]
if file_path == ved.view.path {
continue
}
// Should not happen, but safe check
text := os.read_file(file_path) or {
println('Error reading file: ${file_path}')
continue // Skip file if cannot read
}
if _ := text.index(ved.search_query) {
ved.view.open_file(file_path, 0) // Open the new file
// Now perform a search within the newly opened file to find the first match
ved.search(search_type) // Recursive call to find position in the new file
ved.add_to_search_history() // Add to history
return
}
}
// If search reaches end of directory without finding, maybe wrap around? (Optional)
println('Search query "${ved.search_query}" not found in folder "${ved.search_dir}".')
} else {
println('Search query "${ved.search_query}" not found in current file.')
}
}
// Helper to add query to history only if it's new
fn (mut ved Ved) add_to_search_history() {
if ved.search_query != ''
&& (ved.search_history.len == 0 || ved.search_history.last() != ved.search_query) {
ved.search_history << ved.search_query
ved.search_idx = ved.search_history.len // Point index past the end for new searches
}
}
fn (ved &Ved) load_all_tasks() {
/*
mut rows := ved.timer.db.q_strings('select distinct name from tasks')
for row in rows {
t := row.vals[0]
ved.top_tasks << t
}
println(ved.top_tasks)
*/
}