rufio 1.5.1 → 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 (92) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +86 -0
  3. data/README.md +18 -6
  4. data/README_ja.md +18 -6
  5. data/Rakefile +5 -0
  6. data/SECURITY.md +16 -8
  7. data/docs/csv_cell_preview_design_ja.md +682 -0
  8. data/docs/guide.md +115 -20
  9. data/docs/guide_ja.md +116 -21
  10. data/docs/image_preview_design_ja.md +335 -0
  11. data/docs/native_fuzzy_search_design_ja.md +684 -0
  12. data/examples/config.rb +23 -0
  13. data/info/help.md +32 -18
  14. data/info/keybindings.md +43 -11
  15. data/info/welcome.md +4 -6
  16. data/lib/rufio/background_status.rb +7 -0
  17. data/lib/rufio/bookmark_controller.rb +0 -45
  18. data/lib/rufio/clipboard.rb +162 -0
  19. data/lib/rufio/command_locator.rb +49 -0
  20. data/lib/rufio/config.rb +7 -4
  21. data/lib/rufio/config_loader.rb +66 -11
  22. data/lib/rufio/csv_table_source.rb +125 -0
  23. data/lib/rufio/encoding_detector.rb +152 -0
  24. data/lib/rufio/file_operation_controller.rb +41 -0
  25. data/lib/rufio/file_preview.rb +92 -19
  26. data/lib/rufio/fuzzy_candidate_block.rb +131 -0
  27. data/lib/rufio/fuzzy_file_source.rb +180 -0
  28. data/lib/rufio/fuzzy_search.rb +327 -0
  29. data/lib/rufio/fuzzy_search_session.rb +426 -0
  30. data/lib/rufio/graphics_layer.rb +223 -0
  31. data/lib/rufio/health_checker.rb +27 -32
  32. data/lib/rufio/help_menu.rb +16 -10
  33. data/lib/rufio/image_preview_view.rb +421 -0
  34. data/lib/rufio/image_source.rb +158 -0
  35. data/lib/rufio/keybind_handler.rb +21 -34
  36. data/lib/rufio/kitty_graphics.rb +128 -0
  37. data/lib/rufio/native/aarch64-linux-gnu/librufio_zig.so +0 -0
  38. data/lib/rufio/native/aarch64-linux-musl/librufio_zig.so +0 -0
  39. data/lib/rufio/native/aarch64-macos/librufio_zig.dylib +0 -0
  40. data/lib/rufio/native/aarch64-windows/rufio_zig.dll +0 -0
  41. data/lib/rufio/native/x86_64-linux-gnu/librufio_zig.so +0 -0
  42. data/lib/rufio/native/x86_64-linux-musl/librufio_zig.so +0 -0
  43. data/lib/rufio/native/x86_64-macos/librufio_zig.dylib +0 -0
  44. data/lib/rufio/native/x86_64-windows/rufio_zig.dll +0 -0
  45. data/lib/rufio/native_content_search.rb +44 -8
  46. data/lib/rufio/native_image.rb +177 -0
  47. data/lib/rufio/native_scanner_zig.rb +39 -0
  48. data/lib/rufio/navigation_controller.rb +47 -16
  49. data/lib/rufio/preview_registry.rb +86 -0
  50. data/lib/rufio/preview_view.rb +363 -0
  51. data/lib/rufio/renderer.rb +8 -8
  52. data/lib/rufio/screen.rb +38 -1
  53. data/lib/rufio/search_prompt.rb +50 -0
  54. data/lib/rufio/search_screen.rb +680 -0
  55. data/lib/rufio/sheet_picker.rb +87 -0
  56. data/lib/rufio/sqlite_table_source.rb +205 -0
  57. data/lib/rufio/tab_mode_manager.rb +1 -3
  58. data/lib/rufio/table_cursor.rb +117 -0
  59. data/lib/rufio/table_document.rb +261 -0
  60. data/lib/rufio/table_layout.rb +231 -0
  61. data/lib/rufio/table_preview_view.rb +376 -0
  62. data/lib/rufio/table_query.rb +240 -0
  63. data/lib/rufio/table_source.rb +46 -0
  64. data/lib/rufio/terminal_graphics.rb +168 -0
  65. data/lib/rufio/terminal_ui.rb +74 -60
  66. data/lib/rufio/text_utils.rb +27 -0
  67. data/lib/rufio/ui_renderer.rb +380 -17
  68. data/lib/rufio/version.rb +1 -1
  69. data/lib/rufio/xlsx_table_source.rb +112 -0
  70. data/lib/rufio.rb +21 -1
  71. data/lib_zig/rufio_native/Makefile +21 -5
  72. data/lib_zig/rufio_native/build.zig +18 -0
  73. data/lib_zig/rufio_native/src/fuzzy.zig +520 -0
  74. data/lib_zig/rufio_native/src/fuzzy_test.zig +212 -0
  75. data/lib_zig/rufio_native/src/image.zig +525 -0
  76. data/lib_zig/rufio_native/src/image_bmp.zig +111 -0
  77. data/lib_zig/rufio_native/src/image_jpeg.zig +878 -0
  78. data/lib_zig/rufio_native/src/image_png.zig +561 -0
  79. data/lib_zig/rufio_native/src/image_resize.zig +170 -0
  80. data/lib_zig/rufio_native/src/image_test.zig +401 -0
  81. data/lib_zig/rufio_native/src/main.zig +28 -0
  82. data/lib_zig/rufio_native/src/search.zig +260 -64
  83. data/lib_zig/rufio_native/src/search_test.zig +35 -0
  84. data/lib_zig/rufio_native/src/testdata/alpha.png +0 -0
  85. data/lib_zig/rufio_native/src/testdata/gradient.bmp +0 -0
  86. data/lib_zig/rufio_native/src/testdata/gradient.jpg +0 -0
  87. data/lib_zig/rufio_native/src/testdata/gradient.png +0 -0
  88. data/retag.sh +4 -4
  89. metadata +44 -5
  90. data/lib/rufio/global_search.rb +0 -412
  91. data/lib/rufio/search_controller.rb +0 -92
  92. data/lib_zig/rufio_native/src/main.zig.sync +0 -205
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rufio
4
+ # 画像ファイルの形式と寸法を、見出し(先頭の数十バイト)だけから読む。
5
+ #
6
+ # 判定は拡張子ではなくシグネチャで行う(`.png` という名前の別形式を端末へ送らない)。
7
+ # 画素は読まないので Ruby だけで完結し、デコードが要る処理は Zig 側(Phase 3)に回す。
8
+ class ImageSource
9
+ # JPEG はマーカーを辿るため、見出しとして読む上限を決めておく
10
+ HEADER_BYTES = 64 * 1024
11
+
12
+ SIGNATURES = {
13
+ png: "\x89PNG\r\n\x1a\n".b,
14
+ jpeg: "\xff\xd8".b,
15
+ gif: 'GIF8'.b,
16
+ bmp: 'BM'.b
17
+ }.freeze
18
+
19
+ # 寸法を持たない JPEG のフレーム以外のマーカー(長さを読んで飛ばす)
20
+ JPEG_SOF_MARKERS = [0xc0, 0xc1, 0xc2, 0xc3, 0xc5, 0xc6, 0xc7,
21
+ 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf].freeze
22
+
23
+ Info = Struct.new(:path, :kind, :width, :height, :bytes, keyword_init: true) do
24
+ # 画面に出す形式名("PNG" / "JPEG" ...)
25
+ def label
26
+ kind.to_s.upcase
27
+ end
28
+
29
+ def dimensions?
30
+ width.to_i.positive? && height.to_i.positive?
31
+ end
32
+
33
+ def dimensions
34
+ dimensions? ? "#{width}x#{height}" : '?'
35
+ end
36
+ end
37
+
38
+ class << self
39
+ # @return [Boolean] 画像として扱える形式か
40
+ def supports?(path)
41
+ !kind(path).nil?
42
+ end
43
+
44
+ # @return [Symbol, nil] :png / :jpeg / :gif / :bmp(判定できなければ nil)
45
+ def kind(path)
46
+ header = read_header(path, 8)
47
+ return nil if header.nil? || header.empty?
48
+
49
+ SIGNATURES.each { |name, signature| return name if header.start_with?(signature) }
50
+ nil
51
+ end
52
+
53
+ # @return [Info, nil] 形式が分かれば Info(寸法が読めない場合も Info を返す)
54
+ def read(path)
55
+ header = read_header(path, HEADER_BYTES)
56
+ return nil if header.nil? || header.empty?
57
+
58
+ name, = SIGNATURES.find { |_, signature| header.start_with?(signature) }
59
+ return nil unless name
60
+
61
+ width, height = dimensions(name, header)
62
+ Info.new(path: path, kind: name, width: width, height: height, bytes: file_size(path))
63
+ end
64
+
65
+ # 端末がそのままデコードできる形式か(Kitty は PNG を自前で読める)
66
+ def direct?(info)
67
+ return false unless info
68
+
69
+ info.kind == :png
70
+ end
71
+
72
+ private
73
+
74
+ def read_header(path, length)
75
+ return nil unless File.file?(path)
76
+
77
+ File.binread(path, length)
78
+ rescue SystemCallError, IOError
79
+ nil
80
+ end
81
+
82
+ def file_size(path)
83
+ File.size(path)
84
+ rescue SystemCallError
85
+ 0
86
+ end
87
+
88
+ def dimensions(kind, header)
89
+ case kind
90
+ when :png then png_dimensions(header)
91
+ when :jpeg then jpeg_dimensions(header)
92
+ when :gif then gif_dimensions(header)
93
+ when :bmp then bmp_dimensions(header)
94
+ end || [0, 0]
95
+ end
96
+
97
+ # IHDR は必ず先頭チャンク。幅・高さはビッグエンディアンの 32bit
98
+ def png_dimensions(header)
99
+ return nil unless header.bytesize >= 24 && header.byteslice(12, 4) == 'IHDR'
100
+
101
+ header.byteslice(16, 8).unpack('N2')
102
+ end
103
+
104
+ # 論理画面記述子(6 バイト目から)はリトルエンディアンの 16bit
105
+ def gif_dimensions(header)
106
+ return nil unless header.bytesize >= 10
107
+
108
+ header.byteslice(6, 4).unpack('v2')
109
+ end
110
+
111
+ # DIB ヘッダの幅・高さはリトルエンディアンの符号付き 32bit
112
+ # (高さが負なら上から下へ並ぶ画像。寸法としては絶対値を使う)
113
+ def bmp_dimensions(header)
114
+ return nil unless header.bytesize >= 26
115
+
116
+ width, height = header.byteslice(18, 8).unpack('l<2')
117
+ [width.abs, height.abs]
118
+ end
119
+
120
+ # SOI の後ろからマーカーを辿り、最初のフレーム(SOFn)の寸法を読む
121
+ def jpeg_dimensions(header)
122
+ offset = 2
123
+ size = header.bytesize
124
+ while offset + 4 <= size
125
+ # マーカーは 0xff で始まる。0xff の連続は詰め物なので読み飛ばす
126
+ unless header.getbyte(offset) == 0xff
127
+ offset += 1
128
+ next
129
+ end
130
+
131
+ marker = header.getbyte(offset + 1)
132
+ break if marker.nil?
133
+ if marker == 0xff
134
+ offset += 1
135
+ next
136
+ end
137
+
138
+ # 長さを持たないマーカー(RSTn / SOI / EOI / TEM)
139
+ if (0xd0..0xd9).cover?(marker) || marker == 0x01
140
+ offset += 2
141
+ next
142
+ end
143
+
144
+ length = header.byteslice(offset + 2, 2)&.unpack1('n')
145
+ break if length.nil? || length < 2
146
+
147
+ if JPEG_SOF_MARKERS.include?(marker) && offset + 9 <= size
148
+ height, width = header.byteslice(offset + 5, 4).unpack('n2')
149
+ return [width, height]
150
+ end
151
+
152
+ offset += 2 + length
153
+ end
154
+ nil
155
+ end
156
+ end
157
+ end
158
+ end
@@ -8,7 +8,6 @@ require_relative 'filter_manager'
8
8
  require_relative 'navigation_controller'
