rufio 1.6.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +49 -0
  3. data/README.md +17 -10
  4. data/README_ja.md +17 -9
  5. data/SECURITY.md +14 -6
  6. data/docs/csv_cell_preview_design_ja.md +682 -0
  7. data/docs/guide.md +109 -47
  8. data/docs/guide_ja.md +110 -48
  9. data/docs/image_preview_design_ja.md +335 -0
  10. data/docs/native_fuzzy_search_design_ja.md +2 -2
  11. data/examples/config.rb +23 -0
  12. data/info/help.md +32 -27
  13. data/info/keybindings.md +42 -12
  14. data/info/welcome.md +4 -6
  15. data/lib/rufio/background_status.rb +7 -0
  16. data/lib/rufio/bookmark_controller.rb +0 -45
  17. data/lib/rufio/clipboard.rb +162 -0
  18. data/lib/rufio/command_locator.rb +49 -0
  19. data/lib/rufio/config.rb +7 -2
  20. data/lib/rufio/config_loader.rb +56 -21
  21. data/lib/rufio/csv_table_source.rb +125 -0
  22. data/lib/rufio/encoding_detector.rb +152 -0
  23. data/lib/rufio/file_operation_controller.rb +41 -0
  24. data/lib/rufio/file_preview.rb +92 -19
  25. data/lib/rufio/graphics_layer.rb +223 -0
  26. data/lib/rufio/health_checker.rb +27 -10
  27. data/lib/rufio/help_menu.rb +16 -11
  28. data/lib/rufio/image_preview_view.rb +421 -0
  29. data/lib/rufio/image_source.rb +158 -0
  30. data/lib/rufio/keybind_handler.rb +21 -35
  31. data/lib/rufio/kitty_graphics.rb +128 -0
  32. data/lib/rufio/native/aarch64-linux-gnu/librufio_zig.so +0 -0
  33. data/lib/rufio/native/aarch64-linux-musl/librufio_zig.so +0 -0
  34. data/lib/rufio/native/aarch64-macos/librufio_zig.dylib +0 -0
  35. data/lib/rufio/native/aarch64-windows/rufio_zig.dll +0 -0
  36. data/lib/rufio/native/x86_64-linux-gnu/librufio_zig.so +0 -0
  37. data/lib/rufio/native/x86_64-linux-musl/librufio_zig.so +0 -0
  38. data/lib/rufio/native/x86_64-macos/librufio_zig.dylib +0 -0
  39. data/lib/rufio/native/x86_64-windows/rufio_zig.dll +0 -0
  40. data/lib/rufio/native_content_search.rb +1 -1
  41. data/lib/rufio/native_image.rb +177 -0
  42. data/lib/rufio/native_scanner_zig.rb +22 -0
  43. data/lib/rufio/navigation_controller.rb +47 -16
  44. data/lib/rufio/preview_registry.rb +86 -0
  45. data/lib/rufio/preview_view.rb +363 -0
  46. data/lib/rufio/renderer.rb +8 -8
  47. data/lib/rufio/screen.rb +33 -0
  48. data/lib/rufio/search_prompt.rb +50 -0
  49. data/lib/rufio/{global_search.rb → search_screen.rb} +41 -14
  50. data/lib/rufio/sheet_picker.rb +87 -0
  51. data/lib/rufio/sqlite_table_source.rb +205 -0
  52. data/lib/rufio/tab_mode_manager.rb +1 -3
  53. data/lib/rufio/table_cursor.rb +117 -0
  54. data/lib/rufio/table_document.rb +261 -0
  55. data/lib/rufio/table_layout.rb +231 -0
  56. data/lib/rufio/table_preview_view.rb +376 -0
  57. data/lib/rufio/table_query.rb +240 -0
  58. data/lib/rufio/table_source.rb +46 -0
  59. data/lib/rufio/terminal_graphics.rb +168 -0
  60. data/lib/rufio/terminal_ui.rb +63 -111
  61. data/lib/rufio/text_utils.rb +27 -0
  62. data/lib/rufio/ui_renderer.rb +379 -16
  63. data/lib/rufio/version.rb +1 -1
  64. data/lib/rufio/xlsx_table_source.rb +112 -0
  65. data/lib/rufio.rb +19 -5
  66. data/lib_zig/rufio_native/Makefile +6 -4
  67. data/lib_zig/rufio_native/build.zig +10 -0
  68. data/lib_zig/rufio_native/src/image.zig +525 -0
  69. data/lib_zig/rufio_native/src/image_bmp.zig +111 -0
  70. data/lib_zig/rufio_native/src/image_jpeg.zig +878 -0
  71. data/lib_zig/rufio_native/src/image_png.zig +561 -0
  72. data/lib_zig/rufio_native/src/image_resize.zig +170 -0
  73. data/lib_zig/rufio_native/src/image_test.zig +401 -0
  74. data/lib_zig/rufio_native/src/main.zig +1 -0
  75. data/lib_zig/rufio_native/src/search.zig +38 -19
  76. data/lib_zig/rufio_native/src/search_test.zig +35 -0
  77. data/lib_zig/rufio_native/src/testdata/alpha.png +0 -0
  78. data/lib_zig/rufio_native/src/testdata/gradient.bmp +0 -0
  79. data/lib_zig/rufio_native/src/testdata/gradient.jpg +0 -0
  80. data/lib_zig/rufio_native/src/testdata/gradient.png +0 -0
  81. data/retag.sh +4 -4
  82. metadata +37 -9
  83. data/lib/rufio/fuzzy_candidate_cache.rb +0 -258
  84. data/lib/rufio/fuzzy_finder.rb +0 -313
  85. data/lib/rufio/fuzzy_preview_loader.rb +0 -107
  86. data/lib/rufio/rga_result_source.rb +0 -89
  87. data/lib/rufio/search_controller.rb +0 -94
  88. data/lib_zig/rufio_native/src/main.zig.sync +0 -205
