rufio 1.5.1 → 1.6.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 (49) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +37 -0
  3. data/README.md +8 -3
  4. data/README_ja.md +7 -3
  5. data/Rakefile +5 -0
  6. data/SECURITY.md +2 -2
  7. data/docs/guide.md +41 -8
  8. data/docs/guide_ja.md +41 -8
  9. data/docs/native_fuzzy_search_design_ja.md +684 -0
  10. data/info/help.md +13 -4
  11. data/info/keybindings.md +5 -3
  12. data/info/welcome.md +2 -2
  13. data/lib/rufio/config.rb +0 -2
  14. data/lib/rufio/config_loader.rb +24 -4
  15. data/lib/rufio/fuzzy_candidate_block.rb +131 -0
  16. data/lib/rufio/fuzzy_candidate_cache.rb +258 -0
  17. data/lib/rufio/fuzzy_file_source.rb +180 -0
  18. data/lib/rufio/fuzzy_finder.rb +313 -0
  19. data/lib/rufio/fuzzy_preview_loader.rb +107 -0
  20. data/lib/rufio/fuzzy_search.rb +327 -0
  21. data/lib/rufio/fuzzy_search_session.rb +426 -0
  22. data/lib/rufio/global_search.rb +292 -51
  23. data/lib/rufio/health_checker.rb +0 -22
  24. data/lib/rufio/help_menu.rb +5 -4
  25. data/lib/rufio/keybind_handler.rb +6 -5
  26. data/lib/rufio/native/aarch64-linux-gnu/librufio_zig.so +0 -0
  27. data/lib/rufio/native/aarch64-linux-musl/librufio_zig.so +0 -0
  28. data/lib/rufio/native/aarch64-macos/librufio_zig.dylib +0 -0
  29. data/lib/rufio/native/aarch64-windows/rufio_zig.dll +0 -0
  30. data/lib/rufio/native/x86_64-linux-gnu/librufio_zig.so +0 -0
  31. data/lib/rufio/native/x86_64-linux-musl/librufio_zig.so +0 -0
  32. data/lib/rufio/native/x86_64-macos/librufio_zig.dylib +0 -0
  33. data/lib/rufio/native/x86_64-windows/rufio_zig.dll +0 -0
  34. data/lib/rufio/native_content_search.rb +43 -7
  35. data/lib/rufio/native_scanner_zig.rb +17 -0
  36. data/lib/rufio/rga_result_source.rb +89 -0
  37. data/lib/rufio/screen.rb +5 -1
  38. data/lib/rufio/search_controller.rb +56 -54
  39. data/lib/rufio/terminal_ui.rb +70 -8
  40. data/lib/rufio/ui_renderer.rb +1 -1
  41. data/lib/rufio/version.rb +1 -1
  42. data/lib/rufio.rb +6 -0
  43. data/lib_zig/rufio_native/Makefile +16 -2
  44. data/lib_zig/rufio_native/build.zig +8 -0
  45. data/lib_zig/rufio_native/src/fuzzy.zig +520 -0
  46. data/lib_zig/rufio_native/src/fuzzy_test.zig +212 -0
  47. data/lib_zig/rufio_native/src/main.zig +27 -0
  48. data/lib_zig/rufio_native/src/search.zig +221 -44
  49. metadata +13 -2
@@ -11,7 +11,7 @@ module Rufio
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
@@ -107,6 +107,12 @@ module Rufio
107
107
  @files_available == true
108
108
  end
109
109
 
110
+ # ファイル一覧を相対パスのラベル列で返す API(1.6.0 で追加)
111
+ def labels_available?
112
+ load! unless @loaded
113
+ @labels_available == true
114
+ end
115
+
110
116
  # ZIP データ処理 API(古いライブラリには存在しない)
111
117
  def zip_available?
112
118
  load! unless @loaded
@@ -130,6 +136,11 @@ module Rufio
130
136
  extern 'uint64_t core_cache_create(uint64_t)'
131
137
  extern 'uint64_t core_cache_bytes(uint64_t)'
132
138
  extern 'void core_cache_destroy(uint64_t)'
139
+ begin
140
+ extern 'void core_cache_wait(uint64_t)'
141
+ rescue Fiddle::DLError
142
+ nil # 1.6.0 より前: 検索中にキャッシュへ格納するので待つものがない
143
+ end
133
144
  @search_available = true
134
145
  begin
135
146
  extern 'uint64_t core_files_create(const char*, size_t, const char*, size_t, uint32_t)'
