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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +49 -0
- data/README.md +17 -10
- data/README_ja.md +17 -9
- data/SECURITY.md +14 -6
- data/docs/csv_cell_preview_design_ja.md +682 -0
- data/docs/guide.md +109 -47
- data/docs/guide_ja.md +110 -48
- data/docs/image_preview_design_ja.md +335 -0
- data/docs/native_fuzzy_search_design_ja.md +2 -2
- data/examples/config.rb +23 -0
- data/info/help.md +32 -27
- data/info/keybindings.md +42 -12
- data/info/welcome.md +4 -6
- data/lib/rufio/background_status.rb +7 -0
- data/lib/rufio/bookmark_controller.rb +0 -45
- data/lib/rufio/clipboard.rb +162 -0
- data/lib/rufio/command_locator.rb +49 -0
- data/lib/rufio/config.rb +7 -2
- data/lib/rufio/config_loader.rb +56 -21
- data/lib/rufio/csv_table_source.rb +125 -0
- data/lib/rufio/encoding_detector.rb +152 -0
- data/lib/rufio/file_operation_controller.rb +41 -0
- data/lib/rufio/file_preview.rb +92 -19
- data/lib/rufio/graphics_layer.rb +223 -0
- data/lib/rufio/health_checker.rb +27 -10
- data/lib/rufio/help_menu.rb +16 -11
- data/lib/rufio/image_preview_view.rb +421 -0
- data/lib/rufio/image_source.rb +158 -0
- data/lib/rufio/keybind_handler.rb +21 -35
- data/lib/rufio/kitty_graphics.rb +128 -0
- data/lib/rufio/native/aarch64-linux-gnu/librufio_zig.so +0 -0
- data/lib/rufio/native/aarch64-linux-musl/librufio_zig.so +0 -0
- data/lib/rufio/native/aarch64-macos/librufio_zig.dylib +0 -0
- data/lib/rufio/native/aarch64-windows/rufio_zig.dll +0 -0
- data/lib/rufio/native/x86_64-linux-gnu/librufio_zig.so +0 -0
- data/lib/rufio/native/x86_64-linux-musl/librufio_zig.so +0 -0
- data/lib/rufio/native/x86_64-macos/librufio_zig.dylib +0 -0
- data/lib/rufio/native/x86_64-windows/rufio_zig.dll +0 -0
- data/lib/rufio/native_content_search.rb +1 -1
- data/lib/rufio/native_image.rb +177 -0
- data/lib/rufio/native_scanner_zig.rb +22 -0
- data/lib/rufio/navigation_controller.rb +47 -16
- data/lib/rufio/preview_registry.rb +86 -0
- data/lib/rufio/preview_view.rb +363 -0
- data/lib/rufio/renderer.rb +8 -8
- data/lib/rufio/screen.rb +33 -0
- data/lib/rufio/search_prompt.rb +50 -0
- data/lib/rufio/{global_search.rb → search_screen.rb} +41 -14
- data/lib/rufio/sheet_picker.rb +87 -0
- data/lib/rufio/sqlite_table_source.rb +205 -0
- data/lib/rufio/tab_mode_manager.rb +1 -3
- data/lib/rufio/table_cursor.rb +117 -0
- data/lib/rufio/table_document.rb +261 -0
- data/lib/rufio/table_layout.rb +231 -0
- data/lib/rufio/table_preview_view.rb +376 -0
- data/lib/rufio/table_query.rb +240 -0
- data/lib/rufio/table_source.rb +46 -0
- data/lib/rufio/terminal_graphics.rb +168 -0
- data/lib/rufio/terminal_ui.rb +63 -111
- data/lib/rufio/text_utils.rb +27 -0
- data/lib/rufio/ui_renderer.rb +379 -16
- data/lib/rufio/version.rb +1 -1
- data/lib/rufio/xlsx_table_source.rb +112 -0
- data/lib/rufio.rb +19 -5
- data/lib_zig/rufio_native/Makefile +6 -4
- data/lib_zig/rufio_native/build.zig +10 -0
- data/lib_zig/rufio_native/src/image.zig +525 -0
- data/lib_zig/rufio_native/src/image_bmp.zig +111 -0
- data/lib_zig/rufio_native/src/image_jpeg.zig +878 -0
- data/lib_zig/rufio_native/src/image_png.zig +561 -0
- data/lib_zig/rufio_native/src/image_resize.zig +170 -0
- data/lib_zig/rufio_native/src/image_test.zig +401 -0
- data/lib_zig/rufio_native/src/main.zig +1 -0
- data/lib_zig/rufio_native/src/search.zig +38 -19
- data/lib_zig/rufio_native/src/search_test.zig +35 -0
- data/lib_zig/rufio_native/src/testdata/alpha.png +0 -0
- data/lib_zig/rufio_native/src/testdata/gradient.bmp +0 -0
- data/lib_zig/rufio_native/src/testdata/gradient.jpg +0 -0
- data/lib_zig/rufio_native/src/testdata/gradient.png +0 -0
- data/retag.sh +4 -4
- metadata +37 -9
- data/lib/rufio/fuzzy_candidate_cache.rb +0 -258
- data/lib/rufio/fuzzy_finder.rb +0 -313
- data/lib/rufio/fuzzy_preview_loader.rb +0 -107
- data/lib/rufio/rga_result_source.rb +0 -89
- data/lib/rufio/search_controller.rb +0 -94
- data/lib_zig/rufio_native/src/main.zig.sync +0 -205
data/lib/rufio/fuzzy_finder.rb
DELETED
|
@@ -1,313 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative 'text_utils'
|
|
4
|
-
require_relative 'fuzzy_search'
|
|
5
|
-
require_relative 'fuzzy_preview_loader'
|
|
6
|
-
|
|
7
|
-
module Rufio
|
|
8
|
-
# s / / / F の内蔵検索画面。FuzzySearchSession の最新スナップショットを描画し、
|
|
9
|
-
# Enter で選択中の候補(entry)を返す。セッションの起動・終了もこの画面が受け持つ。
|
|
10
|
-
# 幅が PREVIEW_MIN_WIDTH 以上でプレビューを有効にした場合は、右半分に選択中のファイルを表示する。
|
|
11
|
-
class FuzzyFinder
|
|
12
|
-
SPINNER_FRAMES = ['|', '/', '-', '\\'].freeze
|
|
13
|
-
SPINNER_INTERVAL = 0.12
|
|
14
|
-
MATCH_FG = "\e[30m\e[1m"
|
|
15
|
-
MATCH_BG = "\e[43m"
|
|
16
|
-
EMPTY = [].freeze
|
|
17
|
-
PREVIEW_MIN_WIDTH = 80
|
|
18
|
-
PREVIEW_LINE_BG = "\e[100m"
|
|
19
|
-
TAB = ' '
|
|
20
|
-
|
|
21
|
-
attr_reader :query, :selected_index, :title, :cached_at
|
|
22
|
-
|
|
23
|
-
def initialize(preview_loader_factory: -> { FuzzyPreviewLoader.new })
|
|
24
|
-
@preview_loader_factory = preview_loader_factory
|
|
25
|
-
@session = nil
|
|
26
|
-
@preview_loader = nil
|
|
27
|
-
@query = ''
|
|
28
|
-
@selected_index = 0
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
# cached_at: キャッシュした候補を流す場合の保存時刻(monotonic)。rescan: Ctrl+R で
|
|
32
|
-
# 作り直すセッションを返す Proc。preview: true でプレビューを読み込む。
|
|
33
|
-
def open(session, title:, cached_at: nil, rescan: nil, preview: false)
|
|
34
|
-
close
|
|
35
|
-
@session = session
|
|
36
|
-
@title = title.to_s
|
|
37
|
-
@cached_at = cached_at
|
|
38
|
-
@rescan = rescan
|
|
39
|
-
@notice = nil
|
|
40
|
-
@preview_loader = preview ? @preview_loader_factory.call : nil
|
|
41
|
-
@query = ''
|
|
42
|
-
@selected_index = 0
|
|
43
|
-
@selected_id = nil
|
|
44
|
-
@spinner_index = 0
|
|
45
|
-
@last_spinner_update = monotonic
|
|
46
|
-
@shown = nil
|
|
47
|
-
session.start
|
|
48
|
-
true
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
def active?
|
|
52
|
-
!@session.nil?
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
# セッションとプレビューの停止は非同期。所有ワーカーが後処理を続けるため、ここでは待たない。
|
|
56
|
-
def close
|
|
57
|
-
session = @session
|
|
58
|
-
@session = nil
|
|
59
|
-
session&.close
|
|
60
|
-
@preview_loader&.close
|
|
61
|
-
@preview_loader = nil
|
|
62
|
-
nil
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def preview?
|
|
66
|
-
!@preview_loader.nil?
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
def preview_pending?
|
|
70
|
-
@preview_loader ? @preview_loader.pending? : false
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
# 選択結果を開けなかったときなどに、次のキー入力までフッターに表示する
|
|
74
|
-
def show_notice(text)
|
|
75
|
-
@notice = text.to_s
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
def results
|
|
79
|
-
@session ? @session.results : EMPTY
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
def busy?
|
|
83
|
-
return false unless @session
|
|
84
|
-
|
|
85
|
-
state = @session.snapshot
|
|
86
|
-
state.scanning || state.searching
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
def poll
|
|
90
|
-
return false unless @session
|
|
91
|
-
|
|
92
|
-
@session.poll
|
|
93
|
-
state = @session.snapshot
|
|
94
|
-
changed = !state.equal?(@shown)
|
|
95
|
-
if changed
|
|
96
|
-
@shown = state
|
|
97
|
-
follow_selection(state.results)
|
|
98
|
-
end
|
|
99
|
-
request_preview
|
|
100
|
-
changed = true if @preview_loader&.poll
|
|
101
|
-
if state.scanning || state.searching
|
|
102
|
-
now = monotonic
|
|
103
|
-
if now - @last_spinner_update >= SPINNER_INTERVAL
|
|
104
|
-
@spinner_index = (@spinner_index + 1) % SPINNER_FRAMES.length
|
|
105
|
-
@last_spinner_update = now
|
|
106
|
-
changed = true
|
|
107
|
-
end
|
|
108
|
-
end
|
|
109
|
-
changed
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
def handle_key(key)
|
|
113
|
-
return nil unless @session
|
|
114
|
-
|
|
115
|
-
@notice = nil
|
|
116
|
-
case key
|
|
117
|
-
when "\e" then close
|
|
118
|
-
when "\r"
|
|
119
|
-
return results[@selected_index]&.[](:entry)
|
|
120
|
-
when :down then select_index(@selected_index + 1)
|
|
121
|
-
when :up then select_index(@selected_index - 1)
|
|
122
|
-
when "\x0a", "\x0e" then handle_key(:down) # Ctrl+J / Ctrl+N
|
|
123
|
-
when "\x0b", "\x10" then handle_key(:up) # Ctrl+K / Ctrl+P
|
|
124
|
-
when "\x15" then update_query('') # Ctrl+U
|
|
125
|
-
when "\x12" then rescan # Ctrl+R
|
|
126
|
-
when "\u007f", "\b" then update_query(@query.chop)
|
|
127
|
-
else
|
|
128
|
-
update_query(@query + key) if printable?(key)
|
|
129
|
-
end
|
|
130
|
-
request_preview
|
|
131
|
-
nil
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
def draw(screen)
|
|
135
|
-
screen.clear(mark_dirty: true)
|
|
136
|
-
width = screen.width
|
|
137
|
-
state = @session&.snapshot
|
|
138
|
-
return unless state
|
|
139
|
-
|
|
140
|
-
header = [@title]
|
|
141
|
-
header << "Cached #{cache_age}" if @cached_at
|
|
142
|
-
header << 'Ctrl+R: Rescan' if @rescan
|
|
143
|
-
write_line(screen, 0, (header << 'ESC: Back').join(' | '), width, "\e[1;36m")
|
|
144
|
-
write_line(screen, 1, "> #{@query}_", width)
|
|
145
|
-
busy = state.scanning || state.searching
|
|
146
|
-
status = busy ? "[#{SPINNER_FRAMES[@spinner_index]}] Searching..." : 'Search complete'
|
|
147
|
-
write_line(screen, 2, "#{status} | #{state.results.length}/#{state.total_matches} matches | " \
|
|
148
|
-
"#{state.candidate_count} candidates | #{state.errors.length} errors",
|
|
149
|
-
width, busy ? "\e[1;33m" : "\e[32m")
|
|
150
|
-
|
|
151
|
-
capacity = [screen.height - 6, 0].max
|
|
152
|
-
list_width = preview_visible?(width) ? width / 2 : width
|
|
153
|
-
first = [@selected_index - capacity + 1, 0].max
|
|
154
|
-
state.results.slice(first, capacity).to_a.each_with_index do |result, index|
|
|
155
|
-
selected = first + index == @selected_index
|
|
156
|
-
draw_result(screen, index + 3, result, selected, list_width)
|
|
157
|
-
end
|
|
158
|
-
if state.results.empty?
|
|
159
|
-
message = busy ? 'Searching...' : 'No matching candidates. Edit your search to try again.'
|
|
160
|
-
write_line(screen, 3, message, list_width)
|
|
161
|
-
end
|
|
162
|
-
|
|
163
|
-
entry = state.results[@selected_index]&.[](:entry)
|
|
164
|
-
draw_preview(screen, list_width, capacity, entry) if list_width < width
|
|
165
|
-
if @notice
|
|
166
|
-
notice = @rescan ? "#{@notice} | Ctrl+R: Rescan" : @notice
|
|
167
|
-
write_line(screen, screen.height - 3, notice, width, "\e[1;33m")
|
|
168
|
-
else
|
|
169
|
-
footer = if entry
|
|
170
|
-
entry[:line_number] ? "#{entry[:path]}:#{entry[:line_number]}" : entry[:path].to_s
|
|
171
|
-
else
|
|
172
|
-
state.errors.first.to_s
|
|
173
|
-
end
|
|
174
|
-
write_line(screen, screen.height - 3, footer, width, "\e[90m")
|
|
175
|
-
end
|
|
176
|
-
write_line(screen, screen.height - 2,
|
|
177
|
-
'ESC: Back | Enter: Open | Up/Down or Ctrl+J/K: Select | Ctrl+U: Clear | Type: Search', width)
|
|
178
|
-
end
|
|
179
|
-
|
|
180
|
-
private
|
|
181
|
-
|
|
182
|
-
def monotonic
|
|
183
|
-
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
184
|
-
end
|
|
185
|
-
|
|
186
|
-
def cache_age
|
|
187
|
-
seconds = [(monotonic - @cached_at).floor, 0].max
|
|
188
|
-
if seconds < 60 then "#{seconds}s ago"
|
|
189
|
-
elsif seconds < 3600 then "#{seconds / 60}m ago"
|
|
190
|
-
else "#{seconds / 3600}h ago"
|
|
191
|
-
end
|
|
192
|
-
end
|
|
193
|
-
|
|
194
|
-
def printable?(key)
|
|
195
|
-
key.is_a?(String) && !key.empty? && key.valid_encoding? &&
|
|
196
|
-
key.each_codepoint.all? { |c| c >= 32 && c != 127 }
|
|
197
|
-
end
|
|
198
|
-
|
|
199
|
-
def update_query(text)
|
|
200
|
-
return if text == @query || text.bytesize > FuzzySearch::MAX_QUERY_BYTES
|
|
201
|
-
|
|
202
|
-
@session.update_query(text)
|
|
203
|
-
@query = text
|
|
204
|
-
@selected_index = 0
|
|
205
|
-
@selected_id = nil
|
|
206
|
-
rescue IOError
|
|
207
|
-
# 検索ワーカーが失敗したセッションは入力を受け付けない。エラーは画面に表示済み。
|
|
208
|
-
nil
|
|
209
|
-
end
|
|
210
|
-
|
|
211
|
-
# 検索語を保ったまま、新しいセッション(キャッシュを使わない再走査)に切り替える
|
|
212
|
-
def rescan
|
|
213
|
-
return unless @rescan
|
|
214
|
-
|
|
215
|
-
fresh = @rescan.call
|
|
216
|
-
previous = @session
|
|
217
|
-
@session = fresh
|
|
218
|
-
previous.close
|
|
219
|
-
fresh.start
|
|
220
|
-
fresh.update_query(@query) unless @query.empty?
|
|
221
|
-
@cached_at = nil
|
|
222
|
-
@shown = nil
|
|
223
|
-
@selected_index = 0
|
|
224
|
-
@selected_id = nil
|
|
225
|
-
rescue IOError
|
|
226
|
-
nil
|
|
227
|
-
end
|
|
228
|
-
|
|
229
|
-
def select_index(index)
|
|
230
|
-
list = results
|
|
231
|
-
@selected_index = index.clamp(0, [list.length - 1, 0].max)
|
|
232
|
-
@selected_id = list[@selected_index]&.[](:id)
|
|
233
|
-
end
|
|
234
|
-
|
|
235
|
-
# 候補追加で順位が変わっても、選択中の候補を追い続ける
|
|
236
|
-
def follow_selection(list)
|
|
237
|
-
index = @selected_id && list.index { |result| result[:id] == @selected_id }
|
|
238
|
-
@selected_index = (index || @selected_index).clamp(0, [list.length - 1, 0].max)
|
|
239
|
-
end
|
|
240
|
-
|
|
241
|
-
def preview_visible?(width)
|
|
242
|
-
!@preview_loader.nil? && width >= PREVIEW_MIN_WIDTH
|
|
243
|
-
end
|
|
244
|
-
|
|
245
|
-
def request_preview
|
|
246
|
-
return unless @preview_loader && @session
|
|
247
|
-
|
|
248
|
-
entry = results[@selected_index]&.[](:entry)
|
|
249
|
-
@preview_loader.request(entry[:path], entry[:line_number]) if entry
|
|
250
|
-
end
|
|
251
|
-
|
|
252
|
-
def draw_result(screen, y, result, selected, width)
|
|
253
|
-
prefix = selected ? '> ' : ' '
|
|
254
|
-
text = result[:entry][:text].gsub(/[[:cntrl:]]/, ' ')
|
|
255
|
-
write_line(screen, y, prefix + text, width, selected ? "\e[1;36m" : nil)
|
|
256
|
-
# 省略記号で切り詰めた行は、省略記号の位置をハイライトで上書きしない
|
|
257
|
-
limit = TextUtils.display_width(prefix + text) > width ? width - TextUtils::ELLIPSIS_MIN_WIDTH : width
|
|
258
|
-
offsets = result[:positions]
|
|
259
|
-
return if offsets.nil? || offsets.empty?
|
|
260
|
-
|
|
261
|
-
# 一致位置は UTF-8 の先頭バイトオフセット。表示桁に変換してハイライトする
|
|
262
|
-
targets = offsets.each_with_object({}) { |offset, set| set[offset] = true }
|
|
263
|
-
x = TextUtils.display_width(prefix)
|
|
264
|
-
byte = 0
|
|
265
|
-
result[:entry][:text].each_char do |char|
|
|
266
|
-
shown = char.match?(/[[:cntrl:]]/) ? ' ' : char
|
|
267
|
-
char_width = TextUtils.display_width(shown)
|
|
268
|
-
break if x + char_width > limit
|
|
269
|
-
|
|
270
|
-
screen.put(x, y, shown, fg: MATCH_FG, bg: MATCH_BG, width: char_width) if targets[byte]
|
|
271
|
-
x += char_width
|
|
272
|
-
byte += char.bytesize
|
|
273
|
-
end
|
|
274
|
-
end
|
|
275
|
-
|
|
276
|
-
# 右半分に選択中のファイルを表示する。行番号つきの候補(F)はその行を中央付近に出して強調する
|
|
277
|
-
def draw_preview(screen, list_width, capacity, entry)
|
|
278
|
-
x = list_width + 2
|
|
279
|
-
width = screen.width - x
|
|
280
|
-
capacity.times { |row| screen.put(list_width, row + 3, '│', fg: "\e[90m") }
|
|
281
|
-
return unless entry
|
|
282
|
-
|
|
283
|
-
preview = @preview_loader.result(entry[:path], entry[:line_number])
|
|
284
|
-
unless preview
|
|
285
|
-
write_at(screen, x, 3, 'Loading preview...', width, fg: "\e[90m")
|
|
286
|
-
return
|
|
287
|
-
end
|
|
288
|
-
|
|
289
|
-
lines = preview[:lines] || EMPTY
|
|
290
|
-
target = entry[:line_number] && entry[:line_number] - 1
|
|
291
|
-
start = target ? (target - capacity / 2).clamp(0, [lines.length - capacity, 0].max) : 0
|
|
292
|
-
lines[start, capacity].to_a.each_with_index do |line, index|
|
|
293
|
-
text = line.to_s.scrub.gsub("\t", TAB).gsub(/[[:cntrl:]]/, ' ')
|
|
294
|
-
if start + index == target
|
|
295
|
-
shown = TextUtils.truncate_to_width(text, width)
|
|
296
|
-
shown += ' ' * [width - TextUtils.display_width(shown), 0].max
|
|
297
|
-
write_at(screen, x, index + 3, shown, width, bg: PREVIEW_LINE_BG)
|
|
298
|
-
else
|
|
299
|
-
write_at(screen, x, index + 3, text, width)
|
|
300
|
-
end
|
|
301
|
-
end
|
|
302
|
-
end
|
|
303
|
-
|
|
304
|
-
def write_line(screen, y, text, width, color = nil)
|
|
305
|
-
safe_text = text.to_s.scrub.gsub(/[[:cntrl:]]/, ' ')
|
|
306
|
-
screen.put_string(0, y, TextUtils.truncate_to_width(safe_text, width), fg: color)
|
|
307
|
-
end
|
|
308
|
-
|
|
309
|
-
def write_at(screen, x, y, text, width, fg: nil, bg: nil)
|
|
310
|
-
screen.put_string(x, y, TextUtils.truncate_to_width(text, width), fg: fg, bg: bg)
|
|
311
|
-
end
|
|
312
|
-
end
|
|
313
|
-
end
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'thread'
|
|
4
|
-
require_relative 'file_preview'
|
|
5
|
-
|
|
6
|
-
module Rufio
|
|
7
|
-
# 内蔵検索画面のプレビューを UI スレッドの外で読む。読み込み量は FilePreview の上限
|
|
8
|
-
# (64 KiB・1 行 500 文字)と MAX_LINES 行に制限する。処理中に選択が変わっても待たせず、
|
|
9
|
-
# 次はその時点の最新の要求だけを処理する。結果は直近 CACHE_SIZE 件を再利用する。
|
|
10
|
-
class FuzzyPreviewLoader
|
|
11
|
-
DEFAULT_LINES = 200
|
|
12
|
-
MAX_LINES = 2_000
|
|
13
|
-
CACHE_SIZE = 16
|
|
14
|
-
|
|
15
|
-
def initialize(previewer: FilePreview.new)
|
|
16
|
-
@previewer = previewer
|
|
17
|
-
@mutex = Mutex.new
|
|
18
|
-
@wake = ConditionVariable.new
|
|
19
|
-
@wanted = nil
|
|
20
|
-
@results = {}
|
|
21
|
-
@arrived = false
|
|
22
|
-
@closed = false
|
|
23
|
-
@thread = nil
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
def request(path, line_number = nil)
|
|
27
|
-
key = [path, line_number].freeze
|
|
28
|
-
@mutex.synchronize do
|
|
29
|
-
return if @closed || @wanted == key
|
|
30
|
-
|
|
31
|
-
@wanted = key
|
|
32
|
-
@arrived = false
|
|
33
|
-
if @results.key?(key)
|
|
34
|
-
@results[key] = @results.delete(key)
|
|
35
|
-
else
|
|
36
|
-
@thread ||= Thread.new { run }
|
|
37
|
-
@wake.signal
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
nil
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
# 最新の要求の結果が前回の poll 以降に届いたら true
|
|
44
|
-
def poll
|
|
45
|
-
@mutex.synchronize do
|
|
46
|
-
arrived = @arrived
|
|
47
|
-
@arrived = false
|
|
48
|
-
arrived
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
def result(path, line_number = nil)
|
|
53
|
-
@mutex.synchronize { @results[[path, line_number]] }
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
def pending?
|
|
57
|
-
@mutex.synchronize { !@closed && !@wanted.nil? && !@results.key?(@wanted) }
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
# 停止を伝えるだけで待たない。読み込み中のファイル操作が戻った後にワーカーが終わる
|
|
61
|
-
def close
|
|
62
|
-
@mutex.synchronize do
|
|
63
|
-
@closed = true
|
|
64
|
-
@wanted = nil
|
|
65
|
-
@results.clear
|
|
66
|
-
@wake.signal
|
|
67
|
-
end
|
|
68
|
-
nil
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
def wait(timeout: nil)
|
|
72
|
-
thread = @mutex.synchronize { @thread }
|
|
73
|
-
thread.nil? || !thread.join(timeout).nil?
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
private
|
|
77
|
-
|
|
78
|
-
def run
|
|
79
|
-
loop do
|
|
80
|
-
key = @mutex.synchronize do
|
|
81
|
-
@wake.wait(@mutex) until @closed || (@wanted && !@results.key?(@wanted))
|
|
82
|
-
break nil if @closed
|
|
83
|
-
|
|
84
|
-
@wanted
|
|
85
|
-
end
|
|
86
|
-
break unless key
|
|
87
|
-
|
|
88
|
-
preview = load(*key)
|
|
89
|
-
@mutex.synchronize do
|
|
90
|
-
break if @closed
|
|
91
|
-
|
|
92
|
-
@results.delete(key)
|
|
93
|
-
@results[key] = preview
|
|
94
|
-
@results.shift while @results.length > CACHE_SIZE
|
|
95
|
-
@arrived = true if @wanted == key
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
def load(path, line_number)
|
|
101
|
-
lines = line_number ? (line_number.to_i + DEFAULT_LINES).clamp(DEFAULT_LINES, MAX_LINES) : DEFAULT_LINES
|
|
102
|
-
@previewer.preview_file(path, max_lines: lines).freeze
|
|
103
|
-
rescue StandardError => e
|
|
104
|
-
{ type: 'error', lines: ["#{e.class}: #{e.message}"] }.freeze
|
|
105
|
-
end
|
|
106
|
-
end
|
|
107
|
-
end
|
|
@@ -1,89 +0,0 @@
|
|
|
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
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative 'fuzzy_search_session'
|
|
4
|
-
require_relative 'rga_result_source'
|
|
5
|
-
require_relative 'fuzzy_candidate_cache'
|
|
6
|
-
|
|
7
|
-
module Rufio
|
|
8
|
-
# 検索(内蔵ファジー検索・rga)専用コントローラ
|
|
9
|
-
# 選択画面は TerminalUI の内蔵検索画面(FuzzyFinder)を使う
|
|
10
|
-
class SearchController
|
|
11
|
-
def initialize(directory_listing, file_opener, dialog_renderer = nil)
|
|
12
|
-
@directory_listing = directory_listing
|
|
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
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
def set_directory_listing(directory_listing)
|
|
27
|
-
@directory_listing = directory_listing
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
# ============================
|
|
31
|
-
# 内蔵ファジー検索(s / /)
|
|
32
|
-
# ============================
|
|
33
|
-
|
|
34
|
-
# 現在のディレクトリ配下のファイルを内蔵検索画面で絞り込み、選択したファイルを開く。
|
|
35
|
-
# 直近に走査したディレクトリはキャッシュから表示し、Ctrl+R で走査し直す。
|
|
36
|
-
def file_search
|
|
37
|
-
return false unless @terminal_ui
|
|
38
|
-
|
|
39
|
-
current_path = @directory_listing&.current_path || Dir.pwd
|
|
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)
|
|
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])
|
|
48
|
-
|
|
49
|
-
@file_opener.open_file(entry[:path])
|
|
50
|
-
:needs_refresh
|
|
51
|
-
end
|
|
52
|
-
true
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
# 旧アクション名(ユーザー設定の fzf_search)との互換
|
|
56
|
-
alias fzf_search file_search
|
|
57
|
-
|
|
58
|
-
# ============================
|
|
59
|
-
# rga 検索(F)
|
|
60
|
-
# ============================
|
|
61
|
-
|
|
62
|
-
# rga の結果を内蔵検索画面に流し、選択した行でファイルを開く
|
|
63
|
-
def rga_search
|
|
64
|
-
return false unless rga_available? && @terminal_ui && @dialog_renderer
|
|
65
|
-
|
|
66
|
-
current_path = @directory_listing&.current_path || Dir.pwd
|
|
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
|
|
80
|
-
end
|
|
81
|
-
true
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
def rga_available?
|
|
85
|
-
system('which rga > /dev/null 2>&1')
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
private
|
|
89
|
-
|
|
90
|
-
def preview_enabled?
|
|
91
|
-
ConfigLoader.ui_options[:preview_enabled] != false
|
|
92
|
-
end
|
|
93
|
-
end
|
|
94
|
-
end
|