@@ -2,6 +2,8 @@
2
2
 
3
3
  require_relative 'text_utils'
4
4
  require_relative 'background_status'
5
+ require_relative 'table_layout'
6
+ require_relative 'preview_view'
5
7
 
6
8
  module Rufio
7
9
  # UIレンダリング専用クラス
@@ -20,6 +22,17 @@ module Rufio
20
22
  CURSOR_OFFSET = 1 # カーソル位置のオフセット
21
23
  ICON_SIZE_PADDING = 12 # アイコン、選択マーク、サイズ情報分
22
24
  BOOKMARK_HIGHLIGHT_DURATION = 0.5 # ブックマークハイライト表示時間(秒)
25
+ TABLE_FOOTER_MIN_HEIGHT = 5 # 表プレビューで最下行に情報行を出す最小の高さ
26
+ TABLE_GRID_START_ROW = 2 # プレビューペインでグリッドが始まる行(0:ファイル名 1:情報)
27
+ SHEET_LIST_START_ROW = 3 # シート一覧が始まる行(0:ファイル名 1:情報 2:一覧の見出し)
28
+ # プレビュー内検索(`/`)の一致の色。現在の一致だけ背景を変える
29
+ SEARCH_MATCH_FG = "\e[30m"
30
+ SEARCH_MATCH_BG = "\e[43m"
31
+ # 現在の一致(n / N で動かしている位置)。モードタブのシアンと区別する
32
+ CURRENT_MATCH_FG = "\e[30m\e[1m"
33
+ CURRENT_MATCH_BG = "\e[45m"
34
+ # Space で選んだカラムの印(ファイル一覧の選択済みアイテムと同じ緑背景・黒文字)
35
+ MARKED_COLUMN_COLOR = "\e[42m\e[30m"
23
36
  TAB_SEPARATOR = ">" # タブ間セパレータ
24
37
 
25
38
  # File display constants
@@ -41,7 +54,8 @@ module Rufio
41
54
  file_preview: nil, background_executor: nil,
42
55
  test_mode: false,
43
56
  left_panel_ratio: 0.5,
44
- preview_enabled: true)
57
+ preview_enabled: true,
58
+ graphics_layer: nil)
45
59
  @screen_width = screen_width
46
60
  @screen_height = screen_height
47
61
  @keybind_handler = keybind_handler
@@ -51,10 +65,13 @@ module Rufio
51
65
  @test_mode = test_mode
52
66
  @left_panel_ratio = left_panel_ratio
53
67
  @preview_enabled = preview_enabled
68
+ # 画像プレビュー(端末のグラフィックス機能)。nil なら画像は出さない
69
+ @graphics_layer = graphics_layer
54
70
 
55
71
  # Preview cache
56
72
  @preview_cache = {}
57
73
  @last_preview_path = nil
74
+ @active_image_view = nil
58
75
 
59
76
  # Syntax highlighter(bat が利用可能な場合のみ動作)
60
77
  @syntax_highlighter = SyntaxHighlighter.new
@@ -65,7 +82,7 @@ module Rufio
65
82
  @cached_bookmark_time = nil
66
83
  @bookmark_cache_ttl = 1.0
67
84
 
68
- # Bookmark highlight (Tab ジャンプ時に 500ms ハイライト)
85
+ # Bookmark highlight (1-9 のジャンプ時に 500ms ハイライト)
69
86
  @highlighted_bookmark_index = nil
70
87
  @highlighted_bookmark_time = nil
71
88
 
@@ -81,8 +98,17 @@ module Rufio
81
98
  # ============================
82
99
 
83
100
  def clear_preview_cache
101
+ @preview_cache.each_value { |entry| entry[:image_view]&.release }
84
102
  @preview_cache.clear
85
103
  @last_preview_path = nil
104
+ @active_image_view = nil
105
+ end
106
+
107
+ # 非同期画像デコードの完了をポーリングする(メインループから毎フレーム呼ぶ)
108
+ def poll_image_preview
109
+ return false unless @active_image_view
110
+
111
+ @active_image_view.poll
86
112
  end
