Module: Sergeant::Modals::Navigation

Included in:
Sergeant::Modals
Defined in:
lib/sergeant/modals/navigation.rb

Instance Method Summary collapse

Instance Method Details

#filter_current_viewObject



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
# File 'lib/sergeant/modals/navigation.rb', line 244

def filter_current_view
  max_y = lines
  max_x = cols

  modal_height = 8
  modal_width = [70, max_x - 4].min
  modal_y = (max_y - modal_height) / 2
  modal_x = (max_x - modal_width) / 2

  # Draw modal
  (modal_y..(modal_y + modal_height)).each do |y|
    setpos(y, modal_x)
    attron(color_pair(3)) do
      addstr(' ' * modal_width)
    end
  end

  setpos(modal_y, modal_x)
  attron(color_pair(4) | Curses::A_BOLD) do
    addstr("\u250C#{'' * (modal_width - 2)}\u2510")
  end

  setpos(modal_y + 1, modal_x)
  attron(color_pair(4) | Curses::A_BOLD) do
    addstr('')
  end
  attron(color_pair(5) | Curses::A_BOLD) do
    addstr(' Filter Current View '.center(modal_width - 2))
  end
  attron(color_pair(4) | Curses::A_BOLD) do
    addstr('')
  end

  setpos(modal_y + 2, modal_x)
  attron(color_pair(4)) do
    addstr("\u251C#{'' * (modal_width - 2)}\u2524")
  end

  setpos(modal_y + 3, modal_x)
  attron(color_pair(4)) do
    addstr('')
  end
  addstr('Enter text to filter files/folders (ESC to clear):'.ljust(modal_width - 4))
  attron(color_pair(4)) do
    addstr('')
  end

  setpos(modal_y + 4, modal_x)
  attron(color_pair(4)) do
    addstr('')
  end
  prompt = 'Filter: '
  attron(color_pair(5)) do
    addstr(prompt)
  end
  addstr(' ' * (modal_width - 4 - prompt.length))
  attron(color_pair(4)) do
    addstr('')
  end

  setpos(modal_y + 6, modal_x)
  attron(color_pair(4)) do
    addstr('')
  end
  attron(color_pair(4) | Curses::A_DIM) do
    count = @all_items.length - (@all_items.any? { |i| i[:name] == '..' } ? 1 : 0)
    addstr(" #{count} items in current directory ".center(modal_width - 2))
  end
  attron(color_pair(4)) do
    addstr('')
  end

  setpos(modal_y + modal_height - 1, modal_x)
  attron(color_pair(4) | Curses::A_BOLD) do
    addstr("\u2514#{'' * (modal_width - 2)}\u2518")
  end

  # Input handling
  curs_set(1)
  echo
  input_width = modal_width - 6 - prompt.length
  filter_input = @filter_text.dup

  setpos(modal_y + 4, modal_x + 2 + prompt.length)
  addstr(filter_input.ljust(input_width))
  setpos(modal_y + 4, modal_x + 2 + prompt.length + filter_input.length)

  loop do
    refresh
    ch = getch

    case ch
    when 10, 13  # Enter
      break
    when 27  # ESC - clear filter
      filter_input = ''
      break
    when 127, Curses::Key::BACKSPACE
      if filter_input.length.positive?
        filter_input = filter_input[0...-1]
        setpos(modal_y + 4, modal_x + 2 + prompt.length)
        addstr(filter_input.ljust(input_width))
        setpos(modal_y + 4, modal_x + 2 + prompt.length + filter_input.length)
      end
    else
      if ch.is_a?(String) && filter_input.length < input_width
        filter_input += ch
        setpos(modal_y + 4, modal_x + 2 + prompt.length)
        addstr(filter_input.ljust(input_width))
        setpos(modal_y + 4, modal_x + 2 + prompt.length + filter_input.length)
      end
    end
  end

  noecho
  curs_set(0)

  # Apply filter
  @filter_text = filter_input.strip
  @selected_index = 0
  @scroll_offset = 0
  force_refresh  # Force refresh to apply filter
