Skip to content

Commit

Permalink
Use terminal width when truncating list output
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwhitaker committed Jul 27, 2024
1 parent a5bddaf commit 0b2fab2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
16 changes: 13 additions & 3 deletions runny/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,20 @@ func (c *Config) PrintCommands() {

slices.Sort(names)
var separator = " "
maxLineLength := 80
if !term.IsTerminal(int(os.Stdout.Fd())) {
var maxLineLength int

if term.IsTerminal(int(os.Stdout.Fd())) {
if c.verbose {
maxLineLength = 0
} else {
width, _, err := term.GetSize(int(os.Stdin.Fd()))
if err == nil {
maxLineLength = width
}
}
} else {
separator = "\t"
maxLineLength = 40
maxLineLength = 0
}

for _, name := range names {
Expand Down
2 changes: 1 addition & 1 deletion runny/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func commandStringToSingleLine(command string, maxlength int) string {
trimmedLines = append(trimmedLines, strings.TrimSpace(line))
}
result := strings.Join(trimmedLines, "; ")
if len(result) > maxlength {
if maxlength > 0 && len(result) > maxlength {
result = result[:maxlength-1] + "…"
}
return result
Expand Down
1 change: 1 addition & 0 deletions runny/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestCommandStringToSingleLine(t *testing.T) {
{input: Input{command: "foo bar wibble", maxLength: 10}, expected: "foo bar w…"},
{input: Input{command: "foo\nbar", maxLength: 80}, expected: "foo; bar"},
{input: Input{command: " foo \n bar ", maxLength: 80}, expected: "foo; bar"},
{input: Input{command: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam convallis. Nunc lacus. Curabitur nunc mauris, commodo vel, eleifend in, ornare sit amet, felis. Nullam mi neque, feugiat et, porttitor vitae, pharetra non, lacus. Fusce imperdiet sem quis dui.", maxLength: 0}, expected: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam convallis. Nunc lacus. Curabitur nunc mauris, commodo vel, eleifend in, ornare sit amet, felis. Nullam mi neque, feugiat et, porttitor vitae, pharetra non, lacus. Fusce imperdiet sem quis dui."},
}
for _, p := range params {
output := commandStringToSingleLine(p.input.command, p.input.maxLength)
Expand Down

0 comments on commit 0b2fab2

Please sign in to comment.