87
113
 
88
114
  def clear_bookmark_cache
@@ -128,6 +154,10 @@ module Rufio
128
154
  # @param job_mode_instance [JobMode, nil] ジョブモードインスタンス
129
155
  def draw_screen(screen, notification_message: nil, fps: nil,
130
156
  in_job_mode: false, job_manager: nil, job_mode_instance: nil)
157
+ # 画像は毎フレーム出し直しの申告が要る(プレビューを描かないモードでは消える)
158
+ @graphics_layer&.clear
159
+ @active_image_view = nil
160
+
131
161
  if in_job_mode
132
162
  # ジョブモード: フッタ y=0(上部)、コンテンツ y=1〜h-2、統合行 y=h-1(下部・パス付き)
133
163
  content_height = @screen_height - JOB_HEADER_FOOTER_MARGIN
@@ -232,7 +262,7 @@ module Rufio
232
262
  # FilePreview の結果ハッシュからプレーンテキスト行を抽出する
233
263
  def extract_preview_lines(preview)
234
264
  case preview[:type]
235
- when 'text', 'code', 'archive'
265
+ when 'text', 'code', 'archive', 'table'
236
266
  preview[:lines]
237
267
  when 'binary'
238
268
  ["(#{ConfigLoader.message('file.binary_file')})", ConfigLoader.message('file.cannot_preview')]
@@ -329,22 +359,31 @@ module Rufio
329
359
  highlighted_wrapped_lines = nil
330
360
 
331
361
  if selected_entry && selected_entry[:type] == 'file'
332
- if @last_preview_path != selected_entry[:path]
333
- full_preview = @file_preview.preview_file(selected_entry[:path])
362
+ # プレビューモード中は、スクロールとファイル内検索(/)のために多めの行を読む
363
+ max_lines = fullscreen ? FilePreview::PREVIEW_MODE_MAX_LINES : FilePreview::DEFAULT_MAX_LINES
364
+ cache_entry = @preview_cache[selected_entry[:path]]
365
+ if @last_preview_path != selected_entry[:path] || cache_entry.nil? ||
366
+ (cache_entry[:max_lines] || 0) < max_lines
367
+ full_preview = @file_preview.preview_file(selected_entry[:path], max_lines: max_lines)
334
368
  preview_content = extract_preview_lines(full_preview)
369
+ existing_image_view = cache_entry&.dig(:image_view)
335
370
  @preview_cache[selected_entry[:path]] = {
336
371
  content: preview_content,
337
372
  preview_data: full_preview,
373
+ max_lines: max_lines,
338
374
  highlighted: nil,
339
375
  wrapped: {},
340
376
  highlighted_wrapped: {}
341
377
  }
378
+ @preview_cache[selected_entry[:path]][:image_view] = existing_image_view if cache_entry&.key?(:image_view)
342
379
  @last_preview_path = selected_entry[:path]
343
380
  else
344
- cache_entry = @preview_cache[selected_entry[:path]]
345
- preview_content = cache_entry[:content] if cache_entry
381
+ preview_content = cache_entry[:content]
346
382
  end
347
383
 
384
+ # 表プレビューはグリッドとして描くので、折り返しもハイライトも通さない
385
+ table_preview = @preview_cache[selected_entry[:path]]&.dig(:preview_data, :type) == 'table'
386
+
348
387
  # bat が利用可能な場合はシンタックスハイライトを取得(非同期)
349
388
  if @syntax_highlighter&.available? && preview_content
350
389
  cache_entry = @preview_cache[selected_entry[:path]]
@@ -381,7 +420,7 @@ module Rufio
381
420
  end
382
421
 
383
422
  # プレーンテキストの折り返し(ハイライトなしのフォールバック)
384
- if preview_content && safe_width > 0 && highlighted_wrapped_lines.nil?
423
+ if preview_content && safe_width > 0 && highlighted_wrapped_lines.nil? && !table_preview
385
424
  cache_entry = @preview_cache[selected_entry[:path]]
386
425
  if cache_entry && cache_entry[:wrapped][safe_width]
387
426
  wrapped_lines = cache_entry[:wrapped][safe_width]
@@ -394,6 +433,37 @@ module Rufio
394
433
 
395
434
  content_x = fullscreen ? 0 : cursor_position + 1
396
435
 
