-
Notifications
You must be signed in to change notification settings - Fork 352
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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" | ||||||||||
) | ||||||||||
|
||||||||||
|
@@ -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 | ||||||||||
// Useful for showing a subset of output from a process without clogging up the terminal | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
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 | ||||||||||
// | ||||||||||
|
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 | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
func (o Options) Run() error { | ||||||||||||||||||
// Channel to communicate lines read from stdin | ||||||||||||||||||
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") | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
// printLines prints the last n lines to stdout | ||||||||||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
} |
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"` | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.