@@ -137,6 +148,12 @@ module Rufio
137
148
  rescue Fiddle::DLError
138
149
  @files_available = false
139
150
  end
151
+ begin
152
+ extern 'uint64_t core_files_labels_create(const char*, size_t, const char*, size_t, uint32_t)'
153
+ @labels_available = @files_available
154
+ rescue Fiddle::DLError
155
+ @labels_available = false # 1.6.0 開発版より前のライブラリ
156
+ end
140
157
  rescue Fiddle::DLError => e
141
158
  warn "Zig content search unavailable: #{e.message}" if ENV['RUFIO_DEBUG']
142
159
  @search_available = false
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rufio
4
+ # F の rga 出力(path:line:text)を FuzzySearchSession の候補として流す。
5
+ # rga プロセスの起動・停止はプロデューサースレッド内で完結させる。
6
+ class RgaResultSource
7
+ BATCH_SIZE = 256
8
+ POLL_INTERVAL = 0.05
9
+ LINE_PATTERN = /\A(.+?):(\d+):/.freeze
10
+
11
+ attr_reader :command
12
+
13
+ def initialize(query, root, command: nil)
14
+ @root = File.expand_path(root).freeze
15
+ # "--" で検索語がオプション(--pre=コマンド 等)として解釈されるのを防ぐ
16
+ @command = (command || ['rga', '--line-number', '--with-filename', '--no-heading',
17
+ '--color', 'never', '--', query, '.']).map { |part| part.to_s.dup.freeze }.freeze
18
+ end
19
+
20
+ def each_batch(cancelled:, on_error:)
21
+ return if cancelled.call
22
+
23
+ io = IO.popen(@command, 'rb', chdir: @root, err: File::NULL)
24
+ buffer = +''.b
25
+ batch = []
26
+ until cancelled.call
27
+ next unless IO.select([io], nil, nil, POLL_INTERVAL)
28
+
29
+ begin
30
+ buffer << io.readpartial(65_536)
31
+ rescue EOFError
32
+ break
33
+ end
34
+ while (newline = buffer.index("\n"))
35
+ line = buffer.slice!(0, newline + 1)
36
+ entry = parse(line)
37
+ batch << entry if entry
38
+ next if batch.length < BATCH_SIZE
39
+
40
+ yield batch
41
+ batch = []
42
+ end
43
+ unless batch.empty?
44
+ yield batch
45
+ batch = []
46
+ end
47
+ end
48
+ return if cancelled.call
49
+
50
+ entry = parse(buffer)
51
+ yield [entry] if entry
52
+ finished = true
53
+ rescue SystemCallError, IOError => e
54
+ on_error.call("#{@command.first}: #{e.message}")
55
+ ensure
56
+ stop(io, kill: !finished) if io
57
+ end
58
+
59
+ private
60
+
61
+ def parse(raw)
62
+ line = raw.chomp.delete_suffix("\r")
63
+ match = line.match(LINE_PATTERN)
64
+ return unless match
65
+
66
+ relative = match[1].sub(%r{\A\.[/\\]}, '')
67
+ rest = line.byteslice(match.end(0)..)
68
+ text = "#{relative}:#{match[2]}:#{rest}".force_encoding(Encoding::UTF_8).scrub
69
+ path = File.join(@root.b, relative).force_encoding(Encoding.default_external)
70
+ { text: text.freeze, path: path.freeze, line_number: match[2].to_i }.freeze
71
+ end
72
+
73
+ # 途中で止めたときは rga を終了させてから回収する
74
+ def stop(io, kill:)
75
+ return if io.closed?
76
+
77
+ if kill
78
+ begin
79
+ Process.kill(Gem.win_platform? ? 'KILL' : 'TERM', io.pid)
80
+ rescue SystemCallError
81
+ nil
82
+ end
83
+ end
84
+ io.close
85
+ rescue SystemCallError, IOError
86
+ nil
87
+ end
88
+ end
89
+ end
data/lib/rufio/screen.rb CHANGED
@@ -156,7 +156,10 @@ module Rufio
156
156
  end
157
157
 
158
158
  # Clear the entire screen
159
- def clear
159
+ #
160
+ # @param mark_dirty [Boolean] true で全行を dirty にする。毎フレーム全体を描き直す
161
+ # 画面(G mode・内蔵検索)で、描かなくなった行も Renderer に消させるために使う
162
+ def clear(mark_dirty: false)
160
163
  @cells.each do |row|
161
164
  row.each do |cell|
162
165
  cell.char = ' '