436
+ table_document, table_layout, table_sheet = table_preview_for(selected_entry, safe_width)
437
+ table_rows = nil
438
+ table_status = nil
439
+ table_cursor = nil
440
+ table_col_offset = 0
441
+ if table_layout
442
+ table_cursor = @keybind_handler&.preview_cursor
443
+ if table_cursor
444
+ # カーソルが見えるように、表示できる行数を伝え、横スクロール量を決める
445
+ table_cursor.visible_rows = table_visible_rows(table_sheet, height)
446
+ table_cursor.column_offset = table_layout.offset_for(table_cursor.column, table_cursor.column_offset)
447
+ table_col_offset = table_cursor.column_offset
448
+ end
449
+ table_rows = table_sheet.fetch_rows(
450
+ @keybind_handler&.preview_scroll_offset || 0, [height, 1].max
451
+ )
452
+ table_status = table_status_text(table_document, table_layout, table_sheet, table_col_offset)
453
+ end
454
+
455
+ # シート一覧(t)を開いている間は、グリッドの代わりに一覧を描く
456
+ sheet_picker_title, sheet_picker_rows = sheet_picker_for(table_layout, height)
457
+
458
+ # 画像プレビュー: セルは空白のまま確保し、画素は GraphicsLayer から端末へ送る
459
+ image_lines = apply_image_preview(screen, selected_entry, content_x, safe_width, height)
460
+
461
+ # テキストプレビューの `/` 検索: 折り返し後の行から一致行を求めてビューに渡す
462
+ search_query, search_match_rows = apply_text_search(
463
+ wrapped_lines, highlighted_wrapped_lines, height, safe_width,
464
+ selected_entry && @preview_cache[selected_entry[:path]]
465
+ )
466
+
397
467
  (0...height).each do |i|
398
468
  line_num = i + CONTENT_START_LINE
399
469
 
@@ -412,17 +482,46 @@ module Rufio
412
482
  screen.put_string(content_x, line_num, header)
413
483
 
414
484
  elsif i == 1 && @keybind_handler&.preview_focused?
415
- hint = TextUtils.truncate_to_width(' ESC: Back to files', safe_width)
485
+ # プレビュー種別ごとの情報(表プレビューならセル番地と操作ヒント)
486
+ status = @keybind_handler&.preview_view&.status_line || PreviewView::BACK_HINT
487
+ hint = TextUtils.truncate_to_width(" #{status}", safe_width)
416
488
  screen.put_string(content_x, line_num, hint + ' ' * (safe_width - TextUtils.display_width(hint)))
417
489
 
490
+ elsif i >= 2 && image_lines
491
+ # 画像を出せないときだけ、形式と寸法を文字で出す
492
+ line = image_lines[i - 2]
493
+ screen.put_string(content_x, line_num,
494
+ TextUtils.pad_string_to_width(line ? " #{line}" : '', safe_width))
495
+
496
+ elsif i >= 2 && sheet_picker_rows
497
+ # シート(SQLite のテーブル / Excel のシート)一覧
498
+ draw_sheet_picker_line_to_buffer(screen, content_x, line_num, i, safe_width,
499
+ sheet_picker_title, sheet_picker_rows)
500
+
501
+ elsif i >= 2 && table_layout
502
+ # 行列(グリッド)プレビュー
503
+ screen.put_string(content_x, line_num,
504
+ table_line(table_layout, table_sheet, table_rows, table_status, i, height,
505
+ table_col_offset))
506
+ # 選択カラムの印を先に置き、カーソルセルを後から重ねる(カーソルを隠さない)
507
+ draw_selected_columns_to_buffer(screen, content_x, line_num, table_layout, table_sheet,
508
+ table_rows, table_status, i, height, table_col_offset)
509
+ draw_table_cursor_to_buffer(screen, content_x, line_num, table_layout, table_sheet,
510
+ table_cursor, table_rows, table_status, i, height)
511
+
418
512
  elsif i >= 2 && highlighted_wrapped_lines
419
513
  # シンタックスハイライト付きコンテンツ
420
514
  scroll_offset = @keybind_handler&.preview_scroll_offset || 0
421
515
  display_line_index = i - 2 + scroll_offset
422
516
 
423
517
  if display_line_index < highlighted_wrapped_lines.length
424
- draw_highlighted_line_to_buffer(screen, content_x, line_num,
425
- highlighted_wrapped_lines[display_line_index], safe_width)
518
+ tokens = highlighted_wrapped_lines[display_line_index]
519
+ draw_highlighted_line_to_buffer(screen, content_x, line_num, tokens, safe_width)
520
+ if search_match_rows&.key?(display_line_index)
521
+ plain = " #{tokens.map { |token| token[:text] }.join}"
522
+ draw_text_matches_to_buffer(screen, content_x, line_num, plain, search_query,
523
+ search_match_rows[display_line_index], safe_width)
524
+ end
426
525
  else
427
526
  screen.put_string(content_x, line_num, ' ' * safe_width)
428
527
  end
@@ -441,6 +540,10 @@ module Rufio
441
540
  remaining_space = safe_width - TextUtils.display_width(content_to_print)
442
541
  content_to_print += ' ' * remaining_space if remaining_space > 0
443
542
  screen.put_string(content_x, line_num, content_to_print)
543
+ if search_match_rows&.key?(display_line_index)
544
+ draw_text_matches_to_buffer(screen, content_x, line_num, content_to_print, search_query,
545
+ search_match_rows[display_line_index], safe_width)
546
+ end
444
547
 
445
548
  else
446
549
  screen.put_string(content_x, line_num, ' ' * safe_width)
@@ -448,6 +551,268 @@ module Rufio
448
551
  end
449
552
  end
450
553
 
