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,240 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rufio
4
+ # 表プレビューの `/` で入力する絞り込み条件。SQL を書かずに済む書き方で受け取り、
5
+ # SQLite では WHERE 句へ、CSV / Excel では Ruby 側の判定へ同じ条件を渡す。
6
+ #
7
+ # price > 100, name a% → price > 100 AND name LIKE 'a%'
8
+ # apple → どれかの列に apple を含む
9
+ #
10
+ # 約束事:
11
+ # - カンマで条件を区切る(引用符の中のカンマは区切らない)
12
+ # - 先頭が列名(大文字小文字は無視)か列ラベル(A, B, ...)ならその列の条件
13
+ # - 比較演算子(= != <> >= <= > <)があればそのまま、無ければ LIKE
14
+ # - LIKE の値に % / _ が無ければ前後に % を付ける(部分一致)。引用符で囲めばそのまま
15
+ # - 列を書かなければ全列の OR
16
+ #
17
+ # SQL は組み立てない(それは SqliteTableSource の仕事)。ここで持つのは条件だけで、
18
+ # 列名は必ず実在する列の名前に解決してから渡す。
19
+ class TableQuery
20
+ # 入力が条件として読めなかったとき
21
+ class Error < StandardError; end
22
+
23
+ # @!attribute column
24
+ # @return [String, nil] 列名(nil なら全列)
25
+ # @!attribute operator
26
+ # @return [String] 'LIKE' か比較演算子
27
+ # @!attribute value
28
+ # @return [String] 比較する値(LIKE ならパターン)
29
+ Condition = Struct.new(:column, :operator, :value, keyword_init: true)
30
+
31
+ # 長いものから順に見る(>= を > と = に分けて読まないため)
32
+ OPERATORS = ['>=', '<=', '!=', '<>', '=', '>', '<'].freeze
33
+ LIKE = 'LIKE'
34
+ WILDCARDS = ['%', '_'].freeze
35
+
36
+ attr_reader :text, :conditions, :columns
37
+
38
+ class << self
39
+ # @param text [String, nil] 入力
40
+ # @param columns [Array<String>] 表の列名(見出し行。無ければ空配列)
41
+ # @return [TableQuery, nil] 空の入力なら nil
42
+ def parse(text, columns)
43
+ source = text.to_s
44
+ return nil if source.strip.empty?
45
+
46
+ names = Array(columns).map { |name| name.to_s }
47
+ conditions = split_terms(source).map { |term| parse_term(term, names) }.compact
48
+ return nil if conditions.empty?
49
+
50
+ new(text: source.strip, conditions: conditions, columns: names)
51
+ end
52
+
53
+ private
54
+
55
+ # カンマで分ける。引用符の中のカンマは区切りにしない
56
+ def split_terms(text)
57
+ terms = []
58
+ current = +''
59
+ quote = nil
60
+ text.each_char do |char|
61
+ if quote
62
+ quote = nil if char == quote
63
+ current << char
64
+ elsif char == '"' || char == "'"
65
+ quote = char
66
+ current << char
67
+ elsif char == ','
68
+ terms << current
69
+ current = +''
70
+ else
71
+ current << char
72
+ end
73
+ end
74
+ terms << current
75
+ terms.map(&:strip).reject(&:empty?)
76
+ end
77
+
78
+ def parse_term(term, columns)
79
+ column, rest = split_column(term, columns)
80
+ operator, value = split_operator(rest)
81
+ raise Error, 'missing value after the operator' if value.strip.empty?
82
+
83
+ # 引用符はカンマや空白を含む値をまとめるためのもので、照合の仕方は変えない
84
+ literal = unquote(value.strip)
85
+ if operator == LIKE && WILDCARDS.none? { |wildcard| literal.include?(wildcard) }
86
+ literal = "%#{literal}%"
87
+ end
88
+ Condition.new(column: column, operator: operator, value: literal)
89
+ end
90
+
91
+ # 先頭が列名(または列ラベル)なら、その列と残りを返す。
92
+ # 列が無ければ [nil, 項全体](全列検索)
93
+ def split_column(term, columns)
94
+ candidates = column_candidates(columns)
95
+ # 長い名前を先に試す("name" と "name kana" のような列があっても取り違えない)
96
+ candidates.sort_by { |name, _| -name.length }.each do |name, resolved|
97
+ next unless term.downcase.start_with?(name)
98
+
99
+ rest = term[name.length..].to_s
100
+ # 列名の直後は空白か演算子でなければ列指定とみなさない("apples" の "a" 等)
101
+ next unless rest.empty? || rest.start_with?(' ', "\t") || OPERATORS.any? { |op| rest.start_with?(op) }
102
+
103
+ return [resolved, rest]
104
+ end
105
+ [nil, term]
106
+ end
107
+
108
+ # 探す名前(小文字) => 実在する列名
109
+ def column_candidates(columns)
110
+ candidates = {}
111
+ columns.each_with_index do |name, index|
112
+ label = column_label(index)
113
+ candidates[label.downcase] = name.to_s.empty? ? label : name
114
+ next if name.to_s.empty?
115
+
116
+ candidates[name.downcase] = name
117
+ end
118
+ candidates
119
+ end
120
+
121
+ # A, B, ... Z, AA(TableSheet#column_label と同じ規則)
122
+ def column_label(index)
123
+ label = +''
124
+ number = index
125
+ loop do
126
+ label = ((number % 26) + 65).chr + label
127
+ number = number / 26 - 1
128
+ break if number.negative?
129
+ end
130
+ label
131
+ end
132
+
133
+ def split_operator(rest)
134
+ text = rest.to_s.strip
135
+ operator = OPERATORS.find { |candidate| text.start_with?(candidate) }
136
+ return [LIKE, text] unless operator
137
+
138
+ [operator, text[operator.length..].to_s]
139
+ end
140
+
141
+ # 両端の引用符を外す
142
+ def unquote(value)
143
+ %w[" '].each do |quote|
144
+ return value[1..-2].to_s if value.length >= 2 && value.start_with?(quote) && value.end_with?(quote)
145
+ end
146
+ value
147
+ end
148
+ end
149
+
150
+ def initialize(text:, conditions:, columns:)
151
+ @text = text
152
+ @conditions = conditions
153
+ @columns = columns
154
+ end
155
+
156
+ def to_s
157
+ @text
158
+ end
159
+
160
+ # CSV / Excel のための判定。すべての条件を満たす行だけを通す
161
+ # @param row [Array<String>] 1 行
162
+ # @param columns [Array<String>] 列名(見出し行)
163
+ def matches?(row, columns = @columns)
164
+ cells = Array(row)
165
+ @conditions.all? { |condition| condition_matches?(condition, cells, Array(columns)) }
166
+ end
167
+
168
+ private
169
+
170
+ def condition_matches?(condition, cells, columns)
171
+ return any_column_matches?(condition, cells) if condition.column.nil?
172
+
173
+ index = columns.index(condition.column)
174
+ return false if index.nil?
175
+
176
+ cell_matches?(condition, cells[index])
177
+ end
178
+
179
+ def any_column_matches?(condition, cells)
180
+ cells.any? { |cell| cell_matches?(condition, cell) }
181
+ end
182
+
183
+ def cell_matches?(condition, cell)
184
+ return false if cell.nil?
185
+
186
+ text = cell.to_s
187
+ return like?(text, condition.value) if condition.operator == LIKE
188
+
189
+ compare(text, condition.value, condition.operator)
190
+ end
191
+
192
+ # SQLite の LIKE と同じく、% は 0 文字以上、_ は 1 文字、大文字小文字は無視する
193
+ def like?(text, pattern)
194
+ Regexp.new("\\A#{like_pattern(pattern)}\\z", Regexp::IGNORECASE).match?(text)
195
+ end
196
+
197
+ def like_pattern(pattern)
198
+ pattern.each_char.map do |char|
199
+ case char
200
+ when '%' then '.*'
201
+ when '_' then '.'
202
+ else Regexp.escape(char)
203
+ end
204
+ end.join
205
+ end
206
+
207
+ # 両方が数値として読めれば数値で比べ、そうでなければ文字列で比べる
208
+ # ("80" と "100" を文字列で比べると大小が逆になる)
209
+ def compare(text, value, operator)
210
+ left = numeric(text)
211
+ right = numeric(value)
212
+ if left && right
213
+ apply(left, right, operator)
214
+ else
215
+ apply(text.downcase, value.downcase, operator)
216
+ end
217
+ end
218
+
219
+ def apply(left, right, operator)
220
+ case operator
221
+ when '=' then left == right
222
+ when '!=', '<>' then left != right
223
+ when '>' then (left <=> right) == 1
224
+ when '<' then (left <=> right) == -1
225
+ when '>=' then (left <=> right) >= 0
226
+ when '<=' then (left <=> right) <= 0
227
+ else false
228
+ end
229
+ end
230
+
231
+ def numeric(text)
232
+ value = text.to_s.strip
233
+ return nil if value.empty?
234
+
235
+ Float(value)
236
+ rescue ArgumentError, TypeError
237
+ nil
238
+ end
239
+ end
240
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'table_document'
4
+ require_relative 'config_loader'
5
+
6
+ module Rufio
7
+ # 表として読める形式の共通インターフェース。
8
+ #
9
+ # 形式を追加するときに実装するのは supports? と open の 2 つだけで、
10
+ # 描画(TableLayout / UIRenderer)とキー処理には手を入れない。
11
+ #
12
+ # class SqliteTableSource < TableSource
13
+ # def self.supports?(path) = ...
14
+ # def self.open(path, limits:, options: {}) = TableDocument.new(sheets: [...])
15
+ # end
16
+ class TableSource
17
+ # 表として解釈できなかったとき(テキストプレビューへフォールバックする)
18
+ class ParseError < StandardError; end
19
+
20
+ # 読み込みの上限。形式に依存しない共通の歯止め。
21
+ # 既定値は設定(TABLE_PREVIEW)から取り、上限の定義を 1 箇所にする。
22
+ Limits = Struct.new(:max_bytes, :max_rows, :max_columns, keyword_init: true) do
23
+ def self.default
24
+ settings = ConfigLoader.table_preview
25
+ new(max_bytes: settings[:max_bytes], max_rows: settings[:max_rows],
26
+ max_columns: settings[:max_columns])
27
+ end
28
+ end
29
+
30
+ class << self
31
+ # @param _path [String]
32
+ # @return [Boolean] この形式として扱えるか
33
+ def supports?(_path)
34
+ false
35
+ end
36
+
37
+ # @param _path [String]
38
+ # @param limits [Limits]
39
+ # @param options [Hash] 形式ごとの設定(header / delimiter / encoding など)
40
+ # @return [TableDocument]
41
+ def open(_path, limits: Limits.default, options: {})
42
+ raise NotImplementedError, "#{self} must implement open"
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,168 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rbconfig'
4
+ require_relative 'kitty_graphics'
5
+
6
+ module Rufio
7
+ # 端末が画像表示(Kitty グラフィックスプロトコル)に対応しているかを 1 回だけ判定する。
8
+ #
9
+ # 判定は問い合わせ(`a=q`)+ Primary DA(`\e[c`)を続けて送り、応答を読む方式。
10
+ # 非対応の端末でも DA は返るため、無応答で待たされない。あわせて `CSI 16t` で
11
+ # 1 セルのピクセル寸法も尋ねる(取れなければ既定値を使う)。
12
+ #
13
+ # 起動直後(raw モードにした後・メインループに入る前)に 1 回だけ呼ぶ。
14
+ module TerminalGraphics
15
+ # 問い合わせに使う画像 ID(他アプリの応答と混ざらないようにする)
16
+ QUERY_ID = 31
17
+ # 応答を待つ上限(秒)
18
+ DEFAULT_TIMEOUT = 0.2
19
+ # 端末応答の読み込み上限(異常な出力でのメモリ枯渇を防ぐ)
20
+ MAX_RESPONSE_BYTES = 4096
21
+ # セルのピクセル寸法が分からない端末での仮の値
22
+ DEFAULT_CELL_PIXELS = [8, 16].freeze
23
+ # 異常な端末応答で極端なセル寸法が入るのを防ぐ上限
24
+ MAX_CELL_PIXELS = 1024
25
+ # Primary DA の応答(これが来たら読み終わり)
26
+ DA_PATTERN = /\e\[\?[0-9;]*c/.freeze
27
+ # CSI 16t の応答: \e[6;<セルの高さ>;<セルの幅>t
28
+ CELL_PATTERN = /\e\[6;(\d+);(\d+)t/.freeze
29
+
30
+ @protocol = nil
31
+ @cell_pixels = DEFAULT_CELL_PIXELS
32
+ @cell_pixels_known = false
33
+
34
+ class << self
35
+ # @return [Symbol] :kitty / :none(未判定なら :none)
36
+ def protocol
37
+ @protocol || :none
38
+ end
39
+
40
+ def supported?
41
+ protocol == :kitty
42
+ end
43
+
44
+ # @return [Array(Integer, Integer)] 1 セルの [幅, 高さ](ピクセル)
45
+ def cell_pixels
46
+ @cell_pixels || DEFAULT_CELL_PIXELS
47
+ end
48
+
49
+ # 端末から実測したセル寸法か(既定値 8x16 の仮置きなら false)
50
+ def cell_pixels_known?
51
+ @cell_pixels_known == true
52
+ end
53
+
54
+ def detected?
55
+ !@protocol.nil?
56
+ end
57
+
58
+ # 判定して結果を覚える。2 回目以降は何も送らない
59
+ # @return [Symbol] protocol
60
+ def detect!(input: $stdin, output: $stdout, env: ENV, timeout: DEFAULT_TIMEOUT)
61
+ return protocol if detected?
62
+
63
+ @protocol = :none
64
+ @cell_pixels = DEFAULT_CELL_PIXELS
65
+ @cell_pixels_known = false
66
+ return @protocol unless enabled?(env)
67
+
68
+ response = ask(input, output, timeout)
69
+ return @protocol if response.nil?
70
+
71
+ @protocol = KittyGraphics.ok?(response, id: QUERY_ID) ? :kitty : :none
72
+ measured = parse_cell_pixels(response) || native_cell_pixels(output)
73
+ if measured
74
+ @cell_pixels = measured
75
+ @cell_pixels_known = true
76
+ end
77
+ @protocol
78
+ end
79
+
80
+ # テストと再判定(端末が変わったとき)のために状態を捨てる
81
+ def reset!
82
+ @protocol = nil
83
+ @cell_pixels = DEFAULT_CELL_PIXELS
84
+ @cell_pixels_known = false
85
+ end
86
+
87
+ private
88
+
89
+ # CSI 16t に応答しない端末では、ネイティブの TIOCGWINSZ からセル寸法を補う
90
+ def native_cell_pixels(output)
91
+ return nil unless output.respond_to?(:tty?) && output.tty?
92
+ return nil unless defined?(NativeImage)
93
+
94
+ NativeImage.cell_pixels
95
+ rescue StandardError
96
+ nil
97
+ end
98
+
99
+ # 設定より手前の足切り。Windows の端末と tmux / screen の中では既定で使わない
100
+ def enabled?(env)
101
+ return false if env['RUFIO_IMAGE_PREVIEW'] == '0'
102
+ return false if windows?
103
+ # tmux / screen は DCS パススルーの設定が必要なので、既定では無効(Phase 4 で設定化)
104
+ return false if env['TMUX'] || env['STY']
105
+
106
+ true
107
+ end
108
+
109
+ def windows?
110
+ RbConfig::CONFIG['host_os'].to_s.match?(/mingw|mswin/)
111
+ end
112
+
113
+ # 問い合わせを送り、Primary DA の応答が来るか時間切れになるまで読む
114
+ def ask(input, output, timeout)
115
+ output.write(KittyGraphics.query(id: QUERY_ID))
116
+ output.write("\e[16t")
117
+ output.write("\e[c")
118
+ output.flush
119
+ read_response(input, timeout)
120
+ rescue IOError, SystemCallError
121
+ nil
122
+ end
123
+
124
+ def read_response(input, timeout)
125
+ return '' if input.nil?
126
+
127
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
128
+ buffer = +''
129
+ loop do
130
+ remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
131
+ break if remaining <= 0
132
+
133
+ chunk = read_chunk(input, remaining)
134
+ break if chunk.nil? || chunk.empty?
135
+
136
+ buffer << chunk
137
+ break if buffer.bytesize >= MAX_RESPONSE_BYTES || buffer.match?(DA_PATTERN)
138
+ end
139
+ buffer
140
+ end
141
+
142
+ def read_chunk(input, timeout)
143
+ input.read_nonblock(1024)
144
+ rescue IO::WaitReadable
145
+ return nil unless input.respond_to?(:wait_readable) && input.wait_readable(timeout)
146
+
147
+ begin
148
+ input.read_nonblock(1024)
149
+ rescue StandardError
150
+ nil
151
+ end
152
+ rescue EOFError, IOError, SystemCallError, NoMethodError
153
+ nil
154
+ end
155
+
156
+ def parse_cell_pixels(response)
157
+ match = response.match(CELL_PATTERN)
158
+ return nil unless match
159
+
160
+ height = match[1].to_i
161
+ width = match[2].to_i
162
+ return nil unless width.between?(1, MAX_CELL_PIXELS) && height.between?(1, MAX_CELL_PIXELS)
163
+
164
+ [width, height]
165
+ end
166
+ end
167
+ end
168
+ end