@@ -167,6 +170,7 @@ module Rufio
167
170
  end
168
171
  # Phase1: Clear dirty rows after full clear
169
172
  @dirty_rows.clear
173
+ @height.times { |y| @dirty_rows.add(y) } if mark_dirty
170
174
  end
171
175
 
172
176
  # Phase1: Get dirty rows (rows that have been modified since last clear)
@@ -1,14 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'shellwords'
3
+ require_relative 'fuzzy_search_session'
4
+ require_relative 'rga_result_source'
5
+ require_relative 'fuzzy_candidate_cache'
4
6
 
5
7
  module Rufio
6
- # 検索(fzf・rga)専用コントローラ
7
- # KeybindHandler から fzf/rga 検索系メソッドを分離し、単一責任原則に準拠
8
+ # 検索(内蔵ファジー検索・rga)専用コントローラ
9
+ # 選択画面は TerminalUI の内蔵検索画面(FuzzyFinder)を使う
8
10
  class SearchController
9
- def initialize(directory_listing, file_opener)
11
+ def initialize(directory_listing, file_opener, dialog_renderer = nil)
10
12
  @directory_listing = directory_listing
11
13
  @file_opener = file_opener
14
+ @dialog_renderer = dialog_renderer
15
+ @terminal_ui = nil
16
+ # s / / を開き直したときに走査結果を再利用する(容量・有効期限は FUZZY_SEARCH_CACHE で変更可)
17
+ settings = ConfigLoader.fuzzy_search_cache
18
+ @candidate_cache = FuzzyCandidateCache.new(max_entries: settings[:max_entries], max_roots: settings[:max_roots],
19
+ ttl: settings[:ttl])
20
+ end
21
+
22
+ def set_terminal_ui(terminal_ui)
23
+ @terminal_ui = terminal_ui
12
24
  end
13
25
 
14
26
  def set_directory_listing(directory_listing)
@@ -16,77 +28,67 @@ module Rufio
16
28
  end
17
29
 
18
30
  # ============================
19
- # fzf 検索
31
+ # 内蔵ファジー検索(s / /)
20
32
  # ============================
21
33
 
22
- def fzf_search
23
- return false unless fzf_available?
34
+ # 現在のディレクトリ配下のファイルを内蔵検索画面で絞り込み、選択したファイルを開く。
35
+ # 直近に走査したディレクトリはキャッシュから表示し、Ctrl+R で走査し直す。
36
+ def file_search
37
+ return false unless @terminal_ui
24
38
 
25
39
  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
40
+ source, cached_at = @candidate_cache.source_for(current_path)
41
+ rescan = lambda do
42
+ @candidate_cache.delete(current_path)
43
+ FuzzySearchSession.new(source: @candidate_cache.source_for(current_path).first)
30
44
  end
45
+ @terminal_ui.start_fuzzy_finder(FuzzySearchSession.new(source: source), title: "Files: #{current_path}",
46
+ cached_at: cached_at, rescan: rescan, preview: preview_enabled?) do |entry|
47
+ next :missing unless File.file?(entry[:path])
31
48
 
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)
49
+ @file_opener.open_file(entry[:path])
50
+ :needs_refresh
35
51
  end
36
-
37
- :needs_refresh
52
+ true
38
53
  end
39
54
 
40
- def fzf_available?
41
- system('which fzf > /dev/null 2>&1')
42
- end
55
+ # 旧アクション名(ユーザー設定の fzf_search)との互換
56
+ alias fzf_search file_search
43
57
 
44
58
  # ============================
45
- # rga 検索
59
+ # rga 検索(F)
46
60
  # ============================
47
61
 
62
+ # rga の結果を内蔵検索画面に流し、選択した行でファイルを開く
48
63
  def rga_search
49
- return false unless rga_available?
64
+ return false unless rga_available? && @terminal_ui && @dialog_renderer
50
65
 
51
66
  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
67
+ search_query = @dialog_renderer.show_input_dialog('Content search (rga)', ConfigLoader.message('keybind.search_text'), {
68
+ border_color: "\e[36m",
69
+ title_color: "\e[1;36m",
70
+ content_color: "\e[37m"
71
+ })
72
+ return false if search_query.nil? || search_query.empty?
73
+
74
+ session = FuzzySearchSession.new(source: RgaResultSource.new(search_query, current_path))
75
+ @terminal_ui.start_fuzzy_finder(session, title: "Content: #{search_query}", preview: preview_enabled?) do |entry|
76
+ next :missing unless File.file?(entry[:path])
77
+
78
+ @file_opener.open_file_with_line(entry[:path], entry[:line_number])
79
+ :needs_refresh
69
80
  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
