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
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rufio
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - masisz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-15 00:00:00.000000000 Z
11
+ date: 2026-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: io-console
@@ -237,9 +237,12 @@ files:
237
237
  - docs/CHANGELOG_v0.80.0.md
238
238
  - docs/CHANGELOG_v0.9.0.md
239
239
  - docs/architecture_concurrency_ja_v0.82.1.md
240
+ - docs/csv_cell_preview_design_ja.md
240
241
  - docs/fast_zip_design_ja.md
241
242
  - docs/guide.md
242
243
  - docs/guide_ja.md
244
+ - docs/image_preview_design_ja.md
245
+ - docs/native_fuzzy_search_design_ja.md
243
246
  - examples/bookmarks.yml
244
247
  - examples/config.rb
245
248
  - examples/script_paths.yml
@@ -259,34 +262,45 @@ files:
259
262
  - lib/rufio/bookmark_manager.rb
260
263
  - lib/rufio/bookmark_storage.rb
261
264
  - lib/rufio/builtin_commands.rb
265
+ - lib/rufio/clipboard.rb
262
266
  - lib/rufio/color_helper.rb
263
267
  - lib/rufio/command_completion.rb
264
268
  - lib/rufio/command_history.rb
269
+ - lib/rufio/command_locator.rb
265
270
  - lib/rufio/command_logger.rb
266
271
  - lib/rufio/command_mode.rb
267
272
  - lib/rufio/command_mode_ui.rb
268
273
  - lib/rufio/config.rb
269
274
  - lib/rufio/config_loader.rb
275
+ - lib/rufio/csv_table_source.rb
270
276
  - lib/rufio/dialog_renderer.rb
271
277
  - lib/rufio/dialog_support.rb
272
278
  - lib/rufio/directory_listing.rb
273
279
  - lib/rufio/document_text_extractor.rb
274
280
  - lib/rufio/dsl_command.rb
275
281
  - lib/rufio/dsl_command_loader.rb
282
+ - lib/rufio/encoding_detector.rb
276
283
  - lib/rufio/file_opener.rb
277
284
  - lib/rufio/file_operation_controller.rb
278
285
  - lib/rufio/file_operations.rb
279
286
  - lib/rufio/file_preview.rb
280
287
  - lib/rufio/filter_manager.rb
281
- - lib/rufio/global_search.rb
288
+ - lib/rufio/fuzzy_candidate_block.rb
289
+ - lib/rufio/fuzzy_file_source.rb
290
+ - lib/rufio/fuzzy_search.rb
291
+ - lib/rufio/fuzzy_search_session.rb
292
+ - lib/rufio/graphics_layer.rb
282
293
  - lib/rufio/health_checker.rb
283
294
  - lib/rufio/help_menu.rb
295
+ - lib/rufio/image_preview_view.rb
296
+ - lib/rufio/image_source.rb
284
297
  - lib/rufio/info_notice.rb
285
298
  - lib/rufio/interpreter_resolver.rb
286
299
  - lib/rufio/job_manager.rb
287
300
  - lib/rufio/job_mode.rb
288
301
  - lib/rufio/key_input.rb
289
302
  - lib/rufio/keybind_handler.rb
303
+ - lib/rufio/kitty_graphics.rb
290
304
  - lib/rufio/local_script_scanner.rb
291
305
  - lib/rufio/logger.rb
292
306
  - lib/rufio/multibyte_input_reader.rb
@@ -299,6 +313,7 @@ files:
299
313
  - lib/rufio/native/x86_64-macos/librufio_zig.dylib
300
314
  - lib/rufio/native/x86_64-windows/rufio_zig.dll
301
315
  - lib/rufio/native_content_search.rb
316
+ - lib/rufio/native_image.rb
302
317
  - lib/rufio/native_scanner.rb
303
318
  - lib/rufio/native_scanner_zig.rb
304
319
  - lib/rufio/native_zip.rb
@@ -306,6 +321,8 @@ files:
306
321
  - lib/rufio/notification_manager.rb
307
322
  - lib/rufio/parallel_scanner.rb
308
323
  - lib/rufio/path_jump.rb
324
+ - lib/rufio/preview_registry.rb
325
+ - lib/rufio/preview_view.rb
309
326
  - lib/rufio/rakefile_parser.rb
310
327
  - lib/rufio/renderer.rb
311
328
  - lib/rufio/screen.rb
@@ -314,17 +331,28 @@ files:
314
331
  - lib/rufio/script_file.rb