554
+ # ============================
555
+ # 表プレビュー
556
+ # ============================
557
+
558
+ # 表プレビューのレイアウトを取得する(シートと幅の組ごとにキャッシュ)。
559
+ # プレビューモード中はビューが選んでいるシートを使う。
560
+ # @return [Array(TableDocument, TableLayout, TableSheet)] 表でなければ [nil, nil, nil]
561
+ def table_preview_for(selected_entry, safe_width)
562
+ return [nil, nil, nil] unless selected_entry && selected_entry[:type] == 'file' && safe_width > 0
563
+
564
+ cache_entry = @preview_cache[selected_entry[:path]]
565
+ preview_data = cache_entry && cache_entry[:preview_data]
566
+ return [nil, nil, nil] unless preview_data && preview_data[:type] == 'table'
567
+
568
+ view = @keybind_handler&.preview_view
569
+ document = view&.table? ? view.document : preview_data[:document]
570
+ sheet_index = view&.table? ? view.sheet_index : 0
571
+ # 絞り込み(`/`)中はビューが持つシートを描く
572
+ sheet = view&.table? ? view.sheet : document&.sheet(sheet_index)
573
+ return [nil, nil, nil] unless sheet
574
+
575
+ cache_entry[:table_layout] ||= {}
576
+ key = [document.object_id, sheet.object_id, safe_width]
577
+ layout = (cache_entry[:table_layout][key] ||= TableLayout.build(
578
+ sheet, safe_width, max_column_width: ConfigLoader.table_preview[:max_column_width]
579
+ ))
580
+ [document, layout, sheet]
581
+ end
582
+
583
+ # 画像プレビューの扱い。
584
+ # 送れるときは矩形のセルを確保して GraphicsLayer に依頼し、nil を返す(文字は描かない)。
585
+ # 送れないとき(非対応端末・PNG 以外・大きすぎる)は、ペインに出す説明の行を返す。
586
+ # @return [Array<String>, nil]
587
+ def apply_image_preview(screen, selected_entry, content_x, safe_width, height)
588
+ view = image_view_for(selected_entry)
589
+ return nil unless view && safe_width > 0
590
+
591
+ @active_image_view = view
592
+ rows = [height - TABLE_GRID_START_ROW, 1].max
593
+ top = TABLE_GRID_START_ROW + CONTENT_START_LINE
594
+ # ペインの実ピクセルを伝える(ネイティブのデコードはこの大きさに合わせる)
595
+ cell = TerminalGraphics.cell_pixels
596
+ view.target_pixels = [safe_width * cell[0], rows * cell[1]]
597
+
598
+ return view.detail_lines unless view.displayable?
599
+
600
+ transfer = view.transfer
601
+ # デコード待ちの間は「decoding」と出し、画像はまだ送らない
602
+ return view.loading_lines if transfer.nil?
603
+
604
+ # セルを空白で確保する(通知やフッタが割り込んで画像を削らないようにする)
605
+ screen.reserve(content_x, top, safe_width, rows)
606
+ placement = view.placement(
607
+ max_cols: safe_width,
608
+ max_rows: rows,
609
+ cell_pixels: cell,
610
+ cell_pixels_known: TerminalGraphics.cell_pixels_known?
611
+ )
612
+ @graphics_layer&.request(
613
+ source: transfer[:key],
614
+ # 直送のときは、送ると決まってから初めてファイルを読む
615
+ payload: transfer[:payload],
616
+ encoded: transfer[:encoded],
617
+ format: transfer[:format],
618
+ pixels: transfer[:pixels],
619
+ row: top + 1, col: content_x + 1, # 端末座標は 1 始まり
620
+ cols: placement[:cols], rows: placement[:rows],
621
+ area_rows: placement[:area_rows],
622
+ crop: view.crop
623
+ )
624
+ nil
625
+ end
626
+
627
+ # 画像プレビューのビュー。プレビューモード中はそのビュー、分割表示では
628
+ # 選択中のファイルから作って(パスごとに)キャッシュする
629
+ def image_view_for(selected_entry)
630
+ view = @keybind_handler&.preview_view
631
+ return view if view&.image?
632
+ return nil unless selected_entry && selected_entry[:type] == 'file'
633
+ # プレビューモード中に別種別のビューが出ているなら画像ではない
634
+ return nil if view
635
+
636
+ entry = @preview_cache[selected_entry[:path]]
637
+ return nil unless entry
638
+
639
+ unless entry.key?(:image_view)
640
+ info = ImageSource.read(selected_entry[:path])
641
+ entry[:image_view] = info ? ImagePreviewView.new(info, path: selected_entry[:path]) : nil
642
+ end
643
+ entry[:image_view]
644
+ end
645
+
646
+ # テキストプレビューの `/` 検索。一致行(折り返し後の行番号)をビューに渡し、
647
+ # 描画用に { 行番号 => 現在の一致か } を返す。
648
+ # 折り返しは幅に依存するため、一致行を求めるのは(幅を知っている)ここが担当する。
649
+ # @return [Array(String, Hash), Array(nil, nil)]
650
+ def apply_text_search(wrapped_lines, highlighted_lines, height, width, cache_entry)
651
+ view = @keybind_handler&.preview_view
652
+ return [nil, nil] unless view && !view.table?
653
+
654
+ query = view.search_query
655
+ view.visible_rows = [height - TABLE_GRID_START_ROW, 1].max
656
+ if query.nil? || query.empty?
657
+ view.apply_matches([])
658
+ return [nil, nil]
659
+ end
660
+
661
+ rows = -> { wrapped_lines || highlighted_lines&.map { |tokens| tokens.map { |token| token[:text] }.join } }
662
+ matches = cached_search_matches(cache_entry, query, width, rows)
663
+ view.apply_matches(matches)
664
+
665
+ current = view.current_match
666
+ [query, matches.each_with_object({}) { |index, map| map[index] = index == current }]
667
+ end
668
+
669
+ # 同じ検索語・同じ幅のあいだは一致行を数え直さない
670
+ def cached_search_matches(cache_entry, query, width, rows)
671
+ key = [query, width]
672
+ cache = cache_entry && (cache_entry[:search_matches] ||= {})
673
+ return cache[:matches] if cache && cache[:key] == key
674
+
675
+ needle = query.downcase
676
+ lines = rows.call || []
677
+ matches = lines.each_index.select { |index| lines[index].to_s.downcase.include?(needle) }
678
+ if cache
679
+ cache[:key] = key
680
+ cache[:matches] = matches
681
+ end
682
+ matches
683
+ end
684
+
685
+ # 画面に出した 1 行のうち、検索語に一致した部分だけを塗り替える
686
+ def draw_text_matches_to_buffer(screen, content_x, line_num, text, query, current, safe_width)
687
+ return if query.nil? || query.empty?
688
+
689
+ needle = query.downcase
690
+ lowered = text.downcase
691
+ offset = 0
692
+ background = current ? CURRENT_MATCH_BG : SEARCH_MATCH_BG
693
+ foreground = current ? CURRENT_MATCH_FG : SEARCH_MATCH_FG
694
+ while (found = lowered.index(needle, offset))
695
+ prefix_width = TextUtils.display_width(text[0, found])
696
+ break if prefix_width >= safe_width
697
+
698
+ matched = TextUtils.truncate_to_width(text[found, needle.length].to_s, safe_width - prefix_width)
699
+ screen.put_string(content_x + prefix_width, line_num, matched, fg: foreground, bg: background)
700
+ offset = found + needle.length
701
+ end
702
+ end
703
+
704
+ # シート一覧を開いているなら、見出しと描く行を返す(開いていなければ [nil, nil])
705
+ # @return [Array(String, Array<Hash>)]
706
+ def sheet_picker_for(table_layout, height)
707
+ return [nil, nil] unless table_layout
708
+
709
+ view = @keybind_handler&.preview_view
710
+ return [nil, nil] unless view&.table? && view.picker_open?
711
+
712
+ # 見出しの下に収まる行数を毎フレーム伝える(選択行が見えるようにスクロールする)
713
+ view.picker.visible_rows = [height - SHEET_LIST_START_ROW, 1].max
714
+ [view.picker_title, view.picker_rows]
715
+ end
716
+
717
+ # 一覧のペイン内 i 行目(2:見出し、3 行目以降:シート名)を描く
718
+ def draw_sheet_picker_line_to_buffer(screen, content_x, line_num, index, safe_width, title, rows)
719
+ if index == TABLE_GRID_START_ROW
720
+ screen.put_string(content_x, line_num, TextUtils.pad_string_to_width(" #{title}", safe_width))
721
+ return
722
+ end
723
+
724
+ row = rows[index - SHEET_LIST_START_ROW]
725
+ text = TextUtils.pad_string_to_width(" #{row ? row[:text] : ''}", safe_width)
726
+ fg = row && row[:selected] ? ColorHelper.color_to_selected_ansi(ConfigLoader.colors[:selected]) : nil
727
+ screen.put_string(content_x, line_num, text, fg: fg)
728
+ end
729
+
730
+ # グリッドでデータ行として使える行数(ヘッダ行と最下行の情報行を除く)
731
+ def table_visible_rows(sheet, height)
732
+ rows = height - TABLE_GRID_START_ROW
733
+ rows -= 1 if sheet.header
734
+ rows -= 1 if height >= TABLE_FOOTER_MIN_HEIGHT
735
+ [rows, 1].max
736
+ end
737
+
738
+ # 最下行に出す情報(シート名・行数・列数・文字コード・切り詰め・隠れている列数)
739
+ def table_status_text(document, layout, sheet, col_offset = 0)
740
+ view = @keybind_handler&.preview_view
741
+ parts = [document.status_line(view&.table? ? view.sheet_index : 0, sheet)]
742
+ hidden = layout.hidden_column_count(col_offset)
743
+ # 狭いペインでも最下行に収まるよう短く書く
744
+ parts << "+#{hidden} hidden" if hidden.positive?
745
+ parts.compact.reject(&:empty?).join(' ')
746
+ end
747
+
748
+ # ペイン内 i 行目(i >= 2)に描くグリッド 1 行分の文字列
749
+ def table_line(layout, sheet, rows, status, index, height, col_offset = 0)
750
+ return TextUtils.pad_string_to_width(status, layout.width) if table_footer_row?(status, index, height)
751
+
752
+ data_index = table_data_index(sheet, index)
753
+ return layout.header_line(col_offset) if data_index.nil?
754
+
755
+ row = rows && rows[data_index]
756
+ return ' ' * layout.width unless row
757
+
758
+ scroll_offset = @keybind_handler&.preview_scroll_offset || 0
759
+ layout.row_line(scroll_offset + data_index + 1, row, col_offset)
760
+ end
761
+
762
+ # Space で選んだカラムのセル(見出し行も含む)を選択マーク色で上書きする
763
+ def draw_selected_columns_to_buffer(screen, content_x, line_num, layout, sheet, rows, status,
764
+ index, height, col_offset)
765
+ view = @keybind_handler&.preview_view
766
+ return unless view&.table?
767
+
768
+ selected = view.selected_columns
769
+ return if selected.empty?
770
+ return if table_footer_row?(status, index, height)
771
+
772
+ data_index = table_data_index(sheet, index)
773
+ row = data_index.nil? ? nil : rows && rows[data_index]
774
+ # データのない行(表の下の空白)には印を付けない
775
+ return if !data_index.nil? && row.nil?
776
+
777
+ selected.each do |column|
778
+ x = layout.column_x(column, col_offset)
779
+ next if x.nil?
780
+
781
+ text = data_index.nil? ? layout.format_header_cell(column) : layout.format_cell(row[column], column)
782
+ screen.put_string(content_x + x, line_num, text, fg: MARKED_COLUMN_COLOR)
783
+ end
784
+ end
785
+
786
+ # カーソルのあるセルだけを反転色で上書きする
787
+ def draw_table_cursor_to_buffer(screen, content_x, line_num, layout, sheet, cursor, rows, status, index, height)
788
+ return if cursor.nil? || cursor.empty?
789
+ return if table_footer_row?(status, index, height)
790
+
791
+ data_index = table_data_index(sheet, index)
792
+ return if data_index.nil?
793
+ return unless cursor.row - cursor.row_offset == data_index
794
+
795
+ x = layout.column_x(cursor.column, cursor.column_offset)
796
+ return if x.nil?
797
+
798
+ row = rows && rows[data_index]
799
+ text = layout.format_cell(row && row[cursor.column], cursor.column)
800
+ screen.put_string(content_x + x, line_num, text,
801
+ fg: ColorHelper.color_to_selected_ansi(ConfigLoader.colors[:selected]))
802
+ end
803
+
804
+ # @return [Integer, nil] 表示中のデータ行の添字。ヘッダ行なら nil
805
+ def table_data_index(sheet, index)
806
+ grid_row = index - TABLE_GRID_START_ROW
807
+ return nil if sheet.header && grid_row.zero?
808
+
809
+ sheet.header ? grid_row - 1 : grid_row
810
+ end
811
+
812
+ def table_footer_row?(status, index, height)
813
+ !status.nil? && !status.empty? && height >= TABLE_FOOTER_MIN_HEIGHT && index == height - 1
814
+ end
815
+
451
816
  # ハイライト済みトークン列を1行分 Screen バッファに描画する