81
+ true
86
82
  end
87
83
 
88
84
  def rga_available?
89
85
  system('which rga > /dev/null 2>&1')
90
86
  end
87
+
88
+ private
89
+
90
+ def preview_enabled?
91
+ ConfigLoader.ui_options[:preview_enabled] != false
92
+ end
91
93
  end
92
94
  end
@@ -4,6 +4,7 @@ require 'io/console'
4
4
  require_relative 'text_utils'
5
5
  require_relative 'help_menu'
6
6
  require_relative 'global_search'
7
+ require_relative 'fuzzy_finder'
7
8
  require_relative 'ui_renderer'
8
9
 
9
10
  module Rufio
@@ -34,6 +35,8 @@ module Rufio
34
35
  @command_mode_active = false
35
36
  @help_menu = HelpMenu.new
36
37
  @global_search = GlobalSearch.new
38
+ @fuzzy_finder = FuzzyFinder.new
39
+ @fuzzy_finder_on_select = nil
37
40
  @command_input = ""
38
41
  @command_mode = CommandMode.new
39
42
  @dialog_renderer = DialogRenderer.new
@@ -202,7 +205,7 @@ module Rufio
202
205
  # ConPTY ではバイト到着が遅れることがあるため 5ms では ESC 単体と誤判定する。
203
206
  # G mode は検索中に全画面を再描画し続けて遅延が大きくなりやすく、
204
207
  # しかも ESC 単体で閉じてしまうため、待ち時間を長めに取る。
205
- timeout = global_search_active? ? 0.1 : 0.025
208
+ timeout = search_overlay_active? ? 0.1 : 0.025
206
209
  b = windows_queue_pop_with_timeout(timeout)
207
210
  b.nil? ? nil : b.chr(Encoding::BINARY)
208
211
  else
@@ -237,6 +240,7 @@ module Rufio
237
240
 
238
241
  def cleanup_terminal
239
242
  @global_search.close
243
+ @fuzzy_finder.close
240
244
  # マウスレポートを先に無効化して新しいイベントの流入を止める
241
245
  if windows?
242
246
  print "\e[?1000l\e[?1006l"
@@ -356,6 +360,7 @@ module Rufio
356
360
  needs_redraw = true if @background_status.update(@background_executor, now: start)
357
361
 
358
362
  needs_redraw = true if @global_search.poll
363
+ needs_redraw = true if @fuzzy_finder.poll
359
364
  needs_redraw = true if @keybind_handler&.poll_archive_jobs
360
365
 
361
366
  # 非同期シンタックスハイライト完了チェック(UIRenderer側のフラグを参照)
@@ -408,6 +413,12 @@ module Rufio
408
413
 
409
414
  # UIRenderer に全描画処理を委譲
410
415
  def draw_screen_to_buffer(screen, notification_message = nil, fps = nil)
416
+ if fuzzy_finder_active?
417
+ @fuzzy_finder.draw(screen)
418
+ @ui_renderer.draw_mode_tabs_to_buffer(screen, @screen_height - 1)
419
+ return
420
+ end
421
+
411
422
  if global_search_active?
412
423
  @global_search.draw(screen)
413
424
  @ui_renderer.draw_mode_tabs_to_buffer(screen, @screen_height - 1)
@@ -482,19 +493,19 @@ module Rufio
482
493
  end
483
494
  return true
484
495
  end
485
- if global_search_active? && !help_dialog_active?
496
+ if search_overlay_active? && !help_dialog_active?
486
497
  input = read_global_search_csi(third_char)
487
498
  return true unless input
488
499
  else
489
500
  input = case third_char
490
- when 'A' then global_search_active? && !help_dialog_active? ? :up : 'k' # Up arrow
491
- when 'B' then global_search_active? && !help_dialog_active? ? :down : 'j' # Down arrow
492
- when 'C' then global_search_active? && !help_dialog_active? ? :right : 'l' # Right arrow
493
- when 'D' then global_search_active? && !help_dialog_active? ? :left : 'h' # Left arrow
501
+ when 'A' then search_overlay_active? && !help_dialog_active? ? :up : 'k' # Up arrow
502
+ when 'B' then search_overlay_active? && !help_dialog_active? ? :down : 'j' # Down arrow
503
+ when 'C' then search_overlay_active? && !help_dialog_active? ? :right : 'l' # Right arrow
504
+ when 'D' then search_overlay_active? && !help_dialog_active? ? :left : 'h' # Left arrow
494
505
  when 'Z'
