rufio 1.5.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +86 -0
  3. data/README.md +18 -6
  4. data/README_ja.md +18 -6
  5. data/Rakefile +5 -0
  6. data/SECURITY.md +16 -8
  7. data/docs/csv_cell_preview_design_ja.md +682 -0
  8. data/docs/guide.md +115 -20
  9. data/docs/guide_ja.md +116 -21
  10. data/docs/image_preview_design_ja.md +335 -0
  11. data/docs/native_fuzzy_search_design_ja.md +684 -0
  12. data/examples/config.rb +23 -0
  13. data/info/help.md +32 -18
  14. data/info/keybindings.md +43 -11
  15. data/info/welcome.md +4 -6
  16. data/lib/rufio/background_status.rb +7 -0
  17. data/lib/rufio/bookmark_controller.rb +0 -45
  18. data/lib/rufio/clipboard.rb +162 -0
  19. data/lib/rufio/command_locator.rb +49 -0
  20. data/lib/rufio/config.rb +7 -4
  21. data/lib/rufio/config_loader.rb +66 -11
  22. data/lib/rufio/csv_table_source.rb +125 -0
  23. data/lib/rufio/encoding_detector.rb +152 -0
  24. data/lib/rufio/file_operation_controller.rb +41 -0
  25. data/lib/rufio/file_preview.rb +92 -19
  26. data/lib/rufio/fuzzy_candidate_block.rb +131 -0
  27. data/lib/rufio/fuzzy_file_source.rb +180 -0
  28. data/lib/rufio/fuzzy_search.rb +327 -0
  29. data/lib/rufio/fuzzy_search_session.rb +426 -0
  30. data/lib/rufio/graphics_layer.rb +223 -0
  31. data/lib/rufio/health_checker.rb +27 -32
  32. data/lib/rufio/help_menu.rb +16 -10
  33. data/lib/rufio/image_preview_view.rb +421 -0
  34. data/lib/rufio/image_source.rb +158 -0
  35. data/lib/rufio/keybind_handler.rb +21 -34
  36. data/lib/rufio/kitty_graphics.rb +128 -0
  37. data/lib/rufio/native/aarch64-linux-gnu/librufio_zig.so +0 -0
  38. data/lib/rufio/native/aarch64-linux-musl/librufio_zig.so +0 -0
  39. data/lib/rufio/native/aarch64-macos/librufio_zig.dylib +0 -0
  40. data/lib/rufio/native/aarch64-windows/rufio_zig.dll +0 -0
  41. data/lib/rufio/native/x86_64-linux-gnu/librufio_zig.so +0 -0
  42. data/lib/rufio/native/x86_64-linux-musl/librufio_zig.so +0 -0
  43. data/lib/rufio/native/x86_64-macos/librufio_zig.dylib +0 -0
  44. data/lib/rufio/native/x86_64-windows/rufio_zig.dll +0 -0
  45. data/lib/rufio/native_content_search.rb +44 -8
  46. data/lib/rufio/native_image.rb +177 -0
  47. data/lib/rufio/native_scanner_zig.rb +39 -0
  48. data/lib/rufio/navigation_controller.rb +47 -16
  49. data/lib/rufio/preview_registry.rb +86 -0
  50. data/lib/rufio/preview_view.rb +363 -0
  51. data/lib/rufio/renderer.rb +8 -8
  52. data/lib/rufio/screen.rb +38 -1
  53. data/lib/rufio/search_prompt.rb +50 -0
  54. data/lib/rufio/search_screen.rb +680 -0
  55. data/lib/rufio/sheet_picker.rb +87 -0
  56. data/lib/rufio/sqlite_table_source.rb +205 -0
  57. data/lib/rufio/tab_mode_manager.rb +1 -3
  58. data/lib/rufio/table_cursor.rb +117 -0
  59. data/lib/rufio/table_document.rb +261 -0
  60. data/lib/rufio/table_layout.rb +231 -0
  61. data/lib/rufio/table_preview_view.rb +376 -0
  62. data/lib/rufio/table_query.rb +240 -0
  63. data/lib/rufio/table_source.rb +46 -0
  64. data/lib/rufio/terminal_graphics.rb +168 -0
  65. data/lib/rufio/terminal_ui.rb +74 -60
  66. data/lib/rufio/text_utils.rb +27 -0
  67. data/lib/rufio/ui_renderer.rb +380 -17
  68. data/lib/rufio/version.rb +1 -1
  69. data/lib/rufio/xlsx_table_source.rb +112 -0
  70. data/lib/rufio.rb +21 -1
  71. data/lib_zig/rufio_native/Makefile +21 -5
  72. data/lib_zig/rufio_native/build.zig +18 -0
  73. data/lib_zig/rufio_native/src/fuzzy.zig +520 -0
  74. data/lib_zig/rufio_native/src/fuzzy_test.zig +212 -0
  75. data/lib_zig/rufio_native/src/image.zig +525 -0
  76. data/lib_zig/rufio_native/src/image_bmp.zig +111 -0
  77. data/lib_zig/rufio_native/src/image_jpeg.zig +878 -0
  78. data/lib_zig/rufio_native/src/image_png.zig +561 -0
  79. data/lib_zig/rufio_native/src/image_resize.zig +170 -0
  80. data/lib_zig/rufio_native/src/image_test.zig +401 -0
  81. data/lib_zig/rufio_native/src/main.zig +28 -0
  82. data/lib_zig/rufio_native/src/search.zig +260 -64
  83. data/lib_zig/rufio_native/src/search_test.zig +35 -0
  84. data/lib_zig/rufio_native/src/testdata/alpha.png +0 -0
  85. data/lib_zig/rufio_native/src/testdata/gradient.bmp +0 -0
  86. data/lib_zig/rufio_native/src/testdata/gradient.jpg +0 -0
  87. data/lib_zig/rufio_native/src/testdata/gradient.png +0 -0
  88. data/retag.sh +4 -4
  89. metadata +44 -5
  90. data/lib/rufio/global_search.rb +0 -412
  91. data/lib/rufio/search_controller.rb +0 -92
  92. data/lib_zig/rufio_native/src/main.zig.sync +0 -205
