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
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'csv'
|
|
4
|
+
require_relative 'table_cursor'
|
|
5
|
+
require_relative 'sheet_picker'
|
|
6
|
+
require_relative 'table_query'
|
|
7
|
+
require_relative 'text_utils'
|
|
8
|
+
require_relative 'config_loader'
|
|
9
|
+
|
|
10
|
+
module Rufio
|
|
11
|
+
# 行列(グリッド)プレビュー。セルカーソルを持ち、hjkl で移動する。
|
|
12
|
+
# 形式(CSV / 将来の Excel・SQLite)には依存せず、TableDocument だけを見る。
|
|
13
|
+
class TablePreviewView < PreviewView
|
|
14
|
+
MOVE_HINT = 'hjkl: move Space: col /: filter y: copy ESC: back'
|
|
15
|
+
SHEET_HINT = '[ ]: sheet t: list'
|
|
16
|
+
PICKER_HINT = 'j/k: move Enter: open ESC: cancel'
|
|
17
|
+
# シート一覧を開くキー(`/` はプレビュー内検索に使うため `t`(table))
|
|
18
|
+
LIST_KEY = 't'
|
|
19
|
+
# 一覧で現在表示中のシートに付ける印(Windows の端末でも崩れない ASCII)
|
|
20
|
+
CURRENT_MARK = '>'
|
|
21
|
+
# ステータス行に並べる選択カラム名の上限(残りは +n と出す)
|
|
22
|
+
SELECTED_LABEL_LIMIT = 4
|
|
23
|
+
|
|
24
|
+
attr_reader :document, :cursor, :sheet_index, :picker
|
|
25
|
+
|
|
26
|
+
def initialize(document, sheet_index: 0, path: nil)
|
|
27
|
+
super(path: path)
|
|
28
|
+
@document = document
|
|
29
|
+
@sheet_index = sheet_index
|
|
30
|
+
@cursor = TableCursor.new(sheet)
|
|
31
|
+
@picker = nil
|
|
32
|
+
@selected_columns = []
|
|
33
|
+
clear_filter
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# 表示しているシート(`/` で絞り込んでいればその結果)
|
|
37
|
+
def sheet
|
|
38
|
+
@filtered_sheet || @document.sheet(@sheet_index)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# `/` の入力中はその文字列、確定後は適用中の条件(どちらでもなければ nil)
|
|
42
|
+
def search_query
|
|
43
|
+
@prompt ? @prompt.query : @filter_text
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def search_prompt_open?
|
|
47
|
+
!@prompt.nil?
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Space で選んだカラム(列の順に並べる)
|
|
51
|
+
def selected_columns
|
|
52
|
+
@selected_columns.sort
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def column_selected?(index)
|
|
56
|
+
@selected_columns.include?(index)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def table?
|
|
60
|
+
true
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def row_offset
|
|
64
|
+
@cursor.row_offset
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def reset
|
|
68
|
+
close_sheet_list
|
|
69
|
+
clear_filter
|
|
70
|
+
clear_columns
|
|
71
|
+
@cursor = TableCursor.new(sheet)
|
|
72
|
+
clear_notice
|
|
73
|
+
true
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# シート(SQLite のテーブル / Excel のシート)一覧を開いているか
|
|
77
|
+
def picker_open?
|
|
78
|
+
!@picker.nil?
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# 一覧の見出し("Tables (3)" / "Sheets (2)")
|
|
82
|
+
def picker_title
|
|
83
|
+
return nil unless @picker
|
|
84
|
+
|
|
85
|
+
"#{@document.sheet_label} (#{@picker.count})"
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# 一覧に描く行。反転色をどう出すかは描画側(UIRenderer)が決める
|
|
89
|
+
# @return [Array<Hash>] { text: String, selected: Boolean }
|
|
90
|
+
def picker_rows
|
|
91
|
+
return [] unless @picker
|
|
92
|
+
|
|
93
|
+
@picker.visible.map do |index, name|
|
|
94
|
+
mark = index == @sheet_index ? CURRENT_MARK : ' '
|
|
95
|
+
# シート名・テーブル名はファイルやデータベース由来なので無害化する
|
|
96
|
+
{ text: "#{mark} #{TextUtils.sanitize_for_display(name.to_s)}",
|
|
97
|
+
selected: index == @picker.index }
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# 一覧を開く。選ぶものがない(シートが 1 枚だけの)表では何もしない
|
|
102
|
+
def open_sheet_list
|
|
103
|
+
return false unless @document.sheet_count > 1
|
|
104
|
+
|
|
105
|
+
clear_notice
|
|
106
|
+
@picker = SheetPicker.new(@document.sheet_names, index: @sheet_index)
|
|
107
|
+
true
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def close_sheet_list
|
|
111
|
+
return false unless @picker
|
|
112
|
+
|
|
113
|
+
@picker = nil
|
|
114
|
+
true
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def handle_key(key)
|
|
118
|
+
# 一覧・入力欄を開いている間は、すべてのキーをそちらで扱う
|
|
119
|
+
return handle_picker_key(key) if picker_open?
|
|
120
|
+
return handle_prompt_key(key) if search_prompt_open?
|
|
121
|
+
return open_filter_prompt if key == ConfigLoader.keybinds[:search]
|
|
122
|
+
return clear_filter if key == "\e" && filtering?
|
|
123
|
+
|
|
124
|
+
# 設定されたコピーキー(既定 y)は共通処理に渡す。Y に再割り当てされていても
|
|
125
|
+
# 行コピーより設定が優先される
|
|
126
|
+
return super if copy_key?(key)
|
|
127
|
+
|
|
128
|
+
action = action_for(key)
|
|
129
|
+
return super if action.nil?
|
|
130
|
+
|
|
131
|
+
clear_notice
|
|
132
|
+
case action
|
|
133
|
+
when :down then @cursor.move(rows: 1)
|
|
134
|
+
when :up then @cursor.move(rows: -1)
|
|
135
|
+
when :left then @cursor.move(columns: -1)
|
|
136
|
+
when :right then @cursor.move(columns: 1)
|
|
137
|
+
when :page_down then @cursor.page(1)
|
|
138
|
+
when :page_up then @cursor.page(-1)
|
|
139
|
+
when :first_row then @cursor.first_row
|
|
140
|
+
when :last_row then @cursor.last_row
|
|
141
|
+
when :first_column then @cursor.first_column
|
|
142
|
+
when :last_column then @cursor.last_column
|
|
143
|
+
when :previous_sheet then select_sheet(@sheet_index - 1)
|
|
144
|
+
when :next_sheet then select_sheet(@sheet_index + 1)
|
|
145
|
+
when :sheet_list then open_sheet_list
|
|
146
|
+
when :toggle_column then toggle_column
|
|
147
|
+
when :copy_row then copy_row
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# 複数シート(Excel のシート / SQLite のテーブル)を切り替える
|
|
152
|
+
def select_sheet(index)
|
|
153
|
+
return false if index.negative? || index >= @document.sheet_count
|
|
154
|
+
return false if index == @sheet_index
|
|
155
|
+
|
|
156
|
+
@sheet_index = index
|
|
157
|
+
# 列の並びも中身もシートごとに違うので、選択と絞り込みは持ち越さない
|
|
158
|
+
clear_filter
|
|
159
|
+
clear_columns
|
|
160
|
+
@cursor = TableCursor.new(sheet)
|
|
161
|
+
true
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# マウスホイールなど、キー以外からの縦移動もセルカーソルを動かす
|
|
165
|
+
# (シート一覧を開いている間は一覧の選択を動かす)
|
|
166
|
+
def scroll(delta)
|
|
167
|
+
return @picker.move(delta) if picker_open?
|
|
168
|
+
|
|
169
|
+
clear_notice
|
|
170
|
+
@cursor.move(rows: delta)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def page(delta)
|
|
174
|
+
return @picker.page(delta) if picker_open?
|
|
175
|
+
|
|
176
|
+
@cursor.page(delta)
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
# 例: "R3C2 price | hjkl: move y: copy ESC: back"
|
|
180
|
+
# コピー直後は操作ヒントの代わりに結果を出す。
|
|
181
|
+
# 一覧を開いている間は、選んでいるシートと一覧の操作ヒントを出す
|
|
182
|
+
def status_line
|
|
183
|
+
return picker_status_line if picker_open?
|
|
184
|
+
return prompt_status_line if search_prompt_open?
|
|
185
|
+
|
|
186
|
+
hint = @document.sheet_count > 1 ? "#{MOVE_HINT} #{SHEET_HINT}" : MOVE_HINT
|
|
187
|
+
[cell_address, selected_columns_label, filter_label, notice_text || hint].compact.join(' | ')
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# y でコピーする値(表示用に畳む前の生のセル値)。
|
|
191
|
+
# カラムを選んでいればカーソル行の選択カラムをカンマ区切りで連結した 1 行、
|
|
192
|
+
# 選んでいなければカーソル位置のセル。
|
|
193
|
+
# データ行がなければファイルパスを返す(テキストプレビューと同じ扱い)
|
|
194
|
+
def copy_target
|
|
195
|
+
return super if @cursor.empty?
|
|
196
|
+
return selected_columns_line if @selected_columns.any?
|
|
197
|
+
|
|
198
|
+
@cursor.value
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
private
|
|
202
|
+
|
|
203
|
+
# `/`: 絞り込み条件の入力を始める(毎回空から。ESC で元の表示に戻る)
|
|
204
|
+
def open_filter_prompt
|
|
205
|
+
clear_notice
|
|
206
|
+
@filter_error = nil
|
|
207
|
+
@prompt = SearchPrompt.new
|
|
208
|
+
true
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# 入力中のキーはすべて入力欄で扱う(裏のセルカーソルを動かさない)
|
|
212
|
+
def handle_prompt_key(key)
|
|
213
|
+
case @prompt.handle_key(key)
|
|
214
|
+
when :changed then @filter_error = nil
|
|
215
|
+
when :submitted then apply_filter
|
|
216
|
+
when :cancelled then @prompt = nil
|
|
217
|
+
end
|
|
218
|
+
true
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# 入力を条件として読み、シートを絞り込む。読めなければ入力欄を開いたままにする
|
|
222
|
+
def apply_filter
|
|
223
|
+
text = @prompt.query.to_s.strip
|
|
224
|
+
return clear_filter if text.empty?
|
|
225
|
+
|
|
226
|
+
source = @document.sheet(@sheet_index)
|
|
227
|
+
query = TableQuery.parse(text, source.filter_columns)
|
|
228
|
+
filtered = query ? source.filter(query) : nil
|
|
229
|
+
@prompt = nil
|
|
230
|
+
@filter_error = nil
|
|
231
|
+
@filter_text = text
|
|
232
|
+
@filtered_sheet = filtered
|
|
233
|
+
@cursor = TableCursor.new(sheet)
|
|
234
|
+
true
|
|
235
|
+
rescue TableQuery::Error, TableSource::ParseError => e
|
|
236
|
+
@filter_error = e.message
|
|
237
|
+
true
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def filtering?
|
|
241
|
+
!@filter_text.nil?
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def clear_filter
|
|
245
|
+
@prompt = nil
|
|
246
|
+
@filter_text = nil
|
|
247
|
+
@filter_error = nil
|
|
248
|
+
had_filter = !@filtered_sheet.nil?
|
|
249
|
+
@filtered_sheet = nil
|
|
250
|
+
@cursor = TableCursor.new(sheet) if had_filter
|
|
251
|
+
true
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
# 例: "/price > 100_" / 読めなかったときは理由を添える
|
|
255
|
+
def prompt_status_line
|
|
256
|
+
[+"/#{@prompt.query}_", @filter_error].compact.join(' | ')
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
# 例: "filter: price > 100 (2 rows)"
|
|
260
|
+
def filter_label
|
|
261
|
+
return nil unless filtering?
|
|
262
|
+
|
|
263
|
+
"filter: #{TextUtils.sanitize_for_display(@filter_text)} (#{sheet.row_count} rows)"
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
# 一覧のキー。扱わないキーも含めて必ず true を返し、裏の表に通さない
|
|
267
|
+
def handle_picker_key(key)
|
|
268
|
+
keys = ConfigLoader.keybinds
|
|
269
|
+
|
|
270
|
+
case key
|
|
271
|
+
when "\r", "\n" then choose_sheet
|
|
272
|
+
when "\e", LIST_KEY then close_sheet_list
|
|
273
|
+
when keys[:move_down], "\e[B" then @picker.move(1)
|
|
274
|
+
when keys[:move_up], "\e[A" then @picker.move(-1)
|
|
275
|
+
when "\x04" then @picker.page(1)
|
|
276
|
+
when "\x15" then @picker.page(-1)
|
|
277
|
+
when keys[:top] then @picker.first
|
|
278
|
+
when keys[:bottom] then @picker.last
|
|
279
|
+
end
|
|
280
|
+
true
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
# 一覧で選んだシートを開く(同じシートなら切り替えずに閉じるだけ)
|
|
284
|
+
def choose_sheet
|
|
285
|
+
index = @picker.index
|
|
286
|
+
close_sheet_list
|
|
287
|
+
select_sheet(index)
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
# 例: "2/3 sales | j/k: move Enter: open ESC: cancel"
|
|
291
|
+
def picker_status_line
|
|
292
|
+
name = TextUtils.sanitize_for_display(@picker.selected_name.to_s)
|
|
293
|
+
["#{@picker.index + 1}/#{@picker.count} #{name}", PICKER_HINT].join(' | ')
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
# y: 選択カラムがあれば、何列コピーしたかを通知に出す
|
|
297
|
+
def perform_copy
|
|
298
|
+
return super unless @selected_columns.any? && !@cursor.empty?
|
|
299
|
+
|
|
300
|
+
copy_text(copy_target, label: "#{@selected_columns.length} cols")
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
# Space: カーソル位置のカラムを選択・解除する
|
|
304
|
+
def toggle_column
|
|
305
|
+
index = @cursor.column
|
|
306
|
+
return false unless index < sheet.column_count
|
|
307
|
+
|
|
308
|
+
clear_notice
|
|
309
|
+
@selected_columns.include?(index) ? @selected_columns.delete(index) : @selected_columns << index
|
|
310
|
+
true
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
def clear_columns
|
|
314
|
+
@selected_columns = []
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
# 選択カラムのセルを CSV の 1 行として連結する(カンマ区切り。
|
|
318
|
+
# カンマや改行を含むセルは読み戻せるようクォートする)
|
|
319
|
+
def selected_columns_line
|
|
320
|
+
row = sheet.fetch_rows(@cursor.row, 1).first
|
|
321
|
+
CSV.generate_line(selected_columns.map { |index| row && row[index] }).chomp
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
# 例: "cols A,B" / "cols A,B,C,D +4"
|
|
325
|
+
def selected_columns_label
|
|
326
|
+
return nil if @selected_columns.empty?
|
|
327
|
+
|
|
328
|
+
labels = selected_columns.map { |index| sheet.column_label(index) }
|
|
329
|
+
shown = labels.first(SELECTED_LABEL_LIMIT).join(',')
|
|
330
|
+
rest = labels.length - SELECTED_LABEL_LIMIT
|
|
331
|
+
rest.positive? ? "cols #{shown} +#{rest}" : "cols #{shown}"
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
# Y: カーソル行を CSV の 1 行としてコピーする
|
|
335
|
+
def copy_row
|
|
336
|
+
return copy_text(nil) if @cursor.empty?
|
|
337
|
+
|
|
338
|
+
row = sheet.fetch_rows(@cursor.row, 1).first
|
|
339
|
+
copy_text(CSV.generate_line(Array(row)).chomp, label: 'row')
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
def cell_address
|
|
343
|
+
return nil if @cursor.empty?
|
|
344
|
+
|
|
345
|
+
title = sheet.header && sheet.header[@cursor.column]
|
|
346
|
+
address = "R#{@cursor.row + 1}C#{@cursor.column + 1}"
|
|
347
|
+
return address if title.nil? || title.empty?
|
|
348
|
+
|
|
349
|
+
# 見出しはファイル由来。ここは幅が広く長い文字列が入るため必ず無害化する
|
|
350
|
+
"#{address} #{TextUtils.sanitize_for_display(title)}"
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
# キーバインド設定(hjkl の変更に追従する)と固定キーから操作を決める
|
|
354
|
+
def action_for(key)
|
|
355
|
+
keys = ConfigLoader.keybinds
|
|
356
|
+
|
|
357
|
+
case key
|
|
358
|
+
when keys[:move_down], "\e[B" then :down
|
|
359
|
+
when keys[:move_up], "\e[A" then :up
|
|
360
|
+
when keys[:navigate_parent], "\e[D" then :left
|
|
361
|
+
when keys[:navigate_enter], "\e[C" then :right
|
|
362
|
+
when "\x04" then :page_down
|
|
363
|
+
when "\x15" then :page_up
|
|
364
|
+
when keys[:top] then :first_row
|
|
365
|
+
when keys[:bottom] then :last_row
|
|
366
|
+
when '0' then :first_column
|
|
367
|
+
when '$' then :last_column
|
|
368
|
+
when '[' then :previous_sheet
|
|
369
|
+
when ']' then :next_sheet
|
|
370
|
+
when LIST_KEY then :sheet_list
|
|
371
|
+
when keys[:select] then :toggle_column
|
|
372
|
+
when 'Y' then :copy_row
|
|
373
|
+
end
|
|
374
|
+
end
|
|
375
|
+
end
|
|
376
|
+
end
|
|
@@ -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
|