495
506
  if help_dialog_active?
496
507
  handle_help_input("\e[Z")
497
- elsif !global_search_active?
508
+ elsif !search_overlay_active?
498
509
  handle_shift_tab
499
510
  end
500
511
  return true # Shift+Tab
@@ -505,7 +516,7 @@ module Rufio
505
516
  "\e"
506
517
  end
507
518
  end
508
- elsif next_char == 'O' && global_search_active? && !help_dialog_active?
519
+ elsif next_char == 'O' && search_overlay_active? && !help_dialog_active?
509
520
  # Application cursor mode (SS3), used by terminals such as Ghostty.
510
521
  input = { 'A' => :up, 'B' => :down, 'C' => :right, 'D' => :left }[read_next_input_byte]
511
522
  return true unless input
@@ -515,6 +526,7 @@ module Rufio
515
526
  end
516
527
 
517
528
  return handle_help_input(input) if help_dialog_active?
529
+ return handle_fuzzy_finder_input(input) if fuzzy_finder_active?
518
530
  return handle_global_search_input(input) if global_search_active?
519
531
 
520
532
  # TabキーはFilesモードの時のみブックマーク循環移動(ジョブモード中は無効)
@@ -611,6 +623,12 @@ module Rufio
611
623
  return
612
624
  end
613
625
 
626
+ if fuzzy_finder_active?
627
+ handle_fuzzy_finder_input(:up) if btn == 64
628
+ handle_fuzzy_finder_input(:down) if btn == 65
629
+ return
630
+ end
631
+
614
632
  if global_search_active?
615
633
  handle_global_search_input(:up) if btn == 64
616
634
  handle_global_search_input(:down) if btn == 65
@@ -1016,9 +1034,53 @@ module Rufio
1016
1034
  attr_reader :screen, :renderer
1017
1035
 
1018
1036
  def start_global_search(bookmarks)
1037
+ close_fuzzy_finder
1019
1038
  @global_search.open(bookmarks)
1020
1039
  end
1021
1040
 
1041
+ # s / / / F の内蔵検索画面を開く。Enter で選んだ候補をブロックに渡し、
1042
+ # ブロックが :needs_refresh を返したら(外部エディタから戻った後など)全画面を再描画する。
1043
+ # :missing を返したら画面を閉じずに通知を出す。options は FuzzyFinder#open に渡す
1044
+ # (cached_at: / rescan: / preview:)。
1045
+ def start_fuzzy_finder(session, title:, **options, &on_select)
1046
+ @global_search.close
1047
+ @fuzzy_finder_on_select = on_select
1048
+ @fuzzy_finder.open(session, title: title, **options)
1049
+ # 入力ダイアログ等が直接描いた領域が残らないよう、差分ではなく全画面を描き直す
1050
+ render_full_screen
1051
+ true
1052
+ end
1053
+
1054
+ def fuzzy_finder_active?
1055
+ @fuzzy_finder.active?
1056
+ end
1057
+
1058
+ def close_fuzzy_finder
1059
+ @fuzzy_finder.close
1060
+ @fuzzy_finder_on_select = nil
1061
+ end
1062
+
1063
+ def handle_fuzzy_finder_input(input)
1064
+ entry = @fuzzy_finder.handle_key(input)
1065
+ return true unless entry
1066
+
1067
+ result = @fuzzy_finder_on_select&.call(entry)
1068
+ if result == :missing
1069
+ # キャッシュや rga の結果より後に削除・移動されたファイル
1070
+ @fuzzy_finder.show_notice('File no longer exists')
1071
+ return true
1072
+ end
1073
+
1074
+ close_fuzzy_finder
1075
+ refresh_display if result == :needs_refresh
1076
+ true
1077
+ end
1078
+
1079
+ # G mode と内蔵検索画面は、どちらも矢印キー等を移動として受け取る
1080
+ def search_overlay_active?
1081
+ global_search_active? || fuzzy_finder_active?
1082
+ end
1083
+
1022
1084
  def global_search_active?
1023
1085
  @global_search.active?
1024
1086
  end
@@ -652,7 +652,7 @@ module Rufio
652
652
  end
653
653
 
654
654
  # パスとバージョン情報を行末に追加
655
- current_path = @directory_listing.current_path
655
+ current_path = @directory_listing&.current_path || Dir.pwd
656
656
  version_str = " rufio v#{VERSION}"
