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,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
|
|
@@ -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
|