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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +37 -0
- data/README.md +8 -3
- data/README_ja.md +7 -3
- data/Rakefile +5 -0
- data/SECURITY.md +2 -2
- data/docs/guide.md +41 -8
- data/docs/guide_ja.md +41 -8
- data/docs/native_fuzzy_search_design_ja.md +684 -0
- data/info/help.md +13 -4
- data/info/keybindings.md +5 -3
- data/info/welcome.md +2 -2
- data/lib/rufio/config.rb +0 -2
- data/lib/rufio/config_loader.rb +24 -4
- data/lib/rufio/fuzzy_candidate_block.rb +131 -0
- data/lib/rufio/fuzzy_candidate_cache.rb +258 -0
- data/lib/rufio/fuzzy_file_source.rb +180 -0
- data/lib/rufio/fuzzy_finder.rb +313 -0
- data/lib/rufio/fuzzy_preview_loader.rb +107 -0
- data/lib/rufio/fuzzy_search.rb +327 -0
- data/lib/rufio/fuzzy_search_session.rb +426 -0
- data/lib/rufio/global_search.rb +292 -51
- data/lib/rufio/health_checker.rb +0 -22
- data/lib/rufio/help_menu.rb +5 -4
- data/lib/rufio/keybind_handler.rb +6 -5
- 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 +43 -7
- data/lib/rufio/native_scanner_zig.rb +17 -0
- data/lib/rufio/rga_result_source.rb +89 -0
- data/lib/rufio/screen.rb +5 -1
- data/lib/rufio/search_controller.rb +56 -54
- data/lib/rufio/terminal_ui.rb +70 -8
- data/lib/rufio/ui_renderer.rb +1 -1
- data/lib/rufio/version.rb +1 -1
- data/lib/rufio.rb +6 -0
- data/lib_zig/rufio_native/Makefile +16 -2
- data/lib_zig/rufio_native/build.zig +8 -0
- data/lib_zig/rufio_native/src/fuzzy.zig +520 -0
- data/lib_zig/rufio_native/src/fuzzy_test.zig +212 -0
- data/lib_zig/rufio_native/src/main.zig +27 -0
- data/lib_zig/rufio_native/src/search.zig +221 -44
- metadata +13 -2
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'native_content_search'
|
|
4
|
+
require_relative 'fuzzy_candidate_block'
|
|
5
|
+
|
|
6
|
+
module Rufio
|
|
7
|
+
# Produces display labels and original paths separately. A source belongs to
|
|
8
|
+
# its producer thread, including creation and destruction of the native scan.
|
|
9
|
+
# Batches are FuzzyCandidateBlock: packed labels, with { text:, path: } rows built on demand.
|
|
10
|
+
class FuzzyFileSource
|
|
11
|
+
BATCH_SIZE = 256
|
|
12
|
+
# Zig の走査は 1 回の取り出しで多くのファイルを返すので、大きめのブロックにまとめて登録回数を減らす
|
|
13
|
+
BLOCK_SIZE = 4096
|
|
14
|
+
|
|
15
|
+
def initialize(root, native: true)
|
|
16
|
+
@root = File.expand_path(root).freeze
|
|
17
|
+
@prefix = @root.end_with?(File::SEPARATOR) ? @root : @root + File::SEPARATOR
|
|
18
|
+
@native = native
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def each_batch(cancelled:, on_error:)
|
|
22
|
+
return if cancelled.call
|
|
23
|
+
raise Errno::ENOTDIR, @root unless File.directory?(@root)
|
|
24
|
+
|
|
25
|
+
if @native && ENV['RUFIO_NATIVE_SEARCH'] != '0' && NativeContentSearch.labels_available?
|
|
26
|
+
scan_labels(cancelled, on_error) { |batch| yield batch }
|
|
27
|
+
elsif @native && ENV['RUFIO_NATIVE_SEARCH'] != '0' && NativeContentSearch.files_available?
|
|
28
|
+
scan_native(cancelled, on_error) { |batch| yield batch }
|
|
29
|
+
else
|
|
30
|
+
scan_ruby(cancelled, on_error) { |batch| yield batch }
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
# 1 ブロック分の候補を詰める。Hash や凍結した文字列を候補ごとに作らず、表示名だけを連結する。
|
|
37
|
+
# パスは「ルート/表示名」から組み立て直せるので、scrub した・エンコーディングが違う候補だけ別に持つ。
|
|
38
|
+
class Batch
|
|
39
|
+
def initialize(prefix, on_error)
|
|
40
|
+
@prefix = prefix
|
|
41
|
+
@binary_prefix = prefix.b.freeze
|
|
42
|
+
@on_error = on_error
|
|
43
|
+
reset
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def size
|
|
47
|
+
@builder.size
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def add(path)
|
|
51
|
+
relative = path.delete_prefix(@prefix)
|
|
52
|
+
text = relative.encoding == Encoding::UTF_8 ? relative : relative.dup.force_encoding(Encoding::UTF_8)
|
|
53
|
+
scrubbed = !text.valid_encoding?
|
|
54
|
+
text = text.scrub if scrubbed
|
|
55
|
+
if text.bytesize > FuzzyCandidateBlock::MAX_TEXT_BYTES || text.include?("\0")
|
|
56
|
+
@on_error.call("#{text[0, 200]}: file name cannot be searched")
|
|
57
|
+
return
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
@encoding ||= path.encoding
|
|
61
|
+
index = @builder.add(text)
|
|
62
|
+
return unless scrubbed || path.encoding != @encoding || relative.bytesize == path.bytesize
|
|
63
|
+
|
|
64
|
+
(@exceptions ||= {})[index] = path.frozen? ? path : path.dup.freeze
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# 詰めた候補をブロックにして取り出す。空なら nil
|
|
68
|
+
def take
|
|
69
|
+
return if @builder.empty?
|
|
70
|
+
|
|
71
|
+
block = @builder.build(FuzzyCandidateBlock::PathRows.new(@binary_prefix, @encoding, @exceptions&.freeze))
|
|
72
|
+
reset
|
|
73
|
+
block
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
def reset
|
|
79
|
+
@builder = FuzzyCandidateBlock::Builder.new
|
|
80
|
+
@exceptions = nil
|
|
81
|
+
@encoding = nil
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Zig が相対パスのラベル列を返す場合は、Ruby でファイルごとの文字列を作らずにそのままブロックにする
|
|
86
|
+
def scan_labels(cancelled, on_error)
|
|
87
|
+
scanner = NativeContentSearch.new(roots: [@root], labels: true)
|
|
88
|
+
rows = FuzzyCandidateBlock::PathRows.new(@prefix.b.freeze, Encoding.find('filesystem'))
|
|
89
|
+
batch = Batch.new(@prefix, on_error)
|
|
90
|
+
packed = +''.b
|
|
91
|
+
count = 0
|
|
92
|
+
limit = BATCH_SIZE
|
|
93
|
+
emit = lambda do
|
|
94
|
+
next if count.zero?
|
|
95
|
+
|
|
96
|
+
yield FuzzyCandidateBlock.new(packed.freeze, count, rows: rows, checked: true)
|
|
97
|
+
packed = +''.b
|
|
98
|
+
count = 0
|
|
99
|
+
limit = [limit * 2, BLOCK_SIZE].min
|
|
100
|
+
end
|
|
101
|
+
scanner.each_batch(cancelled: cancelled) do |events|
|
|
102
|
+
events.each do |kind, _root_index, value, size|
|
|
103
|
+
break if cancelled.call
|
|
104
|
+
|
|
105
|
+
case kind
|
|
106
|
+
when :labels
|
|
107
|
+
packed << value
|
|
108
|
+
count += size
|
|
109
|
+
emit.call if count >= limit
|
|
110
|
+
when :file then batch.add(value) # UTF-8 でない名前
|
|
111
|
+
when :error then on_error.call(value)
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
break if cancelled.call
|
|
115
|
+
|
|
116
|
+
emit.call
|
|
117
|
+
block = batch.take
|
|
118
|
+
yield block if block
|
|
119
|
+
end
|
|
120
|
+
ensure
|
|
121
|
+
scanner&.close
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def scan_native(cancelled, on_error)
|
|
125
|
+
scanner = NativeContentSearch.new(roots: [@root])
|
|
126
|
+
batch = Batch.new(@prefix, on_error)
|
|
127
|
+
# 最初の結果を早く出すため小さいブロックから始め、倍々に大きくする
|
|
128
|
+
limit = BATCH_SIZE
|
|
129
|
+
scanner.each_batch(cancelled: cancelled, max_events: BLOCK_SIZE) do |events|
|
|
130
|
+
events.each do |kind, _root_index, path|
|
|
131
|
+
break if cancelled.call
|
|
132
|
+
|
|
133
|
+
if kind == :file
|
|
134
|
+
batch.add(path)
|
|
135
|
+
if batch.size >= limit
|
|
136
|
+
yield batch.take
|
|
137
|
+
limit = [limit * 2, BLOCK_SIZE].min
|
|
138
|
+
end
|
|
139
|
+
elsif kind == :error
|
|
140
|
+
on_error.call(path)
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
block = batch.take
|
|
144
|
+
yield block if block && !cancelled.call
|
|
145
|
+
end
|
|
146
|
+
ensure
|
|
147
|
+
scanner&.close
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def scan_ruby(cancelled, on_error)
|
|
151
|
+
pending = [@root]
|
|
152
|
+
batch = Batch.new(@prefix, on_error)
|
|
153
|
+
until pending.empty? || cancelled.call
|
|
154
|
+
directory = pending.pop
|
|
155
|
+
begin
|
|
156
|
+
Dir.each_child(directory) do |name|
|
|
157
|
+
break if cancelled.call
|
|
158
|
+
|
|
159
|
+
path = File.join(directory, name)
|
|
160
|
+
begin
|
|
161
|
+
stat = File.lstat(path)
|
|
162
|
+
if stat.directory?
|
|
163
|
+
pending << path
|
|
164
|
+
elsif stat.file? || (stat.symlink? && File.file?(path))
|
|
165
|
+
batch.add(path)
|
|
166
|
+
yield batch.take if batch.size >= BATCH_SIZE
|
|
167
|
+
end
|
|
168
|
+
rescue SystemCallError, IOError => e
|
|
169
|
+
on_error.call("#{path}: #{e.message}")
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
rescue SystemCallError, IOError => e
|
|
173
|
+
on_error.call("#{directory}: #{e.message}")
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
block = batch.take
|
|
177
|
+
yield block if block && !cancelled.call
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
end
|
|
@@ -0,0 +1,313 @@
|
|
|
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
|
|
@@ -0,0 +1,107 @@
|
|
|
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
|