end

#goto_bookmarkObject



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
# File 'lib/sergeant/modals/navigation.rb', line 8

def goto_bookmark
  return if @bookmarks.empty? && show_no_bookmarks_modal

  max_y = lines
  max_x = cols

  modal_height = [@bookmarks.length + 8, max_y - 4].min
  modal_width = [60, max_x - 4].min
  modal_y = (max_y - modal_height) / 2
  modal_x = (max_x - modal_width) / 2

  (modal_y..(modal_y + modal_height)).each do |y|
    setpos(y, modal_x)
    attron(color_pair(3)) do
      addstr(' ' * modal_width)
    end
  end

  setpos(modal_y, modal_x)
  attron(color_pair(4) | Curses::A_BOLD) do
    addstr("\u250C#{'' * (modal_width - 2)}\u2510")
  end

  setpos(modal_y + 1, modal_x)
  attron(color_pair(4) | Curses::A_BOLD) do
    addstr('')
  end
  attron(color_pair(5) | Curses::A_BOLD) do
    title = ' Bookmarks '.center(modal_width - 2)
    addstr(title)
  end
  attron(color_pair(4) | Curses::A_BOLD) do
    addstr('')
  end

  setpos(modal_y + 2, modal_x)
  attron(color_pair(4)) do
    addstr("\u251C#{'' * (modal_width - 2)}\u2524")
  end

  visible_bookmarks = @bookmarks.to_a.first(modal_height - 7)
  visible_bookmarks.each_with_index do |(name, path), idx|
    setpos(modal_y + 3 + idx, modal_x)
    attron(color_pair(4)) do
      addstr('')
    end

    attron(color_pair(1) | Curses::A_BOLD) do
      addstr(" #{name}".ljust(20))
    end

    path_space = modal_width - 23
    display_path = path.length > path_space ? "...#{path[-(path_space - 3)..]}" : path
    addstr(display_path.ljust(path_space - 1))

    attron(color_pair(4)) do
      addstr('')
    end
  end

  (visible_bookmarks.length...(modal_height - 7)).each do |idx|
    setpos(modal_y + 3 + idx, modal_x)
    attron(color_pair(4)) do
      addstr("\u2502#{' ' * (modal_width - 2)}\u2502")
    end
  end

  input_line = modal_y + modal_height - 4
  setpos(input_line, modal_x)
  attron(color_pair(4)) do
    addstr("\u251C#{'' * (modal_width - 2)}\u2524")
  end

  setpos(input_line + 1, modal_x)
  attron(color_pair(4)) do
    addstr('')
  end
  prompt = ' Enter bookmark name: '
  attron(color_pair(5)) do
    addstr(prompt)
  end
  addstr(' ' * (modal_width - 2 - prompt.length))
  attron(color_pair(4)) do
    addstr('')
  end

  setpos(input_line + 2, modal_x)
  attron(color_pair(4)) do
    addstr('')
  end

  curs_set(1)
  echo
  setpos(input_line + 2, modal_x + 2)

  input_width = modal_width - 5
  bookmark_name = ''

  loop do
    ch = getch

    case ch
    when 10, 13
      break
    when 27
      bookmark_name = ''
      break
    when 127, Curses::Key::BACKSPACE
      if bookmark_name.length.positive?
        bookmark_name = bookmark_name[0...-1]
        setpos(input_line + 2, modal_x + 2)
        addstr(bookmark_name.ljust(input_width))
        setpos(input_line + 2, modal_x + 2 + bookmark_name.length)
      end
    else
      if ch.is_a?(String) && bookmark_name.length < input_width
        bookmark_name += ch
        setpos(input_line + 2, modal_x + 2)
        addstr(bookmark_name.ljust(input_width))
      end
    end

    refresh
  end

  noecho
  curs_set(0)

  setpos(modal_y + modal_height - 1, modal_x)
  attron(color_pair(4) | Curses::A_BOLD) do
    addstr("\u2514#{'' * (modal_width - 2)}\u2518")
  end

  refresh

  bookmark_name = bookmark_name.strip

  return if bookmark_name.empty?

  if @bookmarks.key?(bookmark_name)
    target_path = @bookmarks[bookmark_name]
    if Dir.exist?(target_path)
      @current_dir = target_path
      @selected_index = 0
      @scroll_offset = 0
    else
      show_error_modal("Bookmark path doesn't exist")
    end
  else
    show_error_modal("Bookmark '#{bookmark_name}' not found")
  end