@@ -0,0 +1,363 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'preview_registry'
4
+ require_relative 'table_source'
5
+ require_relative 'config_loader'
6
+ require_relative 'clipboard'
7
+ require_relative 'search_prompt'
8
+ require_relative 'image_source'
9
+
10
+ module Rufio
11
+ # プレビューモード中の「見え方」と「キー操作」をまとめた層。
12
+ #
13
+ # プレビュー種別を増やすときは、このプロトコルを満たすクラスを足して
14
+ # PreviewView.build が返すようにするだけでよい。NavigationController と
15
+ # UIRenderer はこのプロトコルしか知らない。
16
+ #
17
+ # row_offset 縦スクロール位置(先頭に表示するデータ行)
18
+ # reset 先頭に戻す(別のファイルを選び直したとき)
19
+ # handle_key プレビューフォーカス中のキー。扱わなければ false
20
+ # scroll/page マウスホイールなど、キー以外からのスクロール
21
+ # status_line ヘッダ下の 1 行に出す情報
22
+ # copy_target y でコピーする文字列(nil ならコピー対象なし)
23
+ # cursor セルカーソル(持たない種別は nil)
24
+ # visible_rows= 描画側が毎フレーム伝える表示行数(検索の追従に使う)
25
+ #
26
+ # `/` の検索は種別ごとに意味が変わる(テキストはファイル内検索、表は行の絞り込み)。
27
+ class PreviewView
28
+ BACK_HINT = 'y: copy path ESC: Back to files'
29
+
30
+ attr_reader :path
31
+
32
+ class << self
33
+ # @param path [String] プレビュー対象
34
+ # @return [PreviewView] 画像なら ImagePreviewView、表として読めれば
35
+ # TablePreviewView、それ以外は TextPreviewView
36
+ def build(path)
37
+ info = ImageSource.read(path)
38
+ return ImagePreviewView.new(info, path: path) if info
39
+
40
+ document = load_table(path)
41
+ document ? TablePreviewView.new(document, path: path) : TextPreviewView.new(path: path)
42
+ rescue StandardError
43
+ # プレビューのために操作を止めない。表として読めなければテキスト扱い
44
+ TextPreviewView.new(path: path)
45
+ end
46
+
47
+ private
48
+
49
+ def load_table(path)
50
+ return nil unless PreviewRegistry.table?(path) && File.file?(path)
51
+
52
+ settings = ConfigLoader.table_preview
53
+ limits = TableSource::Limits.new(
54
+ max_bytes: settings[:max_bytes],
55
+ max_rows: settings[:max_rows],
56
+ max_columns: settings[:max_columns]
57
+ )
58
+ # FilePreview と同じキャッシュを使うので、同じファイルを二度パースしない
59
+ PreviewRegistry.load_table(path, limits: limits, options: {
60
+ header: settings[:header],
61
+ delimiter: settings[:delimiter],
62
+ encoding: settings[:encoding]
63
+ })
64
+ rescue TableSource::ParseError
65
+ nil
66
+ end
67
+ end
68
+
69
+ def initialize(path: nil)
70
+ @path = path
71
+ @notice = nil
72
+ end
73
+
74
+ def row_offset
75
+ 0
76
+ end
77
+
78
+ # 先頭に戻す
79
+ def reset
80
+ false
81
+ end
82
+
83
+ # 種別ごとのキーを先に処理し、ここでは共通のキー(y: コピー)だけを扱う
84
+ def handle_key(key)
85
+ return false unless copy_key?(key)
86
+
87
+ perform_copy
88
+ end
89
+
90
+ def scroll(_delta)
91
+ false
92
+ end
93
+
94
+ def page(_delta)
95
+ false
96
+ end
97
+
98
+ def status_line
99
+ BACK_HINT
100
+ end
101
+
102
+ # y でコピーする文字列。表プレビューはセルの値、それ以外はファイルパス
103
+ def copy_target
104
+ @path && File.expand_path(@path)
105
+ end
106
+
107
+ def cursor
108
+ nil
109
+ end
110
+
111
+ # 描画側が毎フレーム伝える表示行数(既定値のままでも破綻しないよう無視できる)
112
+ def visible_rows=(_rows); end
113
+
114
+ # `/` の検索語(検索していなければ nil)
115
+ def search_query
116
+ nil
117
+ end
118
+
119
+ def search_prompt_open?
120
+ false
121
+ end
122
+
123
+ # 描画側が求めた一致行(折り返し後の行番号)
124
+ def search_matches
125
+ []
126
+ end
127
+
128
+ def current_match
129
+ nil
130
+ end
131
+
132
+ def apply_matches(_indices); end
133
+
134
+ def table?
135
+ false
136
+ end
137
+
138
+ # 端末のグラフィックス機能で表示する種別か
139
+ def image?
140
+ false
141
+ end
142
+
143
+ # 描画側が毎フレーム伝えるペインの実ピクセル(画像プレビューだけが使う)
144
+ def target_pixels=(_size); end
145
+
146
+ # 画像のデコード待ちか
147
+ def loading?
148
+ false
149
+ end
150
+
151
+ # ビューを捨てるときの後片付け(デコード中のジョブなど)
152
+ def release
153
+ false
154
+ end
155
+
156
+ # 複数シートを持つ種別だけが 0 以外を返す
157
+ def sheet_index
158
+ 0
159
+ end
160
+
161
+ protected
162
+
163
+ # コピー結果などの短い通知。次の操作まで表示し続ける
164
+ def notice_text
165
+ @notice
166
+ end
167
+
168
+ def notice(text)
169
+ @notice = text
170
+ end
171
+
172
+ def clear_notice
173
+ @notice = nil
174
+ end
175
+
176
+ def copy_key?(key)
177
+ key == ConfigLoader.keybinds[:copy_to_clipboard]
178
+ end
179
+
180
+ def perform_copy
181
+ copy_text(copy_target)
182
+ end
183
+
184
+ # @param label [String, nil] 通知に出す対象名("row" など)
185
+ def copy_text(text, label: nil)
186
+ if text.nil? || text.to_s.empty?
187
+ notice('nothing to copy')
188
+ return true
189
+ end
190
+
191
+ method = Clipboard.copy(text)
192
+ notice(method ? "#{copied_label(text, label)} (#{method})" : 'clipboard unavailable')
193
+ true
194
+ end
195
+
196
+ # グリッドは改行を空白に畳んで表示するため、コピーした内容が複数行なら
197
+ # 画面の見た目と違うことが分かるよう行数を出す
198
+ def copied_label(text, label)
199
+ copied = ['copied', label].compact.join(' ')
200
+ lines = text.to_s.sub(/[\r\n]+\z/, '').lines.length
201
+ lines > 1 ? "#{copied}, #{lines} lines" : copied
202
+ end
203
+ end
204
+
205
+ # 従来のテキスト/コードプレビュー(行単位のスクロール)と、`/` のファイル内検索。
206
+ #
207
+ # 一致行の位置は折り返し後の行番号で扱う。折り返しはペインの幅に依存するため、
208
+ # 一致行を求めるのは描画側(UIRenderer)で、apply_matches / visible_rows= で受け取る。
209
+ class TextPreviewView < PreviewView
210
+ PAGE_ROWS = 20
211
+ # 検索中でないときのヒント
212
+ SCROLL_HINT = 'j/k: scroll /: search y: copy path ESC: back'
213
+ MATCH_HINT = 'n/N: next/prev ESC: clear'
214
+ # 一致行を画面に出すとき、上に残す行数
215
+ MATCH_MARGIN = 2
216
+
217
+ attr_reader :row_offset, :search_query, :search_matches
218
+
219
+ def initialize(path: nil)
220
+ super
221
+ @row_offset = 0
222
+ @visible_rows = PAGE_ROWS
223
+ clear_search
224
+ end
225
+
226
+ def reset
227
+ @row_offset = 0
228
+ clear_search
229
+ clear_notice
230
+ true
231
+ end
232
+
233
+ def visible_rows=(rows)
234
+ @visible_rows = [rows.to_i, 1].max
235
+ end
236
+
237
+ def search_prompt_open?
238
+ !@prompt.nil?
239
+ end
240
+
241
+ # 現在の一致行(折り返し後の行番号)。一致がなければ nil
242
+ def current_match
243
+ @search_matches[@match_index]
244
+ end
245
+
246
+ # 描画側が求めた一致行を受け取る。入力直後は現在位置以降の最初の一致へ飛ぶ
247
+ def apply_matches(indices)
248
+ @search_matches = Array(indices)
249
+ if @search_matches.empty?
250
+ @match_index = 0
251
+ @pending_jump = false
252
+ return
253
+ end
254
+
255
+ if @pending_jump
256
+ @pending_jump = false
257
+ @match_index = @search_matches.index { |index| index >= @row_offset } || 0
258
+ end
259
+ @match_index = @match_index.clamp(0, @search_matches.length - 1)
260
+ ensure_match_visible
261
+ end
262
+
263
+ def handle_key(key)
264
+ return handle_prompt_key(key) if search_prompt_open?
265
+ return open_search_prompt if key == ConfigLoader.keybinds[:search]
266
+ return next_match(1) if key == 'n' && searching?
267
+ return next_match(-1) if key == 'N' && searching?
268
+ return clear_search if key == "\e" && searching?
269
+
270
+ case key
271
+ when 'j', "\e[B" then scroll(1)
272
+ when 'k', "\e[A" then scroll(-1)
273
+ when "\x04" then page(1) # Ctrl+D
274
+ when "\x15" then page(-1) # Ctrl+U
275
+ else super
276
+ end
277
+ end
278
+
279
+ # 入力中は `/query_`、確定後は一致件数と操作ヒントを出す
280
+ def status_line
281
+ return "/#{@prompt.query}_" if search_prompt_open?
282
+ return notice_text if notice_text
283
+ return SCROLL_HINT unless searching?
284
+
285
+ found = @search_matches.empty? ? 'no matches' : "#{@match_index + 1}/#{@search_matches.length}"
286
+ ["\"#{@search_query}\" #{found}", MATCH_HINT].join(' | ')
287
+ end
288
+
289
+ def scroll(delta)
290
+ clear_notice
291
+ @row_offset = [@row_offset + delta, 0].max
292
+ true
293
+ end
294
+
295
+ def page(delta)
296
+ scroll(delta * PAGE_ROWS)
297
+ end
298
+
299
+ private
300
+
301
+ def searching?
302
+ !@search_query.nil?
303
+ end
304
+
305
+ def open_search_prompt
306
+ clear_notice
307
+ @offset_before_search = @row_offset
308
+ @prompt = SearchPrompt.new
309
+ @search_query = ''
310
+ @search_matches = []
311
+ @match_index = 0
312
+ @pending_jump = true
313
+ true
314
+ end
315
+
316
+ # 入力中のキーはすべて入力欄で扱う(スクロールには使わない)
317
+ def handle_prompt_key(key)
318
+ case @prompt.handle_key(key)
319
+ when :changed
320
+ @search_query = @prompt.query
321
+ @pending_jump = true
322
+ when :submitted
323
+ @prompt = nil
324
+ clear_search if @search_query.to_s.empty?
325
+ when :cancelled
326
+ @prompt = nil
327
+ @row_offset = @offset_before_search || @row_offset
328
+ clear_search
329
+ end
330
+ true
331
+ end
332
+
333
+ def clear_search
334
+ @prompt = nil
335
+ @search_query = nil
336
+ @search_matches = []
337
+ @match_index = 0
338
+ @pending_jump = false
339
+ true
340
+ end
341
+
342
+ # n / N: 次 / 前の一致へ。端では反対側へ回る
343
+ def next_match(step)
344
+ return true if @search_matches.empty?
345
+
346
+ @match_index = (@match_index + step) % @search_matches.length
347
+ ensure_match_visible
348
+ true
349
+ end
350
+
351
+ # 一致行が画面の外なら、少し上に余白を残して表示する
352
+ def ensure_match_visible
353
+ match = current_match
354
+ return if match.nil?
355
+ return if match >= @row_offset && match < @row_offset + @visible_rows
356
+
357
+ @row_offset = [match - MATCH_MARGIN, 0].max
358
+ end
359
+ end
360
+ end
361
+
362
+ require_relative 'table_preview_view'
363
+ require_relative 'image_preview_view'
@@ -22,20 +22,20 @@ module Rufio
22
22
  # Render the screen with differential updates
