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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +86 -0
- data/README.md +18 -6
- data/README_ja.md +18 -6
- data/Rakefile +5 -0
- data/SECURITY.md +16 -8
- data/docs/csv_cell_preview_design_ja.md +682 -0
- data/docs/guide.md +115 -20
- data/docs/guide_ja.md +116 -21
- data/docs/image_preview_design_ja.md +335 -0
- data/docs/native_fuzzy_search_design_ja.md +684 -0
- data/examples/config.rb +23 -0
- data/info/help.md +32 -18
- data/info/keybindings.md +43 -11
- 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 -4
- data/lib/rufio/config_loader.rb +66 -11
- 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/fuzzy_candidate_block.rb +131 -0
- data/lib/rufio/fuzzy_file_source.rb +180 -0
- data/lib/rufio/fuzzy_search.rb +327 -0
- data/lib/rufio/fuzzy_search_session.rb +426 -0
- data/lib/rufio/graphics_layer.rb +223 -0
- data/lib/rufio/health_checker.rb +27 -32
- data/lib/rufio/help_menu.rb +16 -10
- data/lib/rufio/image_preview_view.rb +421 -0
- data/lib/rufio/image_source.rb +158 -0
- data/lib/rufio/keybind_handler.rb +21 -34
- 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 +44 -8
- data/lib/rufio/native_image.rb +177 -0
- data/lib/rufio/native_scanner_zig.rb +39 -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 +38 -1
- data/lib/rufio/search_prompt.rb +50 -0
- data/lib/rufio/search_screen.rb +680 -0
- 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 +74 -60
- data/lib/rufio/text_utils.rb +27 -0
- data/lib/rufio/ui_renderer.rb +380 -17
- data/lib/rufio/version.rb +1 -1
- data/lib/rufio/xlsx_table_source.rb +112 -0
- data/lib/rufio.rb +21 -1
- data/lib_zig/rufio_native/Makefile +21 -5
- data/lib_zig/rufio_native/build.zig +18 -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/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 +28 -0
- data/lib_zig/rufio_native/src/search.zig +260 -64
- 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 +44 -5
- data/lib/rufio/global_search.rb +0 -412
- data/lib/rufio/search_controller.rb +0 -92
- data/lib_zig/rufio_native/src/main.zig.sync +0 -205
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'text_utils'
|
|
4
|
+
|
|
5
|
+
module Rufio
|
|
6
|
+
# 表プレビューの幅計算と行文字列の生成。形式(CSV / Excel / SQLite)に依存しない。
|
|
7
|
+
#
|
|
8
|
+
# 1 行の構成:
|
|
9
|
+
# <行番号ガター><"| " + セル><"| " + セル>...<"|">
|
|
10
|
+
#
|
|
11
|
+
# 列幅は全角を 2 として数え(TextUtils)、min..max の範囲に収める。
|
|
12
|
+
class TableLayout
|
|
13
|
+
MIN_COLUMN_WIDTH = 3
|
|
14
|
+
DEFAULT_MAX_COLUMN_WIDTH = 32
|
|
15
|
+
# 列幅算出に使う先頭行数(全行走査を避けるため)
|
|
16
|
+
SAMPLE_ROWS = 200
|
|
17
|
+
# 列の区切り(罫線 + スペース)とセル右側の余白
|
|
18
|
+
CELL_PREFIX = '| '
|
|
19
|
+
CELL_SUFFIX = ' '
|
|
20
|
+
CLOSING_SEPARATOR = '|'
|
|
21
|
+
# 1 列が消費する幅 = 区切り + 余白 + 列幅
|
|
22
|
+
SEGMENT_OVERHEAD = CELL_PREFIX.length + CELL_SUFFIX.length
|
|
23
|
+
MIN_GUTTER_DIGITS = 2
|
|
24
|
+
# 幅指定なしで生成するとき(lines フォールバック用)の上限
|
|
25
|
+
PLAIN_MAX_WIDTH = 500
|
|
26
|
+
# 幅計算と表示で見るセルの文字数。表示幅は max_column_width 以下なので、
|
|
27
|
+
# 極端に長いセル(数十万文字)を毎回走査しないための歯止め
|
|
28
|
+
CELL_SCAN_LIMIT = 256
|
|
29
|
+
|
|
30
|
+
attr_reader :width, :column_widths, :gutter_width, :max_column_width
|
|
31
|
+
|
|
32
|
+
# @param sheet [TableSheet]
|
|
33
|
+
# @param width [Integer, nil] 表示幅。nil / 0 以下なら内容に合わせた自然幅
|
|
34
|
+
def self.build(sheet, width, max_column_width: DEFAULT_MAX_COLUMN_WIDTH, sample_rows: SAMPLE_ROWS)
|
|
35
|
+
new(sheet, width, max_column_width: max_column_width, sample_rows: sample_rows)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# FilePreview の lines フォールバック用。グリッドを文字列の配列として返す。
|
|
39
|
+
def self.plain_lines(document, max_lines: 50, max_column_width: DEFAULT_MAX_COLUMN_WIDTH)
|
|
40
|
+
sheet = document.sheet
|
|
41
|
+
return [] unless sheet
|
|
42
|
+
|
|
43
|
+
layout = build(sheet, nil, max_column_width: max_column_width)
|
|
44
|
+
layout = build(sheet, PLAIN_MAX_WIDTH, max_column_width: max_column_width) if layout.width > PLAIN_MAX_WIDTH
|
|
45
|
+
|
|
46
|
+
lines = []
|
|
47
|
+
lines << layout.header_line if sheet.header
|
|
48
|
+
remaining = [max_lines - lines.length - 1, 0].max
|
|
49
|
+
sheet.fetch_rows(0, remaining).each_with_index do |row, index|
|
|
50
|
+
lines << layout.row_line(index + 1, row)
|
|
51
|
+
end
|
|
52
|
+
status = document.status_line
|
|
53
|
+
lines << status if status && !status.empty?
|
|
54
|
+
lines
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def initialize(sheet, width, max_column_width: DEFAULT_MAX_COLUMN_WIDTH, sample_rows: SAMPLE_ROWS)
|
|
58
|
+
@sheet = sheet
|
|
59
|
+
@max_column_width = [max_column_width.to_i, MIN_COLUMN_WIDTH].max
|
|
60
|
+
@column_widths = compute_column_widths(sample_rows)
|
|
61
|
+
@gutter_digits = [sheet.row_count.to_s.length, MIN_GUTTER_DIGITS].max
|
|
62
|
+
|
|
63
|
+
requested = width.to_i
|
|
64
|
+
if requested <= 0
|
|
65
|
+
@gutter_width = @gutter_digits + 1
|
|
66
|
+
@width = natural_width
|
|
67
|
+
else
|
|
68
|
+
@width = requested
|
|
69
|
+
@gutter_width = fits_gutter? ? @gutter_digits + 1 : 0
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def column_count
|
|
74
|
+
@sheet.column_count
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# col_offset から始まり、表示幅に収まる列インデックス(最低 1 列は返す)
|
|
78
|
+
def visible_columns(col_offset = 0)
|
|
79
|
+
return [] if column_count.zero?
|
|
80
|
+
|
|
81
|
+
offset = col_offset.clamp(0, column_count - 1)
|
|
82
|
+
available = @width - @gutter_width
|
|
83
|
+
return [offset] if available <= 0
|
|
84
|
+
|
|
85
|
+
columns = []
|
|
86
|
+
consumed = 0
|
|
87
|
+
(offset...column_count).each do |index|
|
|
88
|
+
segment = SEGMENT_OVERHEAD + @column_widths[index]
|
|
89
|
+
if consumed + segment > available
|
|
90
|
+
columns << index if columns.empty?
|
|
91
|
+
break
|
|
92
|
+
end
|
|
93
|
+
columns << index
|
|
94
|
+
consumed += segment
|
|
95
|
+
end
|
|
96
|
+
columns
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# 表示されていない右側の列数
|
|
100
|
+
def hidden_column_count(col_offset = 0)
|
|
101
|
+
visible = visible_columns(col_offset)
|
|
102
|
+
return column_count if visible.empty?
|
|
103
|
+
|
|
104
|
+
# 左にスクロールして隠れた列 + 右に収まらなかった列
|
|
105
|
+
visible.first + (column_count - (visible.last + 1))
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# column_index が表示範囲に入る最小の col_offset を返す(横スクロール量の計算)
|
|
109
|
+
def offset_for(column_index, current_offset = 0)
|
|
110
|
+
return 0 if column_count.zero?
|
|
111
|
+
|
|
112
|
+
target = column_index.clamp(0, column_count - 1)
|
|
113
|
+
offset = current_offset.clamp(0, column_count - 1)
|
|
114
|
+
return target if target < offset
|
|
115
|
+
|
|
116
|
+
offset += 1 while offset < target && !visible_columns(offset).include?(target)
|
|
117
|
+
offset
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# ペイン内のセル描画開始 x(セル本体の先頭。区切りとスペースの右側)
|
|
121
|
+
# @return [Integer, nil] 表示範囲外なら nil
|
|
122
|
+
def column_x(column_index, col_offset = 0)
|
|
123
|
+
visible = visible_columns(col_offset)
|
|
124
|
+
return nil unless visible.include?(column_index)
|
|
125
|
+
|
|
126
|
+
x = @gutter_width
|
|
127
|
+
visible.each do |index|
|
|
128
|
+
return x + CELL_PREFIX.length if index == column_index
|
|
129
|
+
|
|
130
|
+
x += SEGMENT_OVERHEAD + @column_widths[index]
|
|
131
|
+
end
|
|
132
|
+
nil
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def column_width(column_index)
|
|
136
|
+
@column_widths[column_index] || MIN_COLUMN_WIDTH
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# セル 1 個分の表示文字列(改行・タブを畳み、列幅に揃える)
|
|
140
|
+
def format_cell(value, column_index)
|
|
141
|
+
TextUtils.pad_string_to_width(normalize(value), column_width(column_index))
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def gutter_text(row_number)
|
|
145
|
+
return '' if @gutter_width.zero?
|
|
146
|
+
|
|
147
|
+
"#{row_number.to_s.rjust(@gutter_digits)} "
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# 見出しセル 1 個分の表示文字列(列ラベル + 列名)
|
|
151
|
+
def format_header_cell(column_index)
|
|
152
|
+
title = TextUtils.sanitize_for_display(@sheet.column_title(column_index))
|
|
153
|
+
TextUtils.pad_string_to_width(title, column_width(column_index))
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def header_line(col_offset = 0)
|
|
157
|
+
line = ' ' * @gutter_width
|
|
158
|
+
visible_columns(col_offset).each do |index|
|
|
159
|
+
line += CELL_PREFIX + format_header_cell(index) + CELL_SUFFIX
|
|
160
|
+
end
|
|
161
|
+
fit(close(line))
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# @param row_number [Integer] 1 始まりの表示用行番号
|
|
165
|
+
# @param cells [Array<String>, nil]
|
|
166
|
+
def row_line(row_number, cells, col_offset = 0)
|
|
167
|
+
line = gutter_text(row_number)
|
|
168
|
+
visible_columns(col_offset).each do |index|
|
|
169
|
+
line += CELL_PREFIX + format_cell(cells && cells[index], index) + CELL_SUFFIX
|
|
170
|
+
end
|
|
171
|
+
fit(close(line))
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
private
|
|
175
|
+
|
|
176
|
+
def natural_width
|
|
177
|
+
@gutter_width + @column_widths.sum { |w| SEGMENT_OVERHEAD + w } + CLOSING_SEPARATOR.length
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def fits_gutter?
|
|
181
|
+
desired = @gutter_digits + 1
|
|
182
|
+
@width - desired >= SEGMENT_OVERHEAD + MIN_COLUMN_WIDTH
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def compute_column_widths(sample_rows)
|
|
186
|
+
rows = @sheet.fetch_rows(0, sample_rows)
|
|
187
|
+
Array.new(column_count) do |index|
|
|
188
|
+
longest = TextUtils.display_width(@sheet.column_title(index))
|
|
189
|
+
rows.each do |row|
|
|
190
|
+
candidate = TextUtils.display_width(normalize(row[index]))
|
|
191
|
+
longest = candidate if candidate > longest
|
|
192
|
+
end
|
|
193
|
+
longest.clamp(MIN_COLUMN_WIDTH, @max_column_width)
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# 表示用の正規化。セル内の改行・タブは幅計算を壊し、エスケープシーケンスは
|
|
198
|
+
# 端末を操作してしまうため、制御文字をまとめて空白に置き換える。
|
|
199
|
+
# y でコピーする値は元のまま(TableSheet 側が保持している)。
|
|
200
|
+
def normalize(value)
|
|
201
|
+
return '' if value.nil?
|
|
202
|
+
|
|
203
|
+
text = value.to_s
|
|
204
|
+
text = text[0, CELL_SCAN_LIMIT] if text.length > CELL_SCAN_LIMIT
|
|
205
|
+
TextUtils.sanitize_for_display(text)
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def close(line)
|
|
209
|
+
TextUtils.display_width(line) + CLOSING_SEPARATOR.length <= @width ? line + CLOSING_SEPARATOR : line
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
# 幅に満たなければ空白で埋め、超えていれば切り詰める(セル側の "..." と区別するため
|
|
213
|
+
# 行末では省略記号を付けない)
|
|
214
|
+
def fit(line)
|
|
215
|
+
current = TextUtils.display_width(line)
|
|
216
|
+
return line + ' ' * (@width - current) if current < @width
|
|
217
|
+
return line if current == @width
|
|
218
|
+
|
|
219
|
+
clipped = ''
|
|
220
|
+
consumed = 0
|
|
221
|
+
line.each_char do |char|
|
|
222
|
+
char_width = TextUtils.char_width(char)
|
|
223
|
+
break if consumed + char_width > @width
|
|
224
|
+
|
|
225
|
+
clipped += char
|
|
226
|
+
consumed += char_width
|
|
227
|
+
end
|
|
228
|
+
consumed < @width ? clipped + ' ' * (@width - consumed) : clipped
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
end
|
|
@@ -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
|