452
817
  def draw_highlighted_line_to_buffer(screen, x, y, tokens, max_width)
453
818
  current_x = x
@@ -491,7 +856,7 @@ module Rufio
491
856
  footer_content = help_text.ljust(@screen_width)[0...@screen_width]
492
857
  screen.put_string(0, y, footer_content, fg: "\e[7m")
493
858
  else
494
- # ブックマークをキャッシュ(Tab移動と同じソース:keybind_handler経由で取得)
859
+ # ブックマークをキャッシュ(keybind_handler 経由で取得)
495
860
  current_time = Time.now
496
861
  if @cached_bookmarks.nil? || @cached_bookmark_time.nil? || (current_time - @cached_bookmark_time) > @bookmark_cache_ttl
497
862
  @cached_bookmarks = @keybind_handler.bookmark_list
@@ -560,7 +925,7 @@ module Rufio
560
925
  end
561
926
  screen.put_string(0, y, footer_content, fg: "\e[90m")
562
927
 
563
- # Tab ジャンプ時:対象ブックマークを 500ms ハイライト(セカンドパス)
928
+ # 1-9 のジャンプ時:対象ブックマークを 500ms ハイライト(セカンドパス)
564
929
  if @highlighted_bookmark_index && !bookmark_highlight_expired? && available_width > 3
