Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

434 Add Command to Tail -n Lines #544

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
8 changes: 8 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,13 @@ type Gum struct {
//
Table table.Options `cmd:"" help:"Render a table of data"`

// Tail
// Provides a means of streaming -n of lines from stdin
Comment on lines +200 to +201
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
// Tail
// Provides a means of streaming -n of lines from stdin
// Tail provides a means of streaming a number of lines from stdin.

// Useful for showing a subset of output from a process without clogging up the terminal
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
// Useful for showing a subset of output from a process without clogging up the terminal
// It is useful for showing a subset of output from a process without clogging up the terminal.

//
// $ { 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

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
62 changes: 62 additions & 0 deletions tail/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package tail

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

// Run executes the tail command

Check failure on line 9 in tail/command.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)
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
// Run executes the tail command
// Run executes the tail command.

func (o Options) Run() error {
// Channel to communicate lines read from stdin

Check failure on line 11 in tail/command.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
lines := make(chan string)

// Read from stdin in a separate goroutine
go func() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lines <- scanner.Text()
}
close(lines)
}()

// Slice to store the last n lines
lastLines := make([]string, 0, o.NumLines)

// Continuously read lines from stdin and update the last n lines
for line := range lines {
lastLines = appendLine(lastLines, line, o.NumLines)
clearScreen()
printLines(lastLines, o.NumLines)
}

return nil
}

// appendLine appends a line to the slice, maintaining the maximum number of lines.
func appendLine(lines []string, line string, maxLines int) []string {
lines = append(lines, line)
if len(lines) > maxLines {
lines = lines[1:]
}
return lines
}

func clearScreen() {
fmt.Print("\033[H\033[2J")
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
fmt.Print("\033[H\033[2J")
fmt.Print(ansi.EraseDisplay(2))

}

// printLines prints the last n lines to stdout

Check failure on line 49 in tail/command.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)
func printLines(lines []string, n int) {
// Print an extra newline for separation of previous shell output and the tailing
fmt.Println()

// Print the last n lines
start := len(lines) - n
if start < 0 {
start = 0
}
for i := start; i < len(lines); i++ {
fmt.Println(lines[i])
}
Comment on lines +55 to +61
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
start := len(lines) - n
if start < 0 {
start = 0
}
for i := start; i < len(lines); i++ {
fmt.Println(lines[i])
}
fmt.Println(strings.Join(lines, "\n"))

}
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 {

Check warning on line 3 in tail/options.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type Options should have comment or be unexported (revive)
NumLines int `short:"n" default:"4" help:"Number of lines you want to tail"`

Check failure on line 4 in tail/options.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
}
Loading