315
332
  - lib/rufio/script_path_manager.rb
316
333
  - lib/rufio/script_runner.rb
317
- - lib/rufio/search_controller.rb
334
+ - lib/rufio/search_prompt.rb
335
+ - lib/rufio/search_screen.rb
318
336
  - lib/rufio/selection_manager.rb
337
+ - lib/rufio/sheet_picker.rb
319
338
  - lib/rufio/shell_command_completion.rb
320
339
  - lib/rufio/shell_init.rb
340
+ - lib/rufio/sqlite_table_source.rb
321
341
  - lib/rufio/syntax_highlighter.rb
322
342
  - lib/rufio/tab_mode_manager.rb
343
+ - lib/rufio/table_cursor.rb
344
+ - lib/rufio/table_document.rb
345
+ - lib/rufio/table_layout.rb
346
+ - lib/rufio/table_preview_view.rb
347
+ - lib/rufio/table_query.rb
348
+ - lib/rufio/table_source.rb
323
349
  - lib/rufio/task_status.rb
350
+ - lib/rufio/terminal_graphics.rb
324
351
  - lib/rufio/terminal_ui.rb
325
352
  - lib/rufio/text_utils.rb
326
353
  - lib/rufio/ui_renderer.rb
327
354
  - lib/rufio/version.rb
355
+ - lib/rufio/xlsx_table_source.rb
328
356
  - lib/rufio/zip_archive.rb
329
357
  - lib/rufio/zip_extractor.rb
330
358
  - lib/rufio/zip_writer.rb
@@ -333,11 +361,22 @@ files:
333
361
  - lib_zig/rufio_native/bench.sh
334
362
  - lib_zig/rufio_native/build.zig
335
363
  - lib_zig/rufio_native/src/cli.zig
364
+ - lib_zig/rufio_native/src/fuzzy.zig
365
+ - lib_zig/rufio_native/src/fuzzy_test.zig
366
+ - lib_zig/rufio_native/src/image.zig
367
+ - lib_zig/rufio_native/src/image_bmp.zig
368
+ - lib_zig/rufio_native/src/image_jpeg.zig
369
+ - lib_zig/rufio_native/src/image_png.zig
370
+ - lib_zig/rufio_native/src/image_resize.zig
371
+ - lib_zig/rufio_native/src/image_test.zig
336
372
  - lib_zig/rufio_native/src/main.zig
337
- - lib_zig/rufio_native/src/main.zig.sync
338
373
  - lib_zig/rufio_native/src/os.zig
339
374
  - lib_zig/rufio_native/src/search.zig
340
375
  - lib_zig/rufio_native/src/search_test.zig
376
+ - lib_zig/rufio_native/src/testdata/alpha.png
377
+ - lib_zig/rufio_native/src/testdata/gradient.bmp
378
+ - lib_zig/rufio_native/src/testdata/gradient.jpg
379
+ - lib_zig/rufio_native/src/testdata/gradient.png
341
380
  - lib_zig/rufio_native/src/zip.zig
342
381
  - retag.sh
343
382
  homepage: https://github.com/masisz/rufio