9
9
  require_relative 'file_operation_controller'
10
10
  require_relative 'bookmark_controller'
11
- require_relative 'search_controller'
12
11
  require_relative 'selection_manager'
13
12
  require_relative 'file_operations'
14
13
  require_relative 'bookmark_manager'
@@ -30,12 +29,16 @@ module Rufio
30
29
  @nav_controller.preview_focused?
31
30
  end
32
31
 
33
- def global_search_mode?
34
- @terminal_ui&.global_search_active? || false
32
+ def search_mode?
33
+ @terminal_ui&.search_active? || false
35
34
  end
36
35
 
37
- def start_global_search
38
- @terminal_ui&.start_global_search(@bookmark_manager.list)
36
+ # `/`: 現在のディレクトリ配下を検索する画面を開く(対象はここだけ)
37
+ def start_search
38
+ return false unless @terminal_ui
39
+
40
+ path = @directory_listing&.current_path || Dir.pwd
41
+ @terminal_ui.start_search([{ name: File.basename(path), path: path }])
39
42
  end
40
43
 
41
44
  def config_directory
@@ -69,7 +72,6 @@ module Rufio
69
72
  when :logs then enter_log_viewer_mode
70
73
  when :jobs then enter_job_mode
71
74
  when :config then enter_config_mode
