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

CLI output: non-tty mode improvements. #1750

Merged
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
19 changes: 10 additions & 9 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,28 @@ const (
var CliCheckLogSniffer = &cliCheckLogSniffer{}

var (
isTTY bool

checkLoggerChan chan string
stopChan chan bool
)

func init() {
isTTY = term.IsTerminal(int(os.Stdin.Fd()))
}

func PrintBanner() {
fmt.Print(banner)
}

type cliCheckLogSniffer struct{}

func isTTY() bool {
return term.IsTerminal(int(os.Stdin.Fd()))
}

func updateRunningCheckLine(checkName string, stopChan <-chan bool) {
startTime := time.Now()

// Local string var to save the last received log line from the running check.
lastCheckLogLine := ""

tickerPeriod := 1 * time.Second
if !isTTY {
if !isTTY() {
// Increase it to avoid flooding the text output.
tickerPeriod = 10 * time.Second
}
Expand Down Expand Up @@ -115,7 +113,7 @@ func printRunningCheckLine(checkName string, startTime time.Time, logLine string

elapsedTime := time.Since(startTime).Round(time.Second)
line := "[ " + CheckResultTagRunning + " ] " + checkName + " (" + elapsedTime.String() + ")"
if !isTTY {
if !isTTY() {
fmt.Print(line + "\n")
return
}
Expand All @@ -132,6 +130,9 @@ func printRunningCheckLine(checkName string, startTime time.Time, logLine string

// Implements the io.Write for the checks' custom handler for slog.
func (c *cliCheckLogSniffer) Write(p []byte) (n int, err error) {
if !isTTY() {
return len(p), nil
}
// Send to channel, or ignore it in case the channel is not ready or is closed.
// This way we avoid blocking the whole program.
select {
Expand Down Expand Up @@ -181,7 +182,7 @@ func PrintCheckRunning(checkName string) {
checkLoggerChan = make(chan string)

line := "[ " + CheckResultTagRunning + " ] " + checkName
if !isTTY {
if !isTTY() {
line += "\n"
}

Expand Down
Loading