@@ -1,412 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'thread'
4
- require_relative 'text_utils'
5
- require_relative 'config_loader'
6
- require_relative 'document_text_extractor'
7
- require_relative 'native_content_search'
8
-
9
- module Rufio
10
- # ブックマーク配下のファイル名・テキスト内容をバックグラウンドで横断検索する。
11
- class GlobalSearch
12
- SPINNER_FRAMES = ['|', '/', '-', '\\'].freeze
13
- SPINNER_INTERVAL = 0.12
14
- # 検索語に一致した部分の表示(黒の太字・黄色背景)
15
- MATCH_FG = "\e[30m\e[1m"
16
- MATCH_BG = "\e[43m"
17
-
18
- attr_reader :query, :results, :errors, :selected_index, :search_type, :skipped_files
19
-
20
- class << self
21
- # nil: 利用可能なら Zig 実装を使う(RUFIO_NATIVE_SEARCH=0 で無効化)
22
- attr_writer :native_search
23
-
24
- def native_search?
25
- enabled = @native_search.nil? ? ENV['RUFIO_NATIVE_SEARCH'] != '0' : @native_search
26
- enabled && NativeContentSearch.available?
27
- end
28
- end
29
-
30
- def initialize
31
- @active = false
32
- @scanning = false
33
- @worker = nil
34
- @document_extractor = DocumentTextExtractor.new
35
- @document_cache = DocumentTextCache.new(extractor: @document_extractor)
36
- end
37
-
38
- def active?
39
- @active
40
- end
41
-
42
- def scanning?
43
- @scanning
44
- end
45
-
46
- def open(bookmarks)
47
- close
48
- @active = true
49
- @query = ''
50
- @search_type = :filename
51
- @excluded_extensions = ConfigLoader.global_search_excluded_extensions
52
- @bookmark_count = bookmarks.length
53
- @roots = bookmarks.map { |bookmark| [bookmark[:name].to_s, bookmark[:path].to_s] }
54
- start_scan
55
- true
56
- end
57
-
58
- def close
59
- @active = false
60
- @scanning = false
61
- @worker&.kill
62
- @worker&.join
63
- @worker = nil
64
- end
65
-
66
- # 1フレーム当たりの処理量を制限し、検索中も入力を受け付ける。
67
- def poll
68
- return false unless active?
69
-
70
- changed = false
71
- 256.times do
72
- event = begin
73
- @events.pop(true)
74
- rescue ThreadError
75
- break
76
- end
77
- changed = true
78
- type, value = event
79
- case type
80
- when :entry
81
- @entries << value
82
- @results << value if @search_type == :content || matches_query?(value)
83
- when :entries
84
- # Zig 実装の結果はまとめて届くので、1 フレーム 256 件の上限に縛られない
85
- @entries.concat(value)
86
- @results.concat(@search_type == :content || @query.empty? ? value : value.select { |entry| matches_query?(entry) })
87
- when :error then @errors << value
88
- when :skipped then @skipped_files += value || 1
89
- when :done then @scanning = false
90
- end
91
- end
92
- if scanning?
93
- now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
94
- if now - @last_spinner_update >= SPINNER_INTERVAL
95
- @spinner_index = (@spinner_index + 1) % SPINNER_FRAMES.length
96
- @last_spinner_update = now
97
- changed = true
98
- end
99
- end
100
- changed
101
- end
102
-
103
- def handle_key(key)
104
- case key
105
- when "\e" then close
106
- when "\t"
107
- @search_type = @search_type == :filename ? :content : :filename
108
- start_scan
109
- when "\r" then return @results[@selected_index]
110
- when :down then @selected_index = [@selected_index + 1, [@results.length - 1, 0].max].min
111
- when :up then @selected_index = [@selected_index - 1, 0].max
112
- when "\x0a", "\x0e" then handle_key(:down) # Ctrl+J / Ctrl+N
113
- when "\x0b", "\x10" then handle_key(:up) # Ctrl+K / Ctrl+P
114
- when "\x15" then update_query('') # Ctrl+U
115
- when "\u007f", "\b" then update_query(@query[0...-1])
116
- else
117
- update_query(@query + key) if key.is_a?(String) && !key.empty? && key.each_codepoint.all? { |c| c >= 32 && c != 127 }
118
- end
119
- nil
120
- end
121
-
122
- def draw(screen)
123
- screen.clear
124
- width = screen.width
125
- type = @search_type == :content ? 'Content' : 'File names'
126
- write_line(screen, 0, "G mode - #{type} | Tab: Switch name/content search", width, "\e[1;36m")
127
- write_line(screen, 1, "Search: #{@query}_", width)
128
- state = if scanning?
129
- "[#{SPINNER_FRAMES[@spinner_index]}] Searching..."
130
- elsif @search_type == :content && @query.empty?
131
- 'Ready'
132
- else
133
- 'Search complete'
134
- end
135
- color = scanning? ? "\e[1;33m" : "\e[32m"
136
- write_line(screen, 2, "#{state} | #{@bookmark_count} bookmarks | #{@results.length} matches | #{@errors.length} errors | #{@skipped_files} skipped", width, color)
137
- row_height = @search_type == :content ? 2 : 1
138
- capacity = [screen.height - 6, 0].max / row_height
139
- first = [@selected_index - capacity + 1, 0].max
140
- @results.slice(first, capacity).to_a.each_with_index do |entry, index|
141
- selected = first + index == @selected_index
142
- prefix = "#{selected ? '>' : ' '} [#{entry[:bookmark]}] "
143
- text = "#{prefix}#{entry[:relative_path]}"
144
- text += if entry[:location]
145
- " - #{entry[:location]}"
146
- elsif entry[:line_number]
147
- ":#{entry[:line_number]}"
148
- else
149
- ''
150
- end
151
- y = index * row_height + 3
152
- # ファイル名検索はパス、本文検索はスニペットの一致箇所をハイライトする
153
- path_highlight = @search_type == :filename ? prefix.length : nil
154
- write_line(screen, y, text, width, selected ? "\e[1;36m" : nil, highlight_from: path_highlight)
155
- write_line(screen, y + 1, " #{entry[:match_text]}", width, highlight_from: 4) if entry[:match_text]
156
- end
157
- if @results.empty?
158
- message = if @bookmark_count.zero?
159
- 'No bookmarks. Add directories with b in Files mode.'
160
- elsif scanning?
161
- 'Searching bookmarked directories...'
162
- elsif @search_type == :content && @query.empty?
163
- 'Type a string to search inside text files.'
164
- elsif !@errors.empty? && @entries.empty?
165
- 'No readable files in bookmarked directories.'
166
- else
167
- 'No matching files. Edit your search to try again.'
168
- end
169
- write_line(screen, 3, message, width)
170
- end
171
- write_line(screen, screen.height - 3, @results[@selected_index]&.[](:path) || @errors.first.to_s, width, "\e[90m")
172
- write_line(screen, screen.height - 2, 'ESC: Back | Enter: Go to file | Up/Down or Ctrl+J/K: Select | Type: Search', width)
173
- end
174
-
175
- private
176
-
177
- def start_scan
178
- @worker&.kill
179
- @worker&.join
180
- @worker = nil
181
- @entries = []
182
- @results = []
183
- @errors = []
184
- @skipped_files = 0
185
- @selected_index = 0
186
- @events = SizedQueue.new(256)
187
- @spinner_index = 0
188
- @last_spinner_update = Process.clock_gettime(Process::CLOCK_MONOTONIC)
189
- @scanning = !(@search_type == :content && @query.empty?)
190
- return unless @scanning
191
-
192
- roots, events, type, query = @roots, @events, @search_type, @query.downcase
193
- native = self.class.native_search?
194
- native_cache = native && type == :content ? (@native_cache ||= NativeContentSearch::Cache.new) : nil
195
- @worker = Thread.new do
196
- begin
197
- if native && type == :content && NativeContentSearch.supports_query?(query)
198
- scan_roots_natively(roots, events, query, native_cache)
199
- elsif native && type == :filename && NativeContentSearch.files_available?
200
- scan_files_natively(roots, events)
201
- else
202
- scan_roots(roots, events, type, query)
203
- end
204
- rescue StandardError => e
205
- events << [:error, e.message]
206
- end
207
- events << [:done, nil]
208
- end
209
- end
210
-
211
- def update_query(query)
212
- @query = query
213
- @selected_index = 0
214
- @search_type == :content ? start_scan : filter_results
215
- end
216
-
217
- def filter_results
218
- query = @query.downcase
219
- @results = @entries.select { |entry| entry[:relative_path].scrub.downcase.include?(query) }
220
- @selected_index = [[@selected_index, @results.length - 1].min, 0].max
221
- end
222
-
223
- def matches_query?(entry)
224
- entry[:relative_path].scrub.downcase.include?(@query.downcase)
225
- end
226
-
227
- def scan_roots(roots, events, type, query)
228
- visited_dirs = {}
229
- excluded_names = @excluded_extensions.select { |e| e.start_with?('/') }.map { |e| e.delete_prefix('/') }
230
- excluded_suffixes = @excluded_extensions.reject { |e| e.start_with?('/') }
231
- roots.each do |name, root|
232
- begin
233
- root = File.expand_path(root)
234
- raise Errno::ENOTDIR, root unless File.directory?(root)
235
-
236
- pending = [root]
237
- until pending.empty?
238
- directory = pending.pop
239
- canonical = File.realpath(directory)
240
- next if visited_dirs[canonical]
241
-
242
- visited_dirs[canonical] = true
243
- begin
244
- Dir.each_child(directory) do |child|
245
- path = File.join(directory, child)
246
- folded_child = child.scrub.downcase
247
- # ディレクトリシンボリックリンクは追わず、循環・範囲外の走査を防ぐ。
248
- if File.directory?(path)
249
- pending << path unless File.symlink?(path) || excluded_names.include?(folded_child)
250
- elsif File.file?(path)
251
- if excluded_names.include?(folded_child) || (!excluded_suffixes.empty? && folded_child.end_with?(*excluded_suffixes))
252
- events << [:skipped, nil]
253
- next
254
- end
255
-
256
- entry = { path: path, bookmark: name, relative_path: path.delete_prefix(root + File::SEPARATOR) }
257
- if type == :content
258
- scan_content(entry, query, events)
259
- else
260
- events << [:entry, entry]
261
- end
262
- end
263
- end
264
- rescue SystemCallError, IOError => e
265
- events << [:error, "#{directory}: #{e.message}"]
266
- end
267
- end
268
- rescue SystemCallError, IOError => e
269
- events << [:error, "#{name}: #{e.message}"]
270
- end
271
- end
272
- end
273
-
274
- # Zig 実装でブックマーク配下のファイル一覧を並列走査する
275
- def scan_files_natively(roots, events)
276
- valid_roots = []
277
- roots.each do |name, root|
278
- root = File.expand_path(root)
279
- raise Errno::ENOTDIR, root unless File.directory?(root)
280
-
281
- valid_roots << [name, root]
282
- rescue SystemCallError, IOError => e
283
- events << [:error, "#{name}: #{e.message}"]
284
- end
285
- return if valid_roots.empty?
286
-
287
- engine = NativeContentSearch.new(roots: valid_roots.map(&:last), query: nil,
288
- excluded_extensions: @excluded_extensions)
289
- engine.each_batch do |batch|
290
- entries = []
291
- batch.each do |kind, *args|
292
- case kind
293
- when :file, :match
294
- name, root = valid_roots[args[0]]
295
- path = args[1]
296
- entries << { path: path, bookmark: name, relative_path: path.delete_prefix(root + File::SEPARATOR) }
297
- when :error then events << [:error, args[1]]
298
- when :skipped then events << [:skipped, args[0]]
299
- end
300
- end
301
- events << [:entries, entries] unless entries.empty?
302
- end
303
- ensure
304
- engine&.close
305
- end
306
-
307
- # Zig 実装で走査・検索し、PDF などのドキュメントだけ Ruby 側で抽出する
308
- def scan_roots_natively(roots, events, query, cache)
309
- valid_roots = []
310
- roots.each do |name, root|
311
- root = File.expand_path(root)
312
- raise Errno::ENOTDIR, root unless File.directory?(root)
313
-
314
- valid_roots << [name, root]
315
- rescue SystemCallError, IOError => e
316
- events << [:error, "#{name}: #{e.message}"]
317
- end
318
- return if valid_roots.empty?
319
-
320
- engine = NativeContentSearch.new(roots: valid_roots.map(&:last), query: query,
321
- excluded_extensions: @excluded_extensions,
322
- document_extensions: DocumentTextExtractor::EXTENSIONS, cache: cache)
323
- engine.each_batch do |batch|
324
- entries = []
325
- batch.each do |kind, *args|
326
- case kind
327
- when :match, :document
328
- name, root = valid_roots[args[0]]
329
- path = args[1]
330
- entry = { path: path, bookmark: name, relative_path: path.delete_prefix(root + File::SEPARATOR) }
331
- if kind == :document
332
- scan_content(entry, query, events)
333
- else
334
- entries << entry.merge(line_number: args[2], match_text: args[3])
335
- end
336
- when :error then events << [:error, args[1]]
337
- when :skipped then events << [:skipped, args[0]]
338
- end
339
- end
340
- events << [:entries, entries] unless entries.empty?
341
- end
342
- ensure
343
- engine&.close
344
- end
345
-
346
- def scan_content(entry, query, events)
347
- if @document_extractor.supported?(entry[:path])
348
- @document_cache.each_record(entry[:path]) do |record|
349
- match_index = record[:search_text].index(query)
350
- next unless match_index
351
-
352
- snippet = record[:text][[match_index - 30, 0].max, 200].to_s.strip
353
- metadata = record.reject { |key, _| key == :text || key == :search_text }
354
- events << [:entry, entry.merge(metadata).merge(match_text: snippet)]
355
- end
356
- return
357
- end
358
-
359
- File.open(entry[:path], 'rb') do |file|
360
- sample = file.read(8192).to_s
361
- if sample.include?("\0")
362
- events << [:skipped, nil]
363
- return
364
- end
365
-
366
- file.rewind
367
- file.each_line.with_index(1) do |raw_line, number|
368
- line = raw_line.force_encoding(Encoding::UTF_8).scrub
369
- match_index = line.downcase.index(query)
370
- next unless match_index
371
-
372
- snippet_start = [match_index - 30, 0].max
373
- snippet = line[snippet_start, 200].strip
374
- events << [:entry, entry.merge(line_number: number, match_text: snippet)]
375
- end
376
- end
377
- rescue SystemCallError, IOError, DocumentTextExtractor::ExtractionError => e
378
- events << [:error, "#{entry[:path]}: #{e.message}"]
379
- end
380
-
381
- def write_line(screen, y, text, width, color = nil, highlight_from: nil)
382
- safe_text = text.scrub.gsub(/[[:cntrl:]]/, ' ')
383
- visible = TextUtils.truncate_to_width(safe_text, width)
384
- screen.put_string(0, y, visible, fg: color)
385
- return unless highlight_from
386
-
387
- match_ranges(visible, highlight_from).each do |start, length|
388
- x = TextUtils.display_width(visible[0, start])
389
- screen.put_string(x, y, visible[start, length], fg: MATCH_FG, bg: MATCH_BG)
390
- end
391
- end
392
-
393
- # 検索と同じく大文字小文字を区別せず、from 文字目以降の一致位置を [開始, 長さ] で返す
394
- def match_ranges(text, from)
395
- needle = @query.to_s.downcase.chars
396
- return [] if needle.empty?
397
-
398
- folded = text.chars.map(&:downcase)
399
- ranges = []
400
- index = from
401
- while index + needle.length <= folded.length
402
- if folded[index, needle.length] == needle
403
- ranges << [index, needle.length]
404
- index += needle.length
405
- else
406
- index += 1
407
- end
408
- end
409
- ranges
410
- end
411
- end
412
- end
@@ -1,92 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'shellwords'
4
-
5
- module Rufio
6
- # 検索(fzf・rga)専用コントローラ
7
- # KeybindHandler から fzf/rga 検索系メソッドを分離し、単一責任原則に準拠
8
- class SearchController
9
- def initialize(directory_listing, file_opener)
10
- @directory_listing = directory_listing
11
- @file_opener = file_opener
12
- end
13
-
14
- def set_directory_listing(directory_listing)
15
- @directory_listing = directory_listing
16
- end
17
-
18
- # ============================
19
- # fzf 検索
20
- # ============================
21
-
22
- def fzf_search
23
- return false unless fzf_available?
24
-
25
- current_path = @directory_listing&.current_path || Dir.pwd
26
-
27
- selected_file = nil
28
- Dir.chdir(current_path) do
29
- selected_file = `find . -type f | fzf --preview 'cat {}'`.strip
30
- end
31
-
32
- if !selected_file.empty?
33
- full_path = File.expand_path(selected_file, current_path)
34
- @file_opener.open_file(full_path) if File.exist?(full_path)
35
- end
36
-
37
- :needs_refresh
38
- end
39
-
40
- def fzf_available?
41
- system('which fzf > /dev/null 2>&1')
42
- end
43
-
44
- # ============================
45
- # rga 検索
46
- # ============================
47
-
48
- def rga_search
49
- return false unless rga_available?
50
-
51
- current_path = @directory_listing&.current_path || Dir.pwd
52
-
53
- print ConfigLoader.message('keybind.search_text')
54
- search_query = STDIN.gets.chomp
55
- return false if search_query.empty?
56
-
57
- search_results = nil
58
- Dir.chdir(current_path) do
59
- escaped_query = Shellwords.escape(search_query)
60
- # "--" で検索語がオプション(--pre=コマンド 等)として解釈されるのを防ぐ
61
- search_results = `rga --line-number --with-filename -- #{escaped_query} . 2>/dev/null`
62
- end
63
-
64
- if search_results.empty?
65
- puts "\n#{ConfigLoader.message('keybind.no_matches')}"
66
- print ConfigLoader.message('keybind.press_any_key')
67
- KeyInput.getch
68
- return true
69
- end
70
-
71
- selected_result = IO.popen('fzf', 'r+') do |fzf|
72
- fzf.write(search_results)
73
- fzf.close_write
74
- fzf.read.strip
75
- end
76
-
77
- if !selected_result.empty? && selected_result.match(/^(.+?):(\d+):/)
78
- file_path = ::Regexp.last_match(1)
79
- line_number = ::Regexp.last_match(2).to_i
80
- full_path = File.expand_path(file_path, current_path)
81
-
82
- @file_opener.open_file_with_line(full_path, line_number) if File.exist?(full_path)
83
- end
84
-
85
- :needs_refresh
86
- end
87
-
88
- def rga_available?
89
- system('which rga > /dev/null 2>&1')
90
- end
91
- end
92
- end