Skip to content

Commit

Permalink
Merge pull request #81 from koki-develop/list-themes-without-color
Browse files Browse the repository at this point in the history
--list-themes output now has color disabled when redirecting
  • Loading branch information
koki-develop authored Oct 11, 2024
2 parents 9df24e8 + 71dfc5a commit cf00095
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ var rootCmd = &cobra.Command{
Short: "cat alternative written in Go",
Long: "cat alternative written in Go.",
RunE: func(cmd *cobra.Command, args []string) error {
ist := term.IsTerminal(int(os.Stdout.Fd()))

switch {
case flagListLangs:
return gat.PrintLanguages(os.Stdout)
case flagListFormats:
return gat.PrintFormats(os.Stdout)
case flagListThemes:
return gat.PrintThemes(os.Stdout)
return gat.PrintThemes(os.Stdout, ist)
}

if strings.HasPrefix(flagFormat, "terminal") {
ist := term.IsTerminal(int(os.Stdout.Fd()))
if !ist {
if !flagForceColor {
flagTheme = "noop"
Expand Down
52 changes: 29 additions & 23 deletions internal/gat/gat.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,42 +287,48 @@ func PrintFormats(w io.Writer) error {
return tw.Flush()
}

func PrintThemes(w io.Writer) error {
src := `package main
func PrintThemes(w io.Writer, withColor bool) error {
if withColor {
src := `package main
import "fmt"
func main() {
fmt.Println("hello world")
}`

for _, t := range styles.List() {
fmt.Fprintf(w, "\x1b[1m%s\x1b[0m\n\n", t)
for _, t := range styles.List() {
fmt.Fprintf(w, "\x1b[1m%s\x1b[0m\n\n", t)

g, err := New(&Config{
Language: "go",
Theme: t,
Format: "terminal256",
})
if err != nil {
return err
}
g, err := New(&Config{
Language: "go",
Theme: t,
Format: "terminal256",
})
if err != nil {
return err
}

buf := new(bytes.Buffer)
if err := g.Print(buf, strings.NewReader(src)); err != nil {
return err
}
buf := new(bytes.Buffer)
if err := g.Print(buf, strings.NewReader(src)); err != nil {
return err
}

// indent source
sc := bufio.NewScanner(buf)
for sc.Scan() {
if _, err := fmt.Fprintf(w, "\t%s\n", sc.Text()); err != nil {
// indent source
sc := bufio.NewScanner(buf)
for sc.Scan() {
if _, err := fmt.Fprintf(w, "\t%s\n", sc.Text()); err != nil {
return err
}
}

if _, err := fmt.Fprintln(w); err != nil {
return err
}
}

if _, err := fmt.Fprintln(w); err != nil {
return err
} else {
for _, t := range styles.List() {
fmt.Fprintln(w, t)
}
}

Expand Down

0 comments on commit cf00095

Please sign in to comment.