565
930
  highlight_idx = @highlighted_bookmark_index
566
931
  if highlight_idx < bookmark_parts.length
@@ -810,9 +1175,7 @@ module Rufio
810
1175
  def sync_tab_mode_with_keybind_handler
811
1176
  return unless @keybind_handler
812
1177
 
813
- current_mode = if @keybind_handler.global_search_mode?
814
- :global
815
- elsif @keybind_handler.in_job_mode?
1178
+ current_mode = if @keybind_handler.in_job_mode?
816
1179
  :jobs
817
1180
  elsif @keybind_handler.help_mode?
818
1181
  :help
data/lib/rufio/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rufio
4
- VERSION = '1.6.0'
4
+ VERSION = '2.0.0'
5
5
  end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'table_source'
4
+ require_relative 'table_document'
5
+
6
+ module Rufio
7
+ # Excel(.xlsx / .xlsm)と OpenDocument(.ods)を表として読む TableSource。
8
+ # 既に依存している roo を使い、シートは表示するものだけを読む。
9
+ class XlsxTableSource < TableSource
10
+ EXTENSIONS = %w[.xlsx .xlsm .ods].freeze
11
+
12
+ # roo はワークブックを展開するためメモリを食う。大きすぎるファイルは表にしない
13
+ # (プレビューは ZIP のエントリ一覧にフォールバックする)
14
+ MAX_FILE_SIZE = 10 * 1024 * 1024
15
+
16
+ class << self
17
+ def supports?(path)
18
+ EXTENSIONS.include?(File.extname(path.to_s).downcase)
19
+ end
20
+
21
+ def open(path, limits: TableSource::Limits.default, options: {})
22
+ size = File.size(path)
23
+ if size > MAX_FILE_SIZE
24
+ raise TableSource::ParseError, "file is larger than #{MAX_FILE_SIZE / (1024 * 1024)} MiB"
25
+ end
26
+
27
+ book = open_workbook(path)
28
+ header_enabled = options.fetch(:header, true)
29
+
30
+ sheets = book.sheets.map do |name|
31
+ LazyTableSheet.new(name: name) { load_sheet(book, name, limits, header_enabled) }
32
+ end
33
+ raise TableSource::ParseError, 'no sheets' if sheets.empty?
34
+
35
+ TableDocument.new(sheets: sheets, encoding: 'UTF-8', label: File.basename(path.to_s))
36
+ rescue TableSource::ParseError
37
+ raise
38
+ rescue StandardError => e
39
+ raise TableSource::ParseError, "#{e.class}: #{e.message}"
40
+ end
41
+
42
+ private
43
+
44
+ # roo は読み込みが重いので、使うときだけ require する
45
+ def open_workbook(path)
46
+ require 'roo'
47
+
48
+ case File.extname(path.to_s).downcase
49
+ when '.ods' then Roo::OpenOffice.new(path.to_s)
50
+ else Roo::Excelx.new(path.to_s)
51
+ end
52
+ end
53
+
54
+ def load_sheet(book, name, limits, header_enabled)
55
+ rows = []
56
+ row_truncated = false
57
+ column_truncated = false
58
+
59
+ each_sheet_row(book, name) do |row|
60
+ if rows.length >= limits.max_rows
61
+ row_truncated = true
62
+ next false
63
+ end
64
+
65
+ cells = row.map { |cell| cell_text(cell) }
66
+ if cells.length > limits.max_columns
67
+ cells = cells[0, limits.max_columns]
68
+ column_truncated = true
69
+ end
70
+ rows << cells
71
+ true
72
+ end
73
+
74
+ header = header_enabled && !rows.empty? ? rows.shift : nil
75
+ { rows: rows, header: header, row_truncated: row_truncated, column_truncated: column_truncated }
76
+ end
77
+
78
+ # 行を 1 行ずつ渡す。ブロックが false を返したら読み取りを止める。
79
+ # .xlsx はストリーミング API があるが、.ods(Roo::OpenOffice)には無いので行番号で読む。
80
+ def each_sheet_row(book, name)
81
+ sheet = book.sheet(name)
82
+
83
+ if sheet.respond_to?(:each_row_streaming)
84
+ # pad_cells: 空セルを nil で埋める(埋めないと桁がずれる)
85
+ sheet.each_row_streaming(pad_cells: true) { |row| break unless yield(Array(row)) }
86
+ else
87
+ first = sheet.first_row
88
+ last = sheet.last_row
89
+ return if first.nil? || last.nil?
90
+
91
+ (first..last).each { |number| break unless yield(Array(sheet.row(number))) }
92
+ end
93
+ end
94
+
95
+ # 書式は再現せず、値をそのまま文字列にする(整数は .0 を付けない)
96
+ def cell_text(cell)
97
+ value = cell.respond_to?(:value) ? cell.value : cell
98
+ case value
99
+ when nil then ''
100
+ when Float then value == value.to_i ? value.to_i.to_s : value.to_s
101
+ when Time then value.strftime('%H:%M:%S')
102
+ else
103
+ if defined?(Date) && value.is_a?(Date)
104
+ value.strftime('%Y-%m-%d')
105
+ else
106
+ value.to_s
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end