end

#show_history_modalObject



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
# File 'lib/sergeant/modals/navigation.rb', line 368

def show_history_modal
  return if @directory_history.empty? && show_no_history_modal

  max_y = lines
  max_x = cols

  modal_height = [@directory_history.length + 8, max_y - 4].min
  modal_width = [70, max_x - 4].min
  modal_y = (max_y - modal_height) / 2
  modal_x = (max_x - modal_width) / 2

  selected = 0
  scroll_offset = 0

  loop do
    # Draw modal background
    (modal_y..(modal_y + modal_height)).each do |y|
      setpos(y, modal_x)
      attron(color_pair(3)) do
        addstr(' ' * modal_width)
      end
    end

    # Draw border
    setpos(modal_y, modal_x)
    attron(color_pair(4) | Curses::A_BOLD) do
      addstr("\u250C#{'' * (modal_width - 2)}\u2510")
    end

    setpos(modal_y + 1, modal_x)
    attron(color_pair(4) | Curses::A_BOLD) do
      addstr('')
    end
    attron(color_pair(5) | Curses::A_BOLD) do
      title = ' Recent Directories '.center(modal_width - 2)
      addstr(title)
    end
    attron(color_pair(4) | Curses::A_BOLD) do
      addstr('')
    end

    setpos(modal_y + 2, modal_x)
    attron(color_pair(4)) do
      addstr("\u251C#{'' * (modal_width - 2)}\u2524")
    end

    # Draw history entries
    visible_height = modal_height - 7
    visible_history = @directory_history[scroll_offset...(scroll_offset + visible_height)]

    visible_history.each_with_index do |dir, idx|
      actual_idx = scroll_offset + idx
      setpos(modal_y + 3 + idx, modal_x)
      attron(color_pair(4)) do
        addstr('')
      end

      # Highlight selected
      if actual_idx == selected
        attron(color_pair(3) | Curses::A_BOLD) do
          display_dir = dir.length > modal_width - 6 ? "...#{dir[-(modal_width - 9)..]}" : dir
          addstr(display_dir.ljust(modal_width - 4))
        end
      else
        display_dir = dir.length > modal_width - 6 ? "...#{dir[-(modal_width - 9)..]}" : dir
        addstr(display_dir.ljust(modal_width - 4))
      end

      attron(color_pair(4)) do
        addstr('')
      end
    end

    # Fill remaining lines
    (visible_history.length...visible_height).each do |idx|
      setpos(modal_y + 3 + idx, modal_x)
      attron(color_pair(4)) do
        addstr("\u2502#{' ' * (modal_width - 2)}\u2502")
      end
    end

    # Draw footer
    setpos(modal_y + modal_height - 4, modal_x)
    attron(color_pair(4)) do
      addstr("\u251C#{'' * (modal_width - 2)}\u2524")
    end

    setpos(modal_y + modal_height - 3, modal_x)
    attron(color_pair(4)) do
      addstr('')
    end
    addstr('↑/↓: Navigate  Enter: Go  ESC: Cancel'.ljust(modal_width - 4))
    attron(color_pair(4)) do
      addstr('')
    end

    setpos(modal_y + modal_height - 2, modal_x)
    attron(color_pair(4)) do
      addstr('')
    end
    addstr("#{selected + 1}/#{@directory_history.length}".ljust(modal_width - 4))
    attron(color_pair(4)) do
      addstr('')
    end

    setpos(modal_y + modal_height - 1, modal_x)
    attron(color_pair(4)) do
      addstr("\u2514#{'' * (modal_width - 2)}\u2518")
    end

    refresh

    # Handle input
    ch = getch
    case ch
    when Curses::Key::UP, 'k'
      selected = [selected - 1, 0].max
      scroll_offset = selected if selected < scroll_offset
    when Curses::Key::DOWN, 'j'
      selected = [selected + 1, @directory_history.length - 1].min
      scroll_offset = selected - visible_height + 1 if selected >= scroll_offset + visible_height
    when 10, 13  # Enter
      target_dir = @directory_history[selected]
      if File.directory?(target_dir)
        @current_dir = target_dir
        @selected_index = 0
        @scroll_offset = 0
        force_refresh
      end
      break
    when 27, 'q'  # ESC
      break
    end
  end