72
- when :global then start_global_search
73
75
  when :help then show_help_dialog
74
76
  else false
75
77
  end
@@ -83,6 +85,16 @@ module Rufio
83
85
  @nav_controller.preview_scroll_offset
84
86
  end
85
87
 
88
+ # プレビューモード中のビュー(テキスト / 表)
89
+ def preview_view
90
+ @nav_controller.preview_view
91
+ end
92
+
93
+ # 表プレビューのセルカーソル(表でなければ nil)
94
+ def preview_cursor
95
+ @nav_controller.preview_cursor
96
+ end
97
+
86
98
  def filter_query
87
99
  @filter_manager.filter_query
88
100
  end
@@ -137,8 +149,6 @@ module Rufio
137
149
  nil, @dialog_renderer, @selection_manager, @job_manager, @notification_manager
138
150
  )
139
151
 
140
- # SearchController(fzf・rga検索を担当)
141
- @search_controller = SearchController.new(nil, @file_opener)
142
152
 
143
153
  # Help mode
144
154
  @in_help_mode = false
@@ -158,7 +168,6 @@ module Rufio
158
168
  @nav_controller.set_directory_listing(directory_listing)
159
169
  @file_op_controller.set_directory_listing(directory_listing)
160
170
  @bookmark_controller.set_directory_listing(directory_listing)