23
23
  #
24
24
  # @param screen [Screen] The back buffer to render
25
- # @return [Boolean] true if rendering was performed, false if skipped
25
+ # @return [Array<Integer>] 実際に書き換えた行番号(何も書かなければ空)。
26
+ # 画像(GraphicsLayer)は、自分の矩形と交差する行が書き換えられたかを見て
27
+ # 送り直すかを決める
26
28
  def render(screen)
27
29
  # CPU最適化: Dirty rowsが空の場合は完全にスキップ
28
30
  dirty = screen.dirty_rows
29
- if dirty.empty?
30
- return false
31
- end
31
+ return [] if dirty.empty?
32
32
 
33
33
  # Phase1: Only process dirty rows (rows that have changed)
34
34
  # 全dirty rowsの出力を1つのバッファに積んでから単一の write() で書き出す。
35
35
  # STDOUT sync=true 環境で print を行ごとに呼ぶと各行で即座にフラッシュされ
36
36
  # 中間状態が表示されてちらつきが発生するため、アトミックな更新を保証する。
37
37
  buf = String.new("")
38
- rendered_count = 0
38
+ rewritten = []
39
39
  dirty.each do |y|
40
40
  line = screen.row(y)
41
41
  next if line == @front[y] # Skip if content is actually the same