657
657
  version_w = version_str.length
658
658
 
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.5.1'
4
+ VERSION = '1.6.0'
5
5
  end
data/lib/rufio.rb CHANGED
@@ -8,6 +8,12 @@ require_relative 'rufio/key_input'
8
8
  require_relative 'rufio/shell_init'
9
9
  require_relative 'rufio/directory_listing'
10
10
  require_relative 'rufio/filter_manager'
11
+ require_relative 'rufio/fuzzy_search'
12
+ require_relative 'rufio/fuzzy_search_session'
13
+ require_relative 'rufio/rga_result_source'
14
+ require_relative 'rufio/fuzzy_candidate_cache'
15
+ require_relative 'rufio/fuzzy_preview_loader'
16
+ require_relative 'rufio/fuzzy_finder'
11
17
  require_relative 'rufio/selection_manager'
12
18
  require_relative 'rufio/file_operations'
13
19
  require_relative 'rufio/bookmark_storage'
@@ -4,6 +4,7 @@
4
4
  # make install 現在の環境向けだけを Gem 同梱と同じ設定でビルドして lib/rufio/native/<platform>/ に配置
5
5
  # make dist Gem 同梱用に全プラットフォームをクロスコンパイル
6
6
  # make test 検索エンジンの単体テスト
7
+ # make sync-nvim 共通コア (search/os/fuzzy とテスト) を usurai.nvim に同期してビルド
7
8
 
8
9
  ZIG := zig
9
10
  RUBY := ruby
@@ -15,7 +16,9 @@ ZIG_FLAGS := -O ReleaseFast -dynamic -lc
15
16
  TARGET := librufio_zig.dylib
16
17
  BENCH := rufio_search
17
18
  SRC := src/main.zig
18
- SOURCES := src/main.zig src/search.zig src/os.zig src/zip.zig
19
+ SOURCES := src/main.zig src/search.zig src/os.zig src/zip.zig src/fuzzy.zig
20
+ SHARED_SOURCES := src/search.zig src/os.zig src/search_test.zig src/fuzzy.zig src/fuzzy_test.zig
21
+ NVIM_ZIG_DIR ?= ../../../usurai.nvim/zig
19
22
  NATIVE_DIR := ../../lib/rufio/native
20
23
  BUILD_DIR := zig-out/dist
21
24
 
@@ -42,6 +45,7 @@ $(BENCH): src/cli.zig src/search.zig src/os.zig
42
45
 
43
46
  test:
44
47
  $(ZIG) test src/search_test.zig
48
+ $(ZIG) test src/fuzzy_test.zig
45
49
 
46
50
  bench: $(BENCH)
47
51
  ./bench.sh
@@ -68,4 +72,14 @@ install:
68
72
  if [ -z "$$platform" ]; then echo "unsupported platform"; exit 1; fi; \
69
73
  $(MAKE) --no-print-directory dist ONLY=$$platform
70
74
 
71
- .PHONY: all clean install test bench dist
75
+ # 共通の検索・ファジーコアを usurai.nvim に同期してテスト・ビルドする
76
+ sync-nvim: $(SHARED_SOURCES)
77
+ @if [ ! -d "$(NVIM_ZIG_DIR)/src" ]; then \
78
+ echo "usurai.nvim Zig directory not found: $(NVIM_ZIG_DIR)/src"; \
79
+ exit 1; \
80
+ fi
81
+ cp $(SHARED_SOURCES) "$(NVIM_ZIG_DIR)/src/"
82
+ cd "$(NVIM_ZIG_DIR)" && $(ZIG) build test && $(ZIG) build -Doptimize=ReleaseFast
83
+ @echo "==> Synced $(SHARED_SOURCES) to $(NVIM_ZIG_DIR)/src and rebuilt librufio_zig"
84
+
85
+ .PHONY: all clean install test bench dist sync-nvim
@@ -39,4 +39,12 @@ pub fn build(b: *std.Build) void {
39
39
  });
40
40
  const test_step = b.step("test", "検索エンジンの単体テストを実行");
41
41
  test_step.dependOn(&b.addRunArtifact(tests).step);
42
+ const fuzzy_tests = b.addTest(.{
43
+ .root_module = b.createModule(.{
44
+ .root_source_file = b.path("src/fuzzy_test.zig"),
45
+ .target = target,
46
+ .optimize = optimize,
47
+ }),
48
+ });
49
+ test_step.dependOn(&b.addRunArtifact(fuzzy_tests).step);
42
50
  }