161
- @search_controller.set_directory_listing(directory_listing)
162
171
  @archive_controller.set_directory_listing(directory_listing)
163
172
  end
164
173
 
@@ -195,10 +204,6 @@ module Rufio
195
204
  return show_help_dialog
196
205
  end
197
206
 
198
- if @key_map[key] == :global_search && !@filter_manager.filter_mode
199
- return start_global_search
200
- end
201
-
202
207
  if @key_map[key] == :config_mode && !@filter_manager.filter_mode
203
208
  return enter_config_mode
204
209
  end
@@ -421,17 +426,7 @@ module Rufio
421
426
  @nav_controller.reset_preview_scroll
422
427
  end
423
428
 
424
- # Tabキー: 次のブックマークへ循環移動
425
- def goto_next_bookmark
426
- @bookmark_controller.goto_next_bookmark
427
- end
428
-
429
- # Shift+Tabキー: 前のブックマークへ循環移動
430
- def goto_prev_bookmark
431
- @bookmark_controller.goto_prev_bookmark
432
- end
433
-
434
- # UIRenderer 用:Tab移動と同じブックマークリストを返す
429
+ # UIRenderer 用:ブックマークリストを返す
435
430
  def bookmark_list
436
431
  @bookmark_manager.list
437
432
  end
@@ -485,6 +480,7 @@ module Rufio
485
480
  when :create_dir then @file_op_controller.create_directory
486
481
  when :move_selected then @file_op_controller.move_selected_to_current
487
482
  when :copy_selected then @file_op_controller.copy_selected_to_current
483
+ when :copy_to_clipboard then @file_op_controller.copy_paths_to_clipboard(current_entry)
488
484
  when :delete_selected then @file_op_controller.delete_selected_files
489
485
  when :select then toggle_selection
490
486
  when :filter
@@ -493,8 +489,7 @@ module Rufio
493
489
  else
494
490
  @nav_controller.start_filter_mode
495
491
  end
496
- when :fzf_search, :fzf_search_alt then fzf_search
497
- when :rga_search then rga_search
492
+ when :search then start_search
498
493
  when :add_bookmark then add_bookmark
499
494
  when :bookmark_menu then show_bookmark_menu
500
495
  when :zoxide then archive_or_zoxide
@@ -522,14 +517,6 @@ module Rufio
522
517
  :quit_cd
523
518
  end
524
519
 