@@ -43,19 +43,19 @@ module Rufio
43
43
  # Move cursor to line y (1-indexed) and buffer the line
44
44
  buf << "\e[#{y + 1};1H#{line}"
45
45
  @front[y] = line
46
- rendered_count += 1
46
+ rewritten << y
47
47
  end
48
48
 
49
49
  # Phase1: Clear dirty tracking after rendering
50
50
  screen.clear_dirty
51
51
 
52
52
  # 単一の write() でアトミックに出力し、その後 flush する
53
- if rendered_count > 0
53
+ unless rewritten.empty?
54
54
  @output.write(buf)
55
55
  @output.flush
56
56
  end
57
57
 
58
- true
58
+ rewritten
59
59
  end
60
60
 
61
61
  # Resize the front buffer
data/lib/rufio/screen.rb CHANGED
@@ -36,6 +36,26 @@ module Rufio
36
36
  @cells = Array.new(height) { Array.new(width) { Cell.new(' ', nil, nil, 1) } }
37
37
  @overlay_cells = nil # オーバーレイレイヤー(ダイアログ用)
38
38
  @dirty_rows = Set.new # Phase1: Dirty row tracking
39
+ @reserved = nil # 画像が占有している矩形(文字を書かせない)
40
+ end
41
+
42
+ # 画像(端末のグラフィックス機能)が占有する矩形を確保する。
43
+ # 確保した範囲への put / put_string は無視するので、通知やフッタが
44
+ # 割り込んで画像を削ることがない。clear で解除される。
45
+ def reserve(x, y, width, height)
46
+ return if width.to_i <= 0 || height.to_i <= 0
47
+
48
+ @reserved = [x.to_i, y.to_i, x.to_i + width.to_i - 1, y.to_i + height.to_i - 1]
49
+ end
50
+
51
+ def clear_reserved
52
+ @reserved = nil
53
+ end
54
+
55
+ def reserved?(x, y)
56
+ return false unless @reserved
57
+
58
+ x >= @reserved[0] && x <= @reserved[2] && y >= @reserved[1] && y <= @reserved[3]
39
59
  end
