Version 0.5.0: pick, Zeilen auf dem Terminal wählen

This commit is contained in:
wkh
2026-10-11 22:25:45 +02:00
parent e7a7577900
commit d2c4343136
9 changed files with 468 additions and 4 deletions
+105
View File
@@ -618,3 +618,108 @@ class MaskTest < RdbCase
assert_equal '', table
end
end
class PickTest < RdbCase
def session(multi: true, height: 3)
t = Rdb::Table.read(File.open(File.join(DEMO, 'inventory')))
Rdb::Pick::Session.new(t.head, t.rows, multi: multi).tap { |x| x.resize(height, 80) }
end
def keys(sess, words) = Rdb::Pick.script(words).map { |k| sess.feed(k) }.compact.last
def test_mark_and_select
s = session
assert_equal :accept, keys(s, 'down space down down space enter')
assert_equal %w[2 5], s.selected.map(&:first)
end
def test_single_mode_takes_the_cursor_row_and_ignores_space
s = session(multi: false)
assert_equal :accept, keys(s, 'down down space enter')
assert_equal %w[3], s.selected.map(&:first)
end
def test_scrolling_keeps_the_cursor_in_view
s = session(height: 3)
keys(s, 'down down down down')
assert_equal [4, 2], [s.cursor, s.top]
keys(s, 'end')
assert_equal [6, 4], [s.cursor, s.top]
keys(s, 'pgup')
assert_equal 3, s.cursor
keys(s, 'home')
assert_equal [0, 0], [s.cursor, s.top]
end
def test_filter_and_clear
s = session
keys(s, '/clam')
assert_predicate s, :typing?
keys(s, 'enter')
assert_equal [2], s.visible
keys(s, '/ esc')
assert_equal 7, s.visible.size
end
def test_marks_survive_a_filter_and_a_all_toggles
s = session
keys(s, 'space /scales enter')
assert_equal [0], s.marked
keys(s, 'a')
assert_equal [0, 6], s.marked.sort
keys(s, 'a')
assert_equal [0], s.marked
end
def test_cancel_and_empty_choice
assert_equal :cancel, keys(session, 'esc')
assert_equal :cancel, keys(session, '/zzz enter enter')
end
def test_command_with_keys_script_is_a_filter
out = rdb('pick', '-m', '-k', 'down space down down space enter', file: 'inventory')
assert_equal %w[2 5], data(out).map(&:first)
assert_equal '292.55', data(rdb('total', 'Value', stdin: out)).first[3]
end
def test_command_cancelled_exits_1_with_no_output
out, _err, st = run3('pick', '-k', 'esc', stdin: File.read(File.join(@dir, 'inventory')))
refute st.success?
assert_equal '', out
end
def test_command_reads_a_list
out = rdb('pick', '-k', 'down enter', stdin: rdb('tabletolist', file: 'owners'))
assert_equal [%w[3 bob]], data(out)
end
def run_on_pty(*key_strokes)
out = File.join(@dir, 'pick.out')
result = nil
PTY.spawn({ 'PATH' => "#{BIN}:#{ENV['PATH']}" }, 'sh', '-c', "cd #{@dir} && pick -m < inventory > pick.out") do |r, w, pid|
sleep 0.5
key_strokes.each do |k|
w.write k
sleep 0.1
r.read_nonblock(65_536) rescue nil
end
sleep 0.3
r.read_nonblock(65_536) rescue nil
_, st = Process.wait2(pid)
result = [File.exist?(out) ? File.read(out) : '', st.exitstatus]
end
result
end
def test_pick_on_a_terminal
table, status = run_on_pty("\e[B", ' ', "\e[B", ' ', "\r")
assert_equal 0, status
assert_equal %w[2 4], data(table).map(&:first)
end
def test_pick_on_a_terminal_cancel
table, status = run_on_pty("\e[B", "\e")
assert_equal 1, status
assert_equal '', table
end
end