Compare commits

...

2 commits

Author SHA1 Message Date
ba7c3ac52d
print error messages from run using new printer 2024-06-17 11:04:20 +01:00
fa9f33a5de
set min term width 2024-06-17 10:29:37 +01:00
2 changed files with 26 additions and 5 deletions

View file

@ -23,7 +23,6 @@ var (
func main() { func main() {
if err := run(); err != nil { if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v.\n", err)
os.Exit(1) os.Exit(1)
} }
} }
@ -181,13 +180,18 @@ func run() error {
exe, ok := executorMap[command] exe, ok := executorMap[command]
if !ok { if !ok {
err := executor.UnknownCommandError{Command: command}
printer.PrintFailure(err.Error() + ".")
flag.Usage() flag.Usage()
return executor.UnknownCommandError{Command: command} return err
} }
if err := executor.Execute(exe, args); err != nil { if err := executor.Execute(exe, args); err != nil {
return fmt.Errorf("(%s) %w", command, err) printer.PrintFailure("(" + command + ") " + err.Error() + ".")
return err
} }
return nil return nil

View file

@ -12,6 +12,10 @@ import (
"time" "time"
) )
const (
minTerminalWidth = 40
)
type theme struct { type theme struct {
reset string reset string
boldblue string boldblue string
@ -19,6 +23,8 @@ type theme struct {
green string green string
boldgreen string boldgreen string
grey string grey string
red string
boldred string
} }
type Printer struct { type Printer struct {
@ -47,6 +53,12 @@ func NewPrinter(
green: "\033[32m", green: "\033[32m",
boldgreen: "\033[32;1m", boldgreen: "\033[32;1m",
grey: "\033[90m", grey: "\033[90m",
red: "\033[31m",
boldred: "\033[31;1m",
}
if maxTerminalWidth < minTerminalWidth {
maxTerminalWidth = minTerminalWidth
} }
return &Printer{ return &Printer{
@ -65,7 +77,7 @@ func NewPrinter(
} }
func (p Printer) PrintSuccess(text string) { func (p Printer) PrintSuccess(text string) {
success := p.theme.green + p.successSymbol + p.theme.reset success := p.theme.boldgreen + p.successSymbol + p.theme.reset
if p.noColor { if p.noColor {
success = p.successSymbol success = p.successSymbol
} }
@ -74,7 +86,12 @@ func (p Printer) PrintSuccess(text string) {
} }
func (p Printer) PrintFailure(text string) { func (p Printer) PrintFailure(text string) {
printToStderr(p.failureSymbol + " " + text) failure := p.theme.boldred + p.failureSymbol + p.theme.reset
if p.noColor {
failure = p.failureSymbol
}
printToStderr(failure + " " + text + "\n")
} }
func (p Printer) PrintInfo(text string) { func (p Printer) PrintInfo(text string) {