40
60
 
41
61
  # Put a single character at (x, y) with optional color
@@ -48,6 +68,14 @@ module Rufio
48
68
  # @param width [Integer, nil] Display width (auto-detected if not provided)
49
69
  def put(x, y, char, fg: nil, bg: nil, width: nil)
50
70
  return if out_of_bounds?(x, y)
71
+ return if reserved?(x, y)
72
+
73
+ # 制御文字は端末を操作してしまうため、バッファに入れる前に空白へ置き換える
74
+ # (幅は置き換え後の文字で数え直す)
75
+ if TextUtils.control_char?(char)
76
+ char = TextUtils::CONTROL_CHAR_REPLACEMENT
77
+ width = nil
78
+ end
51
79
 
52
80
  # Phase1: Width is calculated once here (not in rendering loop)
53
81
  char_width = width || TextUtils.display_width(char)
@@ -87,6 +115,9 @@ module Rufio
87
115
  # Phase1: ANSI stripping only once (minimal processing)
88
116
  # Only strip if the string contains ANSI codes
89
117
  clean_str = str.include?("\e") ? ColorHelper.strip_ansi(str) : str
118
+ # strip_ansi は色指定(\e[...m)しか落とさないので、残る制御文字をここで無害化する。
119
+ # ファイル名・ファイル内容・外部コマンドの出力が端末を操作するのを防ぐ最後の関門
120
+ clean_str = TextUtils.sanitize_for_display(clean_str)
90
121
 
