Improve vertical sizing for small terminals, fixes #137 (#138)

* Improve vertical sizing for small terminals, fixes #137

* Add tests for tiny terminals + update test goldens for tiny terminals after compact sizing
This commit is contained in:
David Dworken 2023-12-03 21:56:45 -08:00 committed by GitHub
parent acdcd9569a
commit 8b83e2bbb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 3 deletions

View File

@ -1639,6 +1639,21 @@ func testTui_resize(t *testing.T) {
out = stripTuiCommandPrefix(t, out)
testutils.CompareGoldens(t, out, "TestTui-SmallTerminal")
// Check the output when the size is tiny
out = captureTerminalOutputWithShellNameAndDimensions(t, tester, tester.ShellName(), 100, 15, []TmuxCommand{
{Keys: "hishtory SPACE tquery ENTER"},
})
out = stripTuiCommandPrefix(t, out)
testutils.CompareGoldens(t, out, "TestTui-TinyTerminal")
// Check the output when the size is tiny and the help page is open
out = captureTerminalOutputWithShellNameAndDimensions(t, tester, tester.ShellName(), 100, 15, []TmuxCommand{
{Keys: "hishtory SPACE tquery ENTER"},
{Keys: "C-h"},
})
out = stripTuiCommandPrefix(t, out)
testutils.CompareGoldens(t, out, "TestTui-TinyTerminalHelp")
// Check that it resizes after the terminal size is adjusted
manuallySubmitHistoryEntry(t, userSecret, testutils.MakeFakeHistoryEntry("echo 'cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc'"))
out = captureTerminalOutputWithShellNameAndDimensions(t, tester, tester.ShellName(), 100, 20, []TmuxCommand{

View File

@ -11,5 +11,7 @@ Search Query: > 1234567890qwertyuip1234567890qwertyuip1234567890qwertyuip1234567
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
hiSHtory: Search your shell history • ctrl+h help

View File

@ -11,5 +11,7 @@ Search Query: > ls
│ │
│ │
│ │
│ │
│ │
└────────────────────────────────────────────────────────────────────────────────────────────┘
hiSHtory: Search your shell history • ctrl+h help

View File

@ -0,0 +1,12 @@
Search Query: > ls
┌────────────────────────────────────────────────────────────────────────────────────────────┐
│ Hostname CWD Timestamp Runtime Exit Code Command │
│────────────────────────────────────────────────────────────────────────────────────────────│
│ localhost /tmp/ Oct 17 2022 21:43:21 PDT 3s 2 echo 'aaaaaa bbbb' │
│ localhost /tmp/ Oct 17 2022 21:43:16 PDT 3s 2 ls ~/ │
│ │
│ │
│ │
└────────────────────────────────────────────────────────────────────────────────────────────┘
hiSHtory: Search your shell history • ctrl+h help

View File

@ -0,0 +1,12 @@
Search Query: > ls
┌────────────────────────────────────────────────────────────────────────────────────────────┐
│ Hostname CWD Timestamp Runtime Exit Code Command │
│────────────────────────────────────────────────────────────────────────────────────────────│
│ localhost /tmp/ Oct 17 2022 21:43:21 PDT 3s 2 echo 'aaaaaa bbbb' │
│ localhost /tmp/ Oct 17 2022 21:43:16 PDT 3s 2 ls ~/ │
hiSHtory: Search your shell history
↑ scroll up
← move left
enter select an entry
ctrl+x select an entry and cd into that directory

View File

@ -425,13 +425,33 @@ func (m model) View() string {
}
additionalMessagesStr := strings.Join(additionalMessages, "\n") + "\n"
helpView := m.help.View(keys)
return fmt.Sprintf("\n%s%s\nSearch Query: %s\n\n%s\n", additionalMessagesStr, m.banner, m.queryInput.View(), renderNullableTable(m)) + helpView
additionalSpacing := "\n"
if isCompactHeightMode() {
additionalSpacing = ""
}
return fmt.Sprintf("%s%s%s%sSearch Query: %s\n\n%s\n", additionalSpacing, additionalMessagesStr, m.banner, additionalSpacing, m.queryInput.View(), renderNullableTable(m, helpView)) + helpView
}
func renderNullableTable(m model) string {
func isCompactHeightMode() bool {
_, height, err := getTerminalSize()
if err != nil {
hctx.GetLogger().Infof("got err=%v when retrieving terminal dimensions, assuming the terminal is reasonably tall", err)
return false
}
return height < 25
}
func renderNullableTable(m model, helpText string) string {
if m.table == nil {
return strings.Repeat("\n", TABLE_HEIGHT+3)
}
helpTextLen := strings.Count(helpText, "\n")
if isCompactHeightMode() && helpTextLen > 1 {
// If the help text is expanded, and this is a small window, then we truncate the table so that the help text displays on top of it
lines := strings.Split(baseStyle.Render(m.table.View()), "\n")
truncated := lines[:len(lines)-helpTextLen]
return strings.Join(truncated, "\n")
}
return baseStyle.Render(m.table.View())
}
@ -632,7 +652,11 @@ func makeTable(ctx context.Context, rows []table.Row) (table.Model, error) {
if err != nil {
return table.Model{}, err
}
tableHeight := min(TABLE_HEIGHT, terminalHeight-12)
tuiSize := 12
if isCompactHeightMode() {
tuiSize -= 2
}
tableHeight := min(TABLE_HEIGHT, terminalHeight-tuiSize)
t := table.New(
table.WithColumns(columns),
table.WithRows(rows),