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,87 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rufio
|
|
4
|
+
# 表プレビューのシート一覧(Excel のシート / SQLite のテーブル)の選択状態。
|
|
5
|
+
#
|
|
6
|
+
# 名前の並びだけを受け取り、選択位置と縦スクロール位置を持つ。TableCursor と同じく
|
|
7
|
+
# 「選択が見えるように最小限ずらす」方式で、表示できる行数は描画側(UIRenderer)が
|
|
8
|
+
# 毎フレーム visible_rows= で教える。
|
|
9
|
+
class SheetPicker
|
|
10
|
+
# 表示行数が分からないうちの既定値
|
|
11
|
+
DEFAULT_VISIBLE_ROWS = 10
|
|
12
|
+
|
|
13
|
+
attr_reader :names, :index, :offset, :visible_rows
|
|
14
|
+
|
|
15
|
+
# @param names [Array<String>] シート名(表示順)
|
|
16
|
+
# @param index [Integer] 最初に選んでおく添字(現在表示中のシート)
|
|
17
|
+
def initialize(names, index: 0)
|
|
18
|
+
@names = Array(names)
|
|
19
|
+
@index = empty? ? 0 : index.to_i.clamp(0, count - 1)
|
|
20
|
+
@offset = 0
|
|
21
|
+
@visible_rows = DEFAULT_VISIBLE_ROWS
|
|
22
|
+
ensure_visible
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def count
|
|
26
|
+
@names.length
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def empty?
|
|
30
|
+
@names.empty?
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def selected_name
|
|
34
|
+
@names[@index]
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# @return [Boolean] 選択位置かスクロール位置が動いたか
|
|
38
|
+
def move(delta)
|
|
39
|
+
move_to(@index + delta)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def first
|
|
43
|
+
move_to(0)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def last
|
|
47
|
+
move_to(count - 1)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def move_to(index)
|
|
51
|
+
return false if empty?
|
|
52
|
+
|
|
53
|
+
before = [@index, @offset]
|
|
54
|
+
@index = index.to_i.clamp(0, count - 1)
|
|
55
|
+
ensure_visible
|
|
56
|
+
before != [@index, @offset]
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def page(pages)
|
|
60
|
+
move(pages * @visible_rows)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# 描画側が表示できる行数を伝える
|
|
64
|
+
def visible_rows=(rows)
|
|
65
|
+
@visible_rows = [rows.to_i, 1].max
|
|
66
|
+
ensure_visible
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# @return [Array<Array(Integer, String)>] 表示する [添字, 名前] の並び
|
|
70
|
+
def visible
|
|
71
|
+
return [] if empty?
|
|
72
|
+
|
|
73
|
+
last_index = [@offset + @visible_rows, count].min - 1
|
|
74
|
+
(@offset..last_index).map { |i| [i, @names[i]] }
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
private
|
|
78
|
+
|
|
79
|
+
# 選択行が表示範囲に入るよう、スクロール位置を最小限ずらす
|
|
80
|
+
def ensure_visible
|
|
81
|
+
@offset = @index if @index < @offset
|
|
82
|
+
last_visible = @offset + @visible_rows - 1
|
|
83
|
+
@offset = @index - @visible_rows + 1 if @index > last_visible
|
|
84
|
+
@offset = @offset.clamp(0, [count - @visible_rows, 0].max)
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'csv'
|
|
4
|
+
require_relative 'table_source'
|
|
5
|
+
require_relative 'table_document'
|
|
6
|
+
require_relative 'table_query'
|
|
7
|
+
require_relative 'command_locator'
|
|
8
|
+
|
|
9
|
+
module Rufio
|
|
10
|
+
# SQLite のデータベースをテーブルごとの表として読む TableSource。
|
|
11
|
+
#
|
|
12
|
+
# rga / bat / zoxide と同じ「インストールされていれば使う」方式で、sqlite3 コマンドが
|
|
13
|
+
# 無ければ表として扱わない(プレビューは従来のバイナリ表示になる)。gem を増やさない。
|
|
14
|
+
#
|
|
15
|
+
# 判定は拡張子ではなくファイル先頭のシグネチャで行う。拡張子は .db / .sqlite に限らず
|
|
16
|
+
# 何でもよく、逆に .db という名前の別形式を誤って開くこともない。
|
|
17
|
+
#
|
|
18
|
+
# 実行は argv(シェル非経由)で、必ず読み取り専用で開き、発行するのは SELECT だけ。
|
|
19
|
+
class SqliteTableSource < TableSource
|
|
20
|
+
COMMAND = 'sqlite3'
|
|
21
|
+
SIGNATURE = "SQLite format 3\x00".b
|
|
22
|
+
# 表示するテーブル(sqlite_ で始まる内部テーブルは除く)
|
|
23
|
+
TABLE_LIST_SQL = "SELECT name FROM sqlite_master WHERE type IN ('table','view') " \
|
|
24
|
+
"AND name NOT LIKE 'sqlite_%' ORDER BY name;"
|
|
25
|
+
|
|
26
|
+
class << self
|
|
27
|
+
def supports?(path)
|
|
28
|
+
return false unless command_available?
|
|
29
|
+
|
|
30
|
+
file = path.to_s
|
|
31
|
+
return false unless File.file?(file)
|
|
32
|
+
|
|
33
|
+
sqlite_file?(file)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def open(path, limits: TableSource::Limits.default, options: {})
|
|
37
|
+
tables = list_tables(path)
|
|
38
|
+
raise TableSource::ParseError, 'no tables' if tables.empty?
|
|
39
|
+
|
|
40
|
+
sheets = tables.map do |table|
|
|
41
|
+
# `/` の絞り込みは WHERE 付きの SELECT を発行し直す(読み込み上限より後ろも探す)
|
|
42
|
+
filter = ->(query) { load_table(path, table, limits, options, query) }
|
|
43
|
+
LazyTableSheet.new(name: table, filter: filter) { load_table(path, table, limits, options) }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
TableDocument.new(sheets: sheets, encoding: 'UTF-8', label: File.basename(path.to_s),
|
|
47
|
+
sheet_label: 'Tables')
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# sqlite3 コマンドが使えるか(テストから差し替えられるよう public)
|
|
51
|
+
def command_available?
|
|
52
|
+
return @command_available unless @command_available.nil?
|
|
53
|
+
|
|
54
|
+
@command_available = CommandLocator.available?(COMMAND)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# argv を受け取り [出力, 成功したか] を返す Proc(テストから差し替える)
|
|
58
|
+
def runner
|
|
59
|
+
@runner ||= method(:run_command)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# -safe を付けられるか(古い sqlite3 は知らないオプションで失敗する)。
|
|
63
|
+
# プロセス内で 1 度だけ確認する
|
|
64
|
+
def safe_mode_supported?
|
|
65
|
+
return @safe_mode_supported unless @safe_mode_supported.nil?
|
|
66
|
+
|
|
67
|
+
_output, ok = runner.call([COMMAND, '-safe', '-csv', ':memory:', 'SELECT 1;'])
|
|
68
|
+
@safe_mode_supported = ok
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
attr_writer :runner
|
|
72
|
+
|
|
73
|
+
def reset!
|
|
74
|
+
@runner = nil
|
|
75
|
+
@command_available = nil
|
|
76
|
+
@safe_mode_supported = nil
|
|
77
|
+
CommandLocator.reset!
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
def sqlite_file?(file)
|
|
83
|
+
File.binread(file, SIGNATURE.bytesize) == SIGNATURE
|
|
84
|
+
rescue SystemCallError
|
|
85
|
+
false
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def list_tables(path)
|
|
89
|
+
output = query(path, TABLE_LIST_SQL, header: false)
|
|
90
|
+
CSV.parse(output).flatten.compact.map(&:strip).reject(&:empty?)
|
|
91
|
+
rescue CSV::MalformedCSVError => e
|
|
92
|
+
raise TableSource::ParseError, e.message
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def load_table(path, table, limits, options, filter = nil)
|
|
96
|
+
# 上限 + 1 行だけ取り、全体の件数を数える COUNT(*) は発行しない
|
|
97
|
+
sql = +"SELECT * FROM #{quote_identifier(table)}"
|
|
98
|
+
sql << " WHERE #{where_clause(filter)}" if filter
|
|
99
|
+
sql << " LIMIT #{limits.max_rows + 1};"
|
|
100
|
+
parsed = CSV.parse(query(path, sql, header: true))
|
|
101
|
+
|
|
102
|
+
# 列の切り詰めは見出し行にも掛ける(掛け忘れると列数が行とずれる)
|
|
103
|
+
column_truncated = false
|
|
104
|
+
rows = parsed.map do |row|
|
|
105
|
+
cells = Array(row).map { |value| value.nil? ? '' : value.to_s }
|
|
106
|
+
if cells.length > limits.max_columns
|
|
107
|
+
cells = cells[0, limits.max_columns]
|
|
108
|
+
column_truncated = true
|
|
109
|
+
end
|
|
110
|
+
cells
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
header = options.fetch(:header, true) && !rows.empty? ? rows.shift : nil
|
|
114
|
+
row_truncated = rows.length > limits.max_rows
|
|
115
|
+
rows = rows[0, limits.max_rows] if row_truncated
|
|
116
|
+
|
|
117
|
+
{ rows: rows, header: header, row_truncated: row_truncated, column_truncated: column_truncated }
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def query(path, sql, header:)
|
|
121
|
+
argv = [COMMAND, '-readonly', '-csv']
|
|
122
|
+
# -safe は writefile() / load_extension() / ATTACH などを拒否する(sqlite3 3.34+)。
|
|
123
|
+
# 読み取り専用の SELECT しか発行しないため、この機能に失うものはない
|
|
124
|
+
argv << '-safe' if safe_mode_supported?
|
|
125
|
+
argv << '-header' if header
|
|
126
|
+
# SECURITY.md の方針どおり、'-' で始まるパスがオプションと解釈されないようにする
|
|
127
|
+
argv += [File.expand_path(path.to_s), sql]
|
|
128
|
+
|
|
129
|
+
output, ok = runner.call(argv)
|
|
130
|
+
raise TableSource::ParseError, "#{COMMAND} failed" unless ok
|
|
131
|
+
|
|
132
|
+
force_utf8(output)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def run_command(argv)
|
|
136
|
+
output = IO.popen(argv, 'r', err: File::NULL, &:read)
|
|
137
|
+
status = Process.last_status
|
|
138
|
+
[force_utf8(output), status.nil? || status.success?]
|
|
139
|
+
rescue SystemCallError, IOError => e
|
|
140
|
+
raise TableSource::ParseError, e.message
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# コマンドの出力は Encoding.default_external になる(LANG が無い環境では US-ASCII)。
|
|
144
|
+
# sqlite3 は UTF-8 で出力するため、読み替えないと日本語を含むテーブルで
|
|
145
|
+
# CSV が「Invalid byte sequence」になる。
|
|
146
|
+
def force_utf8(output)
|
|
147
|
+
text = output.to_s.dup.force_encoding(Encoding::UTF_8)
|
|
148
|
+
text.valid_encoding? ? text : text.scrub('?')
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# テーブル名は識別子クォートする(SQL を文字列連結で組まない)
|
|
152
|
+
def quote_identifier(name)
|
|
153
|
+
%("#{name.to_s.gsub('"', '""')}")
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# TableQuery の条件を WHERE 句にする。
|
|
157
|
+
# 列名は TableQuery が実在する列に解決済みで、ここで識別子クォートする。
|
|
158
|
+
# 演算子は TableQuery が許可したものだけ、値は常にリテラルとして引用する。
|
|
159
|
+
def where_clause(filter)
|
|
160
|
+
parts = filter.conditions.map { |condition| condition_sql(condition, filter.columns) }
|
|
161
|
+
raise TableSource::ParseError, 'no condition' if parts.empty?
|
|
162
|
+
|
|
163
|
+
parts.join(' AND ')
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def condition_sql(condition, columns)
|
|
167
|
+
if condition.column.nil?
|
|
168
|
+
targets = Array(columns)
|
|
169
|
+
raise TableSource::ParseError, 'no columns to search' if targets.empty?
|
|
170
|
+
|
|
171
|
+
"(#{targets.map { |column| like_sql(column, condition.value) }.join(' OR ')})"
|
|
172
|
+
elsif condition.operator == TableQuery::LIKE
|
|
173
|
+
"(#{like_sql(condition.column, condition.value)})"
|
|
174
|
+
else
|
|
175
|
+
"(#{quote_identifier(condition.column)} #{operator_sql(condition.operator)} " \
|
|
176
|
+
"#{value_sql(condition.value)})"
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def like_sql(column, pattern)
|
|
181
|
+
"#{quote_identifier(column)} LIKE #{quote_string(pattern)}"
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# 許可した演算子だけを通す(!= は SQLite の <> に揃える)
|
|
185
|
+
def operator_sql(operator)
|
|
186
|
+
raise TableSource::ParseError, "unsupported operator: #{operator}" unless
|
|
187
|
+
TableQuery::OPERATORS.include?(operator)
|
|
188
|
+
|
|
189
|
+
operator == '!=' ? '<>' : operator
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# 数値はそのまま、それ以外は文字列リテラルとして引用する
|
|
193
|
+
def value_sql(value)
|
|
194
|
+
text = value.to_s
|
|
195
|
+
return text if text.match?(/\A-?\d+(\.\d+)?\z/)
|
|
196
|
+
|
|
197
|
+
quote_string(text)
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def quote_string(value)
|
|
201
|
+
"'#{value.to_s.gsub("'", "''")}'"
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
end
|
|
@@ -4,12 +4,11 @@ module Rufio
|
|
|
4
4
|
# タブモード管理クラス
|
|
5
5
|
# 上部メニューの2段目に表示するモードタブを管理する
|
|
6
6
|
class TabModeManager
|
|
7
|
-
MODES = %i[files config
|
|
7
|
+
MODES = %i[files config logs jobs help].freeze
|
|
8
8
|
|
|
9
9
|
MODE_LABELS = {
|
|
10
10
|
files: 'Files',
|
|
11
11
|
config: 'Config',
|
|
12
|
-
global: 'Search',
|
|
13
12
|
logs: 'Logs',
|
|
14
13
|
jobs: 'Jobs',
|
|
15
14
|
help: 'Help'
|
|
@@ -18,7 +17,6 @@ module Rufio
|
|
|
18
17
|
MODE_KEYS = {
|
|
19
18
|
files: nil,
|
|
20
19
|
config: 'C',
|
|
21
|
-
global: 'G',
|
|
22
20
|
logs: 'L',
|
|
23
21
|
jobs: 'J',
|
|
24
22
|
help: '?'
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rufio
|
|
4
|
+
# 表プレビューのセルカーソル。行・列の位置と、縦横のスクロール位置を持つ。
|
|
5
|
+
#
|
|
6
|
+
# 縦スクロールは「カーソルが見えるように最小限ずらす」方式で、表示できる行数は
|
|
7
|
+
# 描画側(UIRenderer)が毎フレーム visible_rows= で教える。
|
|
8
|
+
# 横スクロールは列幅に依存するため、ずらす量の計算は TableLayout#offset_for が担う。
|
|
9
|
+
class TableCursor
|
|
10
|
+
# 表示行数が分からないうちのページ送り量(既存のプレビュースクロールと同じ)
|
|
11
|
+
DEFAULT_VISIBLE_ROWS = 20
|
|
12
|
+
|
|
13
|
+
attr_reader :row, :column, :row_offset, :visible_rows
|
|
14
|
+
attr_accessor :column_offset
|
|
15
|
+
|
|
16
|
+
def initialize(sheet)
|
|
17
|
+
@sheet = sheet
|
|
18
|
+
@row = 0
|
|
19
|
+
@column = 0
|
|
20
|
+
@row_offset = 0
|
|
21
|
+
@column_offset = 0
|
|
22
|
+
@visible_rows = DEFAULT_VISIBLE_ROWS
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def row_count
|
|
26
|
+
@sheet.row_count
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def column_count
|
|
30
|
+
@sheet.column_count
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# カーソルの指すセルの生の値(表示用に畳んでいない値)
|
|
34
|
+
def value
|
|
35
|
+
@sheet.cell(@row, @column)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def empty?
|
|
39
|
+
row_count.zero? || column_count.zero?
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# 先頭のセルに戻す(表示できる行数の記憶は保つ)
|
|
43
|
+
def reset
|
|
44
|
+
@row = 0
|
|
45
|
+
@column = 0
|
|
46
|
+
@row_offset = 0
|
|
47
|
+
@column_offset = 0
|
|
48
|
+
true
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# @return [Boolean] 位置が動いたか
|
|
52
|
+
def move(rows: 0, columns: 0)
|
|
53
|
+
move_to(@row + rows, @column + columns)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def move_to(row, column)
|
|
57
|
+
before = [@row, @column, @row_offset]
|
|
58
|
+
@row = clamp(row, row_count)
|
|
59
|
+
@column = clamp(column, column_count)
|
|
60
|
+
ensure_row_visible
|
|
61
|
+
before != [@row, @column, @row_offset]
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def move_to_row(row)
|
|
65
|
+
move_to(row, @column)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def move_to_column(column)
|
|
69
|
+
move_to(@row, column)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def first_row
|
|
73
|
+
move_to_row(0)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def last_row
|
|
77
|
+
move_to_row(row_count - 1)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def first_column
|
|
81
|
+
move_to_column(0)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def last_column
|
|
85
|
+
move_to_column(column_count - 1)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# @param pages [Integer] 正で下、負で上
|
|
89
|
+
def page(pages)
|
|
90
|
+
move(rows: pages * [@visible_rows, 1].max)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# 描画側が表示できるデータ行数を伝える
|
|
94
|
+
def visible_rows=(count)
|
|
95
|
+
@visible_rows = [count.to_i, 1].max
|
|
96
|
+
ensure_row_visible
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# カーソル行が表示範囲に入るよう、スクロール位置を最小限ずらす
|
|
100
|
+
def ensure_row_visible
|
|
101
|
+
@row_offset = @row if @row < @row_offset
|
|
102
|
+
last_visible = @row_offset + @visible_rows - 1
|
|
103
|
+
@row_offset = @row - @visible_rows + 1 if @row > last_visible
|
|
104
|
+
@row_offset = 0 if @row_offset.negative?
|
|
105
|
+
@row_offset = [@row_offset, [row_count - 1, 0].max].min
|
|
106
|
+
@row_offset
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
private
|
|
110
|
+
|
|
111
|
+
def clamp(value, count)
|
|
112
|
+
return 0 if count <= 0
|
|
113
|
+
|
|
114
|
+
value.clamp(0, count - 1)
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'text_utils'
|
|
4
|
+
|
|
5
|
+
module Rufio
|
|
6
|
+
# 表プレビューの 1 シート。
|
|
7
|
+
# CSV は 1 枚、Excel はシート、SQLite はテーブルがそれぞれこれに対応する。
|
|
8
|
+
#
|
|
9
|
+
# 行は fetch_rows(offset, limit) 経由でのみ取得する。こうしておくと、
|
|
10
|
+
# 全行をメモリに持つ形式(CSV)と、必要な範囲だけ読む形式(SQLite の LIMIT/OFFSET、
|
|
11
|
+
# Excel のストリーミング)を同じ表示コードで扱える。
|
|
12
|
+
class TableSheet
|
|
13
|
+
attr_reader :name, :header, :row_count, :column_count, :row_truncated, :column_truncated
|
|
14
|
+
|
|
15
|
+
def initialize(name:, header: nil, row_count: 0, column_count: 0,
|
|
16
|
+
row_truncated: false, column_truncated: false)
|
|
17
|
+
@name = name
|
|
18
|
+
@header = header
|
|
19
|
+
@row_count = row_count
|
|
20
|
+
@column_count = column_count
|
|
21
|
+
@row_truncated = row_truncated
|
|
22
|
+
@column_truncated = column_truncated
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @param offset [Integer] 0 始まりのデータ行番号
|
|
26
|
+
# @param limit [Integer] 取得する行数
|
|
27
|
+
# @return [Array<Array<String>>]
|
|
28
|
+
def fetch_rows(_offset, _limit)
|
|
29
|
+
raise NotImplementedError, "#{self.class} must implement fetch_rows"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# 読み込みに失敗した理由(成功時は nil)
|
|
33
|
+
def load_error
|
|
34
|
+
nil
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def empty?
|
|
38
|
+
row_count.zero?
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# @return [Array<String>, nil] 1 行
|
|
42
|
+
def row(index)
|
|
43
|
+
fetch_rows(index, 1).first
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# @return [String, nil] 生のセル値(表示用に畳んでいない値)
|
|
47
|
+
def cell(row_index, column_index)
|
|
48
|
+
values = row(row_index)
|
|
49
|
+
return nil unless values
|
|
50
|
+
|
|
51
|
+
values[column_index]
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# 列見出し(A, B, ... Z, AA, AB, ...)。Excel の列名と同じ規則。
|
|
55
|
+
def column_label(index)
|
|
56
|
+
return '' if index.negative?
|
|
57
|
+
|
|
58
|
+
label = ''
|
|
59
|
+
number = index
|
|
60
|
+
loop do
|
|
61
|
+
label = ((number % 26) + 65).chr + label
|
|
62
|
+
number = number / 26 - 1
|
|
63
|
+
break if number.negative?
|
|
64
|
+
end
|
|
65
|
+
label
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# `/` の絞り込みで条件に書ける列名(見出しが無ければ A, B, ...)
|
|
69
|
+
def filter_columns
|
|
70
|
+
Array.new(column_count) do |index|
|
|
71
|
+
title = header && header[index]
|
|
72
|
+
title.nil? || title.to_s.empty? ? column_label(index) : title.to_s
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# `/` の絞り込み。既定は読み込み済みの行を Ruby 側で判定する
|
|
77
|
+
# (SQLite のように読み込み側で絞り込める形式は差し替える)
|
|
78
|
+
# @param query [TableQuery]
|
|
79
|
+
# @return [TableSheet]
|
|
80
|
+
def filter(query)
|
|
81
|
+
rows = fetch_rows(0, row_count).select { |row| query.matches?(row, filter_columns) }
|
|
82
|
+
MemoryTableSheet.new(name: name, header: header, rows: rows,
|
|
83
|
+
row_truncated: row_truncated, column_truncated: column_truncated)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# ヘッダ行に表示する文字列("A name" / ヘッダなしなら "A")。
|
|
87
|
+
# 見出しはファイル由来なので、端末制御シーケンスを無害化してから返す。
|
|
88
|
+
def column_title(index)
|
|
89
|
+
title = header && header[index]
|
|
90
|
+
return column_label(index) if title.nil? || title.empty?
|
|
91
|
+
|
|
92
|
+
"#{column_label(index)} #{TextUtils.sanitize_for_display(title)}"
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# 行をメモリ上に保持するシート(CSV のような一括読み形式向け)
|
|
97
|
+
class MemoryTableSheet < TableSheet
|
|
98
|
+
def initialize(rows:, name:, header: nil, row_truncated: false, column_truncated: false)
|
|
99
|
+
@rows = rows
|
|
100
|
+
widths = rows.map(&:length)
|
|
101
|
+
widths << header.length if header
|
|
102
|
+
super(name: name, header: header, row_count: rows.length,
|
|
103
|
+
column_count: widths.max || 0,
|
|
104
|
+
row_truncated: row_truncated, column_truncated: column_truncated)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def fetch_rows(offset, limit)
|
|
108
|
+
return [] if offset.negative? || limit <= 0
|
|
109
|
+
|
|
110
|
+
@rows.slice(offset, limit) || []
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# 行を必要になってから読むシート。Excel のシートや SQLite のテーブルのように
|
|
115
|
+
# 読み出しに時間がかかる形式向けで、表示するシートだけを読む。
|
|
116
|
+
#
|
|
117
|
+
# loader は { rows:, header:, row_truncated:, column_truncated: } を返す。
|
|
118
|
+
#
|
|
119
|
+
# filter に Proc を渡すと、`/` の絞り込みを読み込み側に任せられる(SQLite は
|
|
120
|
+
# WHERE 付きの SELECT を発行するので、読み込み上限より後ろの行も探せる)。
|
|
121
|
+
class LazyTableSheet < TableSheet
|
|
122
|
+
def initialize(name:, filter: nil, &loader)
|
|
123
|
+
super(name: name)
|
|
124
|
+
@loader = loader
|
|
125
|
+
@filter_loader = filter
|
|
126
|
+
@loaded = false
|
|
127
|
+
@load_error = nil
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# 読み込み側で絞り込めるならそちらに任せ、できなければ Ruby 側で判定する
|
|
131
|
+
def filter(query)
|
|
132
|
+
return super if @filter_loader.nil?
|
|
133
|
+
|
|
134
|
+
loader = @filter_loader
|
|
135
|
+
# 1 件も残らないと見出しが返らないことがあるので、読み込み済みの見出しを引き継ぐ
|
|
136
|
+
# (列が消えてグリッドの枠まで無くならないようにする)
|
|
137
|
+
fallback_header = header
|
|
138
|
+
LazyTableSheet.new(name: name, filter: loader) do
|
|
139
|
+
result = loader.call(query) || {}
|
|
140
|
+
result[:header] ||= fallback_header
|
|
141
|
+
result
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def fetch_rows(offset, limit)
|
|
146
|
+
load!
|
|
147
|
+
return [] if offset.negative? || limit <= 0
|
|
148
|
+
|
|
149
|
+
@rows.slice(offset, limit) || []
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def header
|
|
153
|
+
load!
|
|
154
|
+
super
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def row_count
|
|
158
|
+
load!
|
|
159
|
+
super
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def column_count
|
|
163
|
+
load!
|
|
164
|
+
super
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def row_truncated
|
|
168
|
+
load!
|
|
169
|
+
super
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def column_truncated
|
|
173
|
+
load!
|
|
174
|
+
super
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def load_error
|
|
178
|
+
load!
|
|
179
|
+
@load_error
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def loaded?
|
|
183
|
+
@loaded
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
private
|
|
187
|
+
|
|
188
|
+
# 読めなかったシートは空として扱い、理由を load_error に残す。
|
|
189
|
+
# 描画中に呼ばれるため、ここで例外を出して UI を止めない。
|
|
190
|
+
def load!
|
|
191
|
+
return if @loaded
|
|
192
|
+
|
|
193
|
+
@loaded = true
|
|
194
|
+
apply(@loader.call || {})
|
|
195
|
+
rescue StandardError => e
|
|
196
|
+
apply({})
|
|
197
|
+
@load_error = e.message
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def apply(result)
|
|
201
|
+
@rows = Array(result[:rows])
|
|
202
|
+
@header = result[:header]
|
|
203
|
+
@row_count = @rows.length
|
|
204
|
+
widths = @rows.map(&:length)
|
|
205
|
+
widths << @header.length if @header
|
|
206
|
+
@column_count = widths.max || 0
|
|
207
|
+
@row_truncated = result[:row_truncated] ? true : false
|
|
208
|
+
@column_truncated = result[:column_truncated] ? true : false
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
# 1 ファイル分の表データ。複数シートを持つ形式(Excel / SQLite)にも対応する。
|
|
213
|
+
class TableDocument
|
|
214
|
+
attr_reader :sheets, :encoding, :byte_truncated, :label, :sheet_label
|
|
215
|
+
|
|
216
|
+
# @param sheet_label [String] シート一覧(/)の見出し。形式ごとの呼び名を使う
|
|
217
|
+
# (Excel は "Sheets"、SQLite は "Tables")
|
|
218
|
+
def initialize(sheets:, encoding: 'UTF-8', byte_truncated: false, label: nil,
|
|
219
|
+
sheet_label: 'Sheets')
|
|
220
|
+
@sheets = sheets
|
|
221
|
+
@encoding = encoding
|
|
222
|
+
@byte_truncated = byte_truncated
|
|
223
|
+
@label = label
|
|
224
|
+
@sheet_label = sheet_label
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# @return [Array<String>] シート名(表示順)
|
|
228
|
+
def sheet_names
|
|
229
|
+
@sheets.map(&:name)
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def sheet(index = 0)
|
|
233
|
+
@sheets[index]
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def sheet_count
|
|
237
|
+
@sheets.length
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def truncated?
|
|
241
|
+
@byte_truncated || @sheets.any? { |sheet| sheet.row_truncated || sheet.column_truncated }
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# 画面最下行に出す情報(行数・列数・文字コード・切り詰め)
|
|
245
|
+
# @param target [TableSheet, nil] 絞り込み後のシートなど、index のシート以外を使うとき
|
|
246
|
+
def status_line(index = 0, target = nil)
|
|
247
|
+
target ||= sheet(index)
|
|
248
|
+
return nil unless target
|
|
249
|
+
|
|
250
|
+
# シート名・テーブル名・読み込みエラー文はファイルや外部コマンド由来なので無害化する
|
|
251
|
+
parts = []
|
|
252
|
+
parts << "#{index + 1}/#{sheet_count} #{TextUtils.sanitize_for_display(target.name)}" if sheet_count > 1
|
|
253
|
+
parts << "#{target.row_count}#{'+' if target.row_truncated} rows"
|
|
254
|
+
parts << "#{target.column_count}#{'+' if target.column_truncated} cols"
|
|
255
|
+
parts << @encoding unless @encoding == 'UTF-8'
|
|
256
|
+
parts << 'byte limit reached' if @byte_truncated
|
|
257
|
+
parts << TextUtils.sanitize_for_display(target.load_error) if target.load_error
|
|
258
|
+
parts.join(' ')
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
end
|