feat(shell): upgrade Ctrl+P / pick to fzf-like interactive TUI

This commit is contained in:
Anya Tokugawa committed 2026-11-30 23:21:40 +09:00
1 parent 6d2bbe57ff
commit 9a0ebd874f
2 files changed
+322 -24

No files matched your search

+85
View File
@@ -1436,6 +1436,91 @@ class RemoteTests(unittest.TestCase):
with self.assertRaisesRegex(Failure, '0600'):
m['_load_shell_history'](FakeReadline(), hist_file)
def test_fuzzy_pick_tui_navigation_caching_and_fallback(self):
# Full-width display truncation avoids terminal line wrapping on CJK text.
self.assertEqual(m['_truncate_display']('日本語タイトル', 7), '日本語…')
self.assertEqual(m['_truncate_display']('日本語タイトル', 6), '日本…')
self.assertEqual(m['_truncate_display']('abc\x1bdef', 10), 'abcdef')
client = Client('https://example.test', 'token')
suggest_calls = []
listing_calls = []
def fake_json(route, **kwargs):
suggest_calls.append(route)
if 'q=guide' in route:
return {'items': [
{'path': '/docs/guide', 'label': 'User Guide', 'kind': 'page'},
{'path': '/docs/guideline', 'label': 'Guideline', 'kind': 'page'},
]}
return {'items': []}
def fake_listing(path, prefix=''):
listing_calls.append(path)
if path == '/':
return {'entries': [
{'name': 'docs', 'path': '/docs', 'kind': 'folder'},
{'name': 'readme', 'path': '/readme', 'kind': 'file'},
]}
if path == '/docs':
return {'entries': [
{'name': 'intro', 'path': '/docs/intro', 'kind': 'file'},
]}
return {'entries': []}
with patch.object(client, 'json', side_effect=fake_json), \
patch.object(client, 'listing', side_effect=fake_listing):
# Type 'g', 'u', backspace -> 'g', clear, Tab into /docs, then Enter on /docs/intro.
keys = iter([
('CHAR', 'g'), ('CHAR', 'u'), 'BACKSPACE', 'CLEAR',
'ENTER',
])
out = io.StringIO()
res = m['_run_tui_picker'](
client, '/', '', key_reader=lambda: next(keys),
out=out, term_size=lambda: os.terminal_size((80, 12)),
)
self.assertEqual(res, '/docs')
rendered = out.getvalue()
self.assertIn('\x1b[H\x1b[J', rendered)
self.assertIn('\x1b[7m>', rendered)
# Drill down into /docs folder with Tab, then navigate Down/Up without duplicate HTTP calls.
suggest_calls.clear()
listing_calls.clear()
keys2 = iter([
'TAB', 'DOWN', 'UP', 'ENTER',
])
res2 = m['_run_tui_picker'](
client, '/', '', key_reader=lambda: next(keys2),
out=io.StringIO(), term_size=lambda: os.terminal_size((80, 12)),
)
self.assertEqual(res2, '/docs/intro')
self.assertEqual(listing_calls, ['/', '/docs'])
self.assertEqual(suggest_calls, [])
# Multi-step typing and ESC cancel returns None and uses query cache.
suggest_calls.clear()
keys3 = iter([
('CHAR', 'g'), ('CHAR', 'u'), ('CHAR', 'i'), ('CHAR', 'd'), ('CHAR', 'e'),
'DOWN', 'UP', 'ESC',
])
res3 = m['_run_tui_picker'](
client, '/', '', key_reader=lambda: next(keys3),
out=io.StringIO(), term_size=lambda: os.terminal_size((80, 12)),
)
self.assertIsNone(res3)
self.assertEqual(len([c for c in suggest_calls if c.endswith('q=guide')]), 1)
# TERM=dumb falls back to _line_fuzzy_pick even when both stdin and stdout are TTYs.
profile = type('P', (), {'name': 'test', 'data': {'cwd': '/', 'expires': time.time() + 600}})()
fake_tty = type('TTY', (), {'isatty': lambda self: True})()
with patch.object(m['sys'], 'stdin', fake_tty), \
patch.object(m['sys'], 'stdout', fake_tty), \
patch.dict(os.environ, {'TERM': 'dumb'}), \
patch.dict(m['fuzzy_pick'].__globals__, {'_line_fuzzy_pick': lambda *a, **kw: '/docs/intro', '_tui_fuzzy_pick': lambda *a, **kw: self.fail('should not invoke TUI on TERM=dumb')}):
self.assertEqual(m['fuzzy_pick'](client, profile, initial_line='cat '), 'cat /docs/intro')
def oidc_integration():
fixture=json.load(sys.stdin)