end

#show_no_bookmarks_modalObject



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
# File 'lib/sergeant/modals/navigation.rb', line 161

def show_no_bookmarks_modal
  max_y = lines
  max_x = cols

  modal_height = 10
  modal_width = 60
  modal_y = (max_y - modal_height) / 2
  modal_x = (max_x - modal_width) / 2

  (modal_y..(modal_y + modal_height)).each do |y|
    setpos(y, modal_x)
    attron(color_pair(3)) do
      addstr(' ' * modal_width)
    end
  end

  setpos(modal_y, modal_x)
  attron(color_pair(4) | Curses::A_BOLD) do
    addstr("\u250C#{'' * (modal_width - 2)}\u2510")
  end

  setpos(modal_y + 1, modal_x)
  attron(color_pair(4) | Curses::A_BOLD) do
    addstr('')
  end
  attron(color_pair(5) | Curses::A_BOLD) do
    addstr(' No Bookmarks Defined '.center(modal_width - 2))
  end
  attron(color_pair(4) | Curses::A_BOLD) do
    addstr('')
  end

  setpos(modal_y + 2, modal_x)
  attron(color_pair(4)) do
    addstr("\u251C#{'' * (modal_width - 2)}\u2524")
  end

  messages = [
    'Add bookmarks to ~/.sgtrc:',
    '',
    '[bookmarks]',
    'home=/home/user',
    'projects=~/projects'
  ]

  messages.each_with_index do |msg, idx|
    setpos(modal_y + 3 + idx, modal_x)
    attron(color_pair(4)) do
      addstr('')
    end
    if idx > 1
      attron(color_pair(1)) do
        addstr(msg.ljust(modal_width - 4))
      end
    else
      addstr(msg.ljust(modal_width - 4))
    end
    attron(color_pair(4)) do
      addstr('')
    end
  end

  setpos(modal_y + 9, modal_x)
  attron(color_pair(4)) do
    addstr('')
  end
  attron(color_pair(4) | Curses::A_DIM) do
    addstr(' Press any key to continue '.center(modal_width - 2))
  end
  attron(color_pair(4)) do
    addstr('')
  end

  setpos(modal_y + modal_height - 1, modal_x)
  attron(color_pair(4) | Curses::A_BOLD) do
    addstr("\u2514#{'' * (modal_width - 2)}\u2518")
  end

  refresh
  getch
  true
end

#show_no_history_modalObject



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
# File 'lib/sergeant/modals/navigation.rb', line 504

def show_no_history_modal
  max_y = lines
  max_x = cols

  modal_height = 8
  modal_width = 50
  modal_y = (max_y - modal_height) / 2
  modal_x = (max_x - modal_width) / 2

  (modal_y..(modal_y + modal_height)).each do |y|
    setpos(y, modal_x)
    attron(color_pair(3)) do
      addstr(' ' * modal_width)
    end
  end

  setpos(modal_y + 3, modal_x)
  attron(color_pair(4)) do
    addstr('')
  end
  addstr('No directory history yet.'.ljust(modal_width - 4))
  attron(color_pair(4)) do
    addstr('')
  end

  setpos(modal_y + 5, modal_x)
  attron(color_pair(4)) do
    addstr('')
  end
  addstr('Press any key to continue'.ljust(modal_width - 4))
  attron(color_pair(4)) do
    addstr('')
  end

  refresh
  getch
  true
end