525
- def fzf_search
526
- @search_controller.fzf_search
527
- end
528
-
529
- def rga_search
530
- @search_controller.rga_search
531
- end
532
-
533
520
  def start_filter_mode
534
521
  @nav_controller.start_filter_mode
535
522
  end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rufio
4
+ # Kitty グラフィックスプロトコルのシーケンス組み立て。
5
+ #
6
+ # ここは文字列を作るだけで IO を持たない(書き出すのは GraphicsLayer)。
7
+ # 仕様: https://sw.kovidgoyal.net/kitty/graphics-protocol/
8
+ module KittyGraphics
9
+ APC = "\e_G"
10
+ ST = "\e\\"
11
+ # 1 チャンクのペイロード上限(プロトコルの定め)
12
+ CHUNK = 4096
13
+
14
+ # f= の値。PNG は端末側がデコードできるので、ファイルをそのまま送れる
15
+ FORMAT_PNG = 100
16
+ FORMAT_RGB = 24
17
+ FORMAT_RGBA = 32
18
+ # Base64 ペイロードに許す文字だけを通し、APC 終了や任意のエスケープ注入を防ぐ
19
+ BASE64_PATTERN = /\A[A-Za-z0-9+\/=]*\z/.freeze
20
+
21
+ class << self
22
+ # 画像を転送して表示する(左上は送信時のカーソル位置)
23
+ #
24
+ # @param id [Integer] 画像 ID(削除・置き換えの単位)
25
+ # @param payload [String] 画像のバイト列(Base64 化はここで行う)
26
+ # @param cols [Integer] 収めるセル数(横)
27
+ # @param rows [Integer] 収めるセル数(縦)
28
+ # @param format [Integer] FORMAT_PNG / FORMAT_RGB / FORMAT_RGBA
29
+ # @param pixels [Array(Integer, Integer), nil] 生ピクセルのときの [幅, 高さ]
30
+ # @param passthrough [Boolean] tmux のパススルーで包むか
31
+ # @param crop [Array(Integer, Integer, Integer, Integer), nil] 元画像から切り出す
32
+ # 領域 [x, y, 幅, 高さ](ズーム)
33
+ # @param encoded [Boolean] payload が既に Base64 済みなら true
34
+ def transmit(id:, payload:, cols: nil, rows: nil, format: FORMAT_PNG, placement: 1,
35
+ pixels: nil, crop: nil, chunk: CHUNK, passthrough: false, encoded: false)
36
+ data = encoded ? payload.to_s : [payload.to_s].pack('m0')
37
+ return '' if encoded && !BASE64_PATTERN.match?(data)
38
+
39
+ pieces = split(data, chunk)
40
+ control = control_keys(id: id, cols: cols, rows: rows, format: format,
41
+ placement: placement, pixels: pixels, crop: crop)
42
+
43
+ pieces.each_with_index.map do |piece, index|
44
+ keys = if index.zero?
45
+ pieces.length > 1 ? "#{control},m=1" : control
46
+ else
47
+ index == pieces.length - 1 ? 'm=0' : 'm=1'
48
+ end
49
+ sequence("#{keys};#{piece}", passthrough: passthrough)
50
+ end.join
51
+ end
52
+
53
+ # カーソルを矩形の左上(1 始まり)へ動かしてから送り、カーソルを戻す。
54
+ # C=1 を付けているのでカーソルは動かないが、位置の起点をはっきりさせる
55
+ def place_at(row:, col:, passthrough: false, **options)
56
+ body = transmit(passthrough: passthrough, **options)
57
+ return '' if body.empty?
58
+
59
+ move = "\e[#{row.to_i};#{col.to_i}H"
60
+ "#{move}#{body}\e[H"
61
+ end
62
+
63
+ # 既に送った画像を、送り直さずに今のカーソル位置へ置く
64
+ def place(id:, cols:, rows:, placement: 1, passthrough: false)
65
+ keys = ['a=p', "i=#{id.to_i}", "p=#{placement.to_i}", "c=#{cols.to_i}", "r=#{rows.to_i}", 'C=1', 'q=2']
66
+ sequence(keys.join(','), passthrough: passthrough)
67
+ end
68
+
69
+ def delete(id:, passthrough: false)
70
+ sequence("a=d,d=i,i=#{id.to_i},q=2", passthrough: passthrough)
71
+ end
72
+
73
+ def delete_all(passthrough: false)
74
+ sequence('a=d,d=A,q=2', passthrough: passthrough)
75
+ end
76
+
77
+ # 対応判定用の問い合わせ(1x1 の RGB を送って応答だけを見る)
78
+ def query(id:, passthrough: false)
79
+ sequence("i=#{id.to_i},s=1,v=1,a=q,t=d,f=24;AAAA", passthrough: passthrough)
80
+ end
81
+
82
+ # 応答が「この問い合わせへの OK」かどうか
83
+ def ok?(response, id:)
84
+ response.to_s.include?("_Gi=#{id};OK")
85
+ end
86
+
87
+ private
88
+
89
+ def control_keys(id:, cols:, rows:, format:, placement:, pixels:, crop: nil)
90
+ keys = ['a=T', "f=#{format.to_i}", 't=d', "i=#{id.to_i}", "p=#{placement.to_i}"]
91
+ # c / r を両方省略すると端末は画像の元ピクセル(元アスペクト比)のまま描画する
92
+ keys << "c=#{cols.to_i}" if cols && cols.to_i.positive?
93
+ keys << "r=#{rows.to_i}" if rows && rows.to_i.positive?
94
+ keys.concat(['C=1', 'q=2'])
95
+ if pixels
96
+ keys << "s=#{pixels[0].to_i}"
97
+ keys << "v=#{pixels[1].to_i}"
98
+ end
99
+ if crop
100
+ # 元画像の一部だけを同じセル矩形に出す(ズーム)
101
+ keys << "x=#{crop[0].to_i}"
102
+ keys << "y=#{crop[1].to_i}"
103
+ keys << "w=#{crop[2].to_i}"
104
+ keys << "h=#{crop[3].to_i}"
105
+ end
106
+ keys.join(',')
107
+ end
108
+
109
+ def split(encoded, chunk)
110
+ size = [chunk.to_i, 1].max
111
+ return [''] if encoded.empty?
112
+
113
+ (0...encoded.bytesize).step(size).map { |offset| encoded.byteslice(offset, size) }
114
+ rescue RangeError
115
+ ['']
116
+ end
117
+
118
+ # tmux の中では ESC を二重にして DCS パススルーで包む
119
+ # (tmux 3.3+ かつ allow-passthrough on が必要)
120
+ def sequence(body, passthrough:)
121
+ raw = "#{APC}#{body}#{ST}"
122
+ return raw unless passthrough
123
+
124
+ "\ePtmux;#{raw.gsub("\e", "\e\e")}#{ST}"
125
+ end
126
+ end
127
+ end
128
+ end
@@ -6,12 +6,12 @@ require_relative 'native_scanner_zig'
6
6
 
