Borg/pkg/ui/non_interactive_prompter.go
google-labs-jules[bot] 52c0fa6b6d feat: Improve test coverage and refactor for testability
This commit introduces a significant refactoring of the `cmd` package to improve testability and increases test coverage across the application.

Key changes include:
- Refactored Cobra commands to use `RunE` for better error handling and testing.
- Extracted business logic from command handlers into separate, testable functions.
- Added comprehensive unit tests for the `cmd`, `compress`, `github`, `logger`, and `pwa` packages.
- Added tests for missing command-line arguments, as requested.
- Implemented the `borg all` command to clone all public repositories for a GitHub user or organization.
- Restored and improved the `collect pwa` functionality.
- Removed duplicate code and fixed various bugs.
- Addressed a resource leak in the `all` command.
- Improved error handling in the `pwa` package.
- Refactored `main.go` to remove duplicated logic.
- Fixed several other minor bugs and inconsistencies.
2025-11-03 18:25:04 +00:00

73 lines
1.3 KiB
Go

package ui
import (
"fmt"
"os"
"sync"
"time"
"github.com/fatih/color"
"github.com/mattn/go-isatty"
)
type NonInteractivePrompter struct {
stopChan chan struct{}
quoteFunc func() (string, error)
started bool
mu sync.Mutex
stopOnce sync.Once
}
func NewNonInteractivePrompter(quoteFunc func() (string, error)) *NonInteractivePrompter {
return &NonInteractivePrompter{
stopChan: make(chan struct{}),
quoteFunc: quoteFunc,
}
}
func (p *NonInteractivePrompter) Start() {
p.mu.Lock()
if p.started {
p.mu.Unlock()
return
}
p.started = true
p.mu.Unlock()
//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)
continue
}
c := color.New(color.FgGreen)
c.Println(quote)
}
}
}()
}
func (p *NonInteractivePrompter) Stop() {
if p.IsInteractive() {
return
}
p.stopOnce.Do(func() {
close(p.stopChan)
})
}
func (p *NonInteractivePrompter) IsInteractive() bool {
return isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
}