91
122
  current_x = x
92
123
  clean_str.each_char do |char|
@@ -156,7 +187,10 @@ module Rufio
156
187
  end
157
188
 
158
189
  # Clear the entire screen
159
- def clear
190
+ #
191
+ # @param mark_dirty [Boolean] true で全行を dirty にする。毎フレーム全体を描き直す
192
+ # 画面(G mode・内蔵検索)で、描かなくなった行も Renderer に消させるために使う
193
+ def clear(mark_dirty: false)
160
194
  @cells.each do |row|
161
195
  row.each do |cell|
162
196
  cell.char = ' '
@@ -167,6 +201,9 @@ module Rufio
167
201
  end
168
202
  # Phase1: Clear dirty rows after full clear
169
203
  @dirty_rows.clear
204
+ @height.times { |y| @dirty_rows.add(y) } if mark_dirty
205
+ # 確保は毎フレーム描画側が申告し直す
206
+ @reserved = nil
170
207
  end
171
208
 
172
209
  # Phase1: Get dirty rows (rows that have been modified since last clear)
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rufio
4
+ # プレビューモードの `/` で出す 1 行の入力欄。
5
+ #
6
+ # 文字の編集だけを担い、検索の実行も表示もしない。利用側(PreviewView の各種別)は
7
+ # handle_key の戻り値で「入力が変わった / 確定した / 取り消した」を判断する。
8
+ class SearchPrompt
9
+ # 入力を取り消す/確定するキー
10
+ CANCEL = "\e"
11
+ SUBMIT = ["\r", "\n"].freeze
12
+ BACKSPACE = ["\u007F", "\b"].freeze
13
+ CLEAR = "\x15" # Ctrl+U
14
+
15
+ attr_reader :query
16
+
17
+ def initialize(query = '')
18
+ @query = query.to_s.dup
19
+ end
20
+
21
+ # @return [Symbol] :changed(入力が変わった)/ :submitted / :cancelled / :ignored
22
+ def handle_key(key)
23
+ return :submitted if SUBMIT.include?(key)
24
+ return :cancelled if key == CANCEL
25
+
26
+ if BACKSPACE.include?(key)
27
+ @query = @query[0...-1] || ''
28
+ return :changed
29
+ end
30
+
31
+ if key == CLEAR
32
+ @query = ''
33
+ return :changed
34
+ end
35
+
36
+ return :ignored unless printable?(key)
37
+
38
+ @query += key
39
+ :changed
40
+ end
41
+
42
+ private
43
+
44
+ # 制御文字・エスケープシーケンスは入力に混ぜない(検索画面と同じ判定)
45
+ def printable?(key)
46
+ key.is_a?(String) && !key.empty? && key.valid_encoding? &&
47
+ key.each_codepoint.all? { |code| code >= 32 && code != 127 }
48
+ end
49
+ end
50
+ end