7
7
  module Rufio
8
8
  # Zig 実装のコンテンツ検索(G mode 用)。
9
- # Ruby 実装(GlobalSearch#scan_content)と同じ結果を、並列走査と SIMD 検索で返す。
9
+ # Ruby 実装(SearchScreen#scan_content)と同じ結果を、並列走査と SIMD 検索で返す。
10
10
  class NativeContentSearch
11
11
  HEADER_SIZE = 25
12
12
  HEADER_FORMAT = 'CL<Q<L<L<L<'
13
13
  STATE_SCANNING = 1
14
- KINDS = { 1 => :match, 2 => :document, 3 => :error }.freeze
14
+ KINDS = { 1 => :match, 2 => :document, 3 => :error, 4 => :labels }.freeze
15
15
  MAX_QUERY_BYTES = 1024
16
16
  POLL_INTERVAL = 0.001
17
17
 
@@ -36,6 +36,14 @@ module Rufio
36
36
  @handle.zero? ? 0 : NativeScannerZigFFI.core_cache_bytes(@handle)
37
37
  end
38
38
 
39
+ # 検索の完了後、キャッシュになかったファイルは別スレッドで格納する(初回の検索を遅くしないため)。
40
+ # その格納が終わるまで待つ。終わるまでの検索はキャッシュを使わずに行われる(結果は同じ)
41
+ def wait_for_fill
42
+ return if @handle.zero? || !NativeScannerZigFFI.respond_to?(:core_cache_wait)
43
+
44
+ NativeScannerZigFFI.core_cache_wait(@handle)
45
+ end
46
+
39
47
  # このキャッシュを使う検索をすべて close してから呼ぶこと
40
48
  def close
41
49
  return if @handle.zero?
