Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ go install github.com/charmbracelet/gum@latest
* [`spin`](#spin): Display spinner while running a command
* [`style`](#style): Apply coloring, borders, spacing to text
* [`table`](#table): Render a table of data
* [`tail`](#tail): Provides a means of streaming -n of lines from stdin
* [`write`](#write): Prompt for long-form text
* [`log`](#log): Log messages to output

Expand Down Expand Up @@ -262,6 +263,14 @@ gum style \

<img src="https://github.com/charmbracelet/gum/assets/42545625/67468acf-b3e0-4e78-bd89-360739eb44fa" width="600" alt="Bubble Gum, So sweet and so fresh!" />

## Tail

Render a subset of lines from stdin for easy log monitoring without cluttering the terminal output.

```bash
{ sudo apt update -y && sudo apt upgrade -y && sudo apt autoremove -y; } | gum tail -n 3
```

## Join

Combine text vertically or horizontally. Use this command with `gum style` to
Expand Down
11 changes: 11 additions & 0 deletions gum.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/charmbracelet/gum/spin"
"github.com/charmbracelet/gum/style"
"github.com/charmbracelet/gum/table"
"github.com/charmbracelet/gum/tail"
"github.com/charmbracelet/gum/write"
)

Expand Down Expand Up @@ -196,6 +197,16 @@ type Gum struct {
//
Table table.Options `cmd:"" help:"Render a table of data"`

// Tail provides a means of streaming a number of lines from stdin
//
// It is useful for showing a subset of output from a process without clogging up the terminal.
//
// Let's watch apt update:
//
// $ { sudo apt update -y && sudo apt upgrade -y && sudo apt autoremove -y; } | gum tail -n 3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// $ { sudo apt update -y && sudo apt upgrade -y && sudo apt autoremove -y; } | gum tail -n 3
// Let's watch apt update:
//
// $ { sudo apt update -y && sudo apt upgrade -y && sudo apt autoremove -y; } | gum tail -n 3

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change applied in 0087173

//
Tail tail.Options `cmd:"" help:"Tail the -n of lines from stdin"`

// Write provides a shell script interface for the text area bubble.
// https://github.com/charmbracelet/bubbles/tree/master/textarea
//
Expand Down
58 changes: 58 additions & 0 deletions tail/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package tail

import (
"bufio"
"fmt"
"os"
)

// Run executes the tail command.
func (o Options) Run() error {
linesChan := make(chan string)

go func() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
linesChan <- scanner.Text()
}
close(linesChan)
}()

lastLines := make([]string, 0, o.NumLines)
frameHeight := 0

for line := range linesChan {
lastLines = appendLine(lastLines, line, o.NumLines)

// Clear previous frame
if frameHeight > 0 {
fmt.Printf("\033[%dA", frameHeight) // Move cursor up
}

// Print current frame
for _, l := range lastLines {
fmt.Printf("\r\033[K%s\n", l) // Clear line + print
}

frameHeight = len(lastLines)
}

// After finishing, clear the live frame and print only final lines
if frameHeight > 0 {
fmt.Printf("\033[%dA", frameHeight)
}
fmt.Print("\033[J") // Clear everything below cursor
for _, l := range lastLines {
fmt.Println(l)
}

return nil
}

func appendLine(lines []string, line string, maxLines int) []string {
lines = append(lines, line)
if len(lines) > maxLines {
lines = lines[1:]
}
return lines
}
5 changes: 5 additions & 0 deletions tail/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package tail

type Options struct {
NumLines int `short:"n" default:"4" help:"Number of lines you want to tail"`
}