Skip to content
Closed
Show file tree
Hide file tree
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
18 changes: 17 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"runtime"
"strings"
"time"
"unicode"
"unicode/utf8"

"github.com/charmbracelet/fang"
"github.com/charmbracelet/lipgloss/v2"
Expand Down Expand Up @@ -229,7 +231,12 @@ func Execute(m Metadata) {

// remove margins so that it matches other pterm.error "style"
// we should add them back later as it looks cleaner
errorTextStyle := styles.ErrorText.UnsetMargins()
//
// fang's default transform title-cases the first word, which
// mangles messages that start with a path or identifier
// ("/tmp/out.gz" becomes "/Tmp/Out.gz"); uppercase only the
// first rune instead.
errorTextStyle := styles.ErrorText.UnsetMargins().Transform(upperFirst)

// Keep command errors on fang's error stream, normally stderr. This
// gives curl-like commands a quiet stdout for response bodies and
Expand Down Expand Up @@ -259,6 +266,15 @@ func Execute(m Metadata) {
// isUsageError is a hack to detect usage errors.
// See: https://github.com/spf13/cobra/pull/2266
// from github.com/charmbracelet/fang/help.go
// upperFirst uppercases the first rune of s, leaving the rest untouched.
func upperFirst(s string) string {
r, size := utf8.DecodeRuneInString(s)
if r == utf8.RuneError {
return s
}
return string(unicode.ToUpper(r)) + s[size:]
}

func isUsageError(err error) bool {
s := err.Error()
for _, prefix := range []string{
Expand Down
15 changes: 15 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,18 @@ func TestResolveProjectSelection(t *testing.T) {
assert.Equal(t, "", resolveProjectSelection(""))
})
}

func TestUpperFirst(t *testing.T) {
tests := []struct {
in string
want string
}{
{"unknown flag: --foo", "Unknown flag: --foo"},
{"/tmp/out.jsonl.gz already exists", "/tmp/out.jsonl.gz already exists"},
{"--start must be before --end", "--start must be before --end"},
{"", ""},
}
for _, test := range tests {
assert.Equal(t, test.want, upperFirst(test.in))
}
}
Loading