@@ -54,6 +62,11 @@ module Rufio
54
62
  NativeScannerZigFFI.files_available?
55
63
  end
56
64
 
65
+ # labels: true で使える(ファイルを相対パスのラベル列でまとめて返す)か
66
+ def self.labels_available?
67
+ NativeScannerZigFFI.labels_available?
68
+ end
69
+
57
70
  # Zig 側は ASCII のみ大文字小文字を畳み込むため、大文字小文字を持つ非 ASCII 文字は Ruby 実装に任せる
58
71
  def self.supports_query?(query)
59
72
  return false if query.empty? || query.bytesize > MAX_QUERY_BYTES
@@ -68,8 +81,13 @@ module Rufio
68
81
  line[[match_index - 30, 0].max, 200].to_s.strip
69
82
  end
70
83
 
71
- def initialize(roots:, query: nil, excluded_extensions: [], document_extensions: [], cache: nil, threads: 0)
84
+ # labels: true(ファイル一覧のみ)は、ファイルを 1 件ずつのイベントではなく
85
+ # [:labels, root_index, ラベル列, 件数] で返す。ラベル列は「ルートからの相対パス NUL」の連結(UTF-8 確認済み)。
86
+ # UTF-8 でない名前だけ従来どおり [:file, root_index, path] で返す。
87
+ def initialize(roots:, query: nil, excluded_extensions: [], document_extensions: [], cache: nil, threads: 0, labels: false)
72
88
  @files_only = query.nil?
89
+ raise ArgumentError, 'labels requires a file listing' if labels && !@files_only
90
+ raise StandardError, 'Zig file labels are not available' if labels && !self.class.labels_available?
73
91
  if @files_only
74
92
  raise StandardError, 'Zig file search is not available' unless self.class.files_available?
75
93
  else
@@ -80,7 +98,13 @@ module Rufio
80
98
  @path_encoding = Encoding.find('filesystem')
81
99
  roots_buf = pack(roots)
82
100
  excluded_buf = pack(excluded_extensions)
83
- @handle = if @files_only
101
+ @handle = if labels
102
+ NativeScannerZigFFI.core_files_labels_create(
103
+ roots_buf, roots_buf.bytesize,
104
+ excluded_buf, excluded_buf.bytesize,
105
+ threads
106
+ )
107
+ elsif @files_only
84
108
  NativeScannerZigFFI.core_files_create(
85
109
  roots_buf, roots_buf.bytesize,
86
110
  excluded_buf, excluded_buf.bytesize,
@@ -109,15 +133,26 @@ module Rufio
109
133
  each_batch { |batch| batch.each(&block) }
110
134
  end
111
135
 
112
- # 1 回の取り出しごとにイベント配列を返す(UI へまとめて渡すため)
113
- def each_batch
114
- return enum_for(:each_batch) unless block_given?
136
+ # 1 回の取り出しごとにイベント配列を返す(UI へまとめて渡すため)。
137
+ # max_events を指定すると、取り出した分が多いときはその件数ごとに分けて返す。
138
+ # 大きな取り出しを解析し終えるまで呼び出し側(と他のスレッド)を待たせない。
139
+ def each_batch(cancelled: nil, max_events: nil)
140
+ return enum_for(:each_batch, cancelled: cancelled, max_events: max_events) unless block_given?
115
141
 
116
142
  loop do
143
+ break if cancelled && cancelled.call
144
+
117
145
  state = NativeScannerZigFFI.core_search_state(@handle)
118
146
  data = drain
119
147
  batch = []
120
- each_record(data) { |event| batch << event }
148
+ each_record(data) do |event|
149
+ batch << event
150
+ next unless max_events && batch.length >= max_events
151
+
152
+ yield batch
153
+ batch = []
154
+ break if cancelled && cancelled.call
155
+ end
121
156
  skipped = NativeScannerZigFFI.core_search_skipped(@handle)
122
157
  if skipped > @skipped
123
158
  batch << [:skipped, skipped - @skipped]
@@ -166,6 +201,7 @@ module Rufio
166
201
  else
167
202
  yield [:match, root, path, line, self.class.snippet(extra, offset)]
168
203
  end
204
+ when :labels then yield [:labels, root, path.b, line]
169
205
  when :document then yield [:document, root, path]
170
206
  when :error then yield [:error, root, "#{path}: #{extra.force_encoding(Encoding::UTF_8).scrub}"]
171
207
  end