Borg/pkg/ui/non_interactive_prompter.go
google-labs-jules[bot] edc0d6a18c feat: Implement non-interactive quote display
This commit introduces a non-interactive mode for the `collect` commands. When running in a non-interactive session, the progress bar is replaced with a series of thematic Borg quotes, printed in matrix-green text.

The quotes are sourced from a `quotes.json` file, which is embedded into the binary using Go's `embed` package. The `pkg/ui` package now contains a `NonInteractivePrompter` that detects the session type and displays the quotes accordingly. The `collect` commands have been updated to use this new prompter, and the underlying `vcs` and `website` packages have been made more robust to handle cases where a progress bar is not provided.
2025-11-02 16:34:45 +00:00

60 lines
1.1 KiB
Go

package ui
import (
"fmt"
"os"
"time"
"github.com/fatih/color"
"github.com/mattn/go-isatty"
)
type NonInteractivePrompter struct {
stopChan chan bool
quoteFunc func() (string, error)
}
func NewNonInteractivePrompter(quoteFunc func() (string, error)) *NonInteractivePrompter {
return &NonInteractivePrompter{
stopChan: make(chan bool),
quoteFunc: quoteFunc,
}
}
func (p *NonInteractivePrompter) Start() {
if p.IsInteractive() {
return // Don't start in interactive mode
}
go func() {
ticker := time.NewTicker(3 * time.Second)
defer ticker.Stop()
for {
select {
case <-p.stopChan:
return
case <-ticker.C:
quote, err := p.quoteFunc()
if err != nil {
fmt.Println("Error getting quote:", err)
return
}
c := color.New(color.FgGreen)
c.Println(quote)
}
}
}()
}
func (p *NonInteractivePrompter) Stop() {
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
return
}
p.stopChan <- true
}
func (p *NonInteractivePrompter) IsInteractive() bool {
return isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
}