Skip to content

Commit

Permalink
Merge pull request #24 from jingweno/color_opt
Browse files Browse the repository at this point in the history
Add options to control printing of colors
  • Loading branch information
owenthereal committed May 22, 2015
2 parents 0f65586 + db13eb0 commit 81a3b4a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 16 deletions.
43 changes: 34 additions & 9 deletions ccat.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,39 @@ import (
"github.com/jingweno/ccat/Godeps/_workspace/src/github.com/mattn/go-isatty"
)

func CCat(fname string, colorDefs ColorDefs, w io.Writer) error {
type CCatPrinter interface {
Print(r io.Reader, w io.Writer) error
}

type AutoColorPrinter struct {
ColorDefs ColorDefs
}

func (a AutoColorPrinter) Print(r io.Reader, w io.Writer) error {
if isatty.IsTerminal(uintptr(syscall.Stdout)) {
return ColorPrinter{a.ColorDefs}.Print(r, w)
} else {
return PlainTextPrinter{}.Print(r, w)
}
}

type ColorPrinter struct {
ColorDefs ColorDefs
}

func (c ColorPrinter) Print(r io.Reader, w io.Writer) error {
return CPrint(r, w, c.ColorDefs)
}

type PlainTextPrinter struct {
}

func (p PlainTextPrinter) Print(r io.Reader, w io.Writer) error {
_, err := io.Copy(w, r)
return err
}

func CCat(fname string, p CCatPrinter, w io.Writer) error {
var r io.Reader

if fname == readFromStdin {
Expand All @@ -32,12 +64,5 @@ func CCat(fname string, colorDefs ColorDefs, w io.Writer) error {
r = file
}

var err error
if isatty.IsTerminal(uintptr(syscall.Stdout)) {
err = CPrint(r, w, colorDefs)
} else {
_, err = io.Copy(w, r)
}

return err
return p.Print(r, w)
}
44 changes: 37 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,44 @@ import (
"github.com/jingweno/ccat/Godeps/_workspace/src/github.com/mattn/go-colorable"
)

const readFromStdin = "-"
const (
readFromStdin = "-"
)

func init() {
cli.AppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.Name}} [options] [file ...]
VERSION:
{{.Version}}
OPTIONS:
{{range .Flags}}{{.}}
{{end}}
Using color is auto both by default and with --color=auto. With --color=auto,
ccat emits color codes only when standard output is connected to a terminal.
`
}

func main() {
app := cli.NewApp()
app.Name = "ccat"
app.Usage = "Concatenate FILE(s), or standard input, to standard output with colorized output."
app.Usage = "Colorize FILE(s), or standard input, to standard output."
app.Version = Version
app.Author = ""
app.Email = ""
app.HideHelp = true
app.HideVersion = true
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "bg",
Value: "light",
Usage: `Set to "light" or "dark" depending on the terminal's background.`,
},
cli.StringFlag{
Name: "C, color",
Value: "auto",
Usage: `Colorize the output; Value can be "never", "always" or "auto".`,
},
}
app.Action = runCCat

Expand All @@ -39,6 +60,15 @@ func runCCat(c *cli.Context) {
colorDefs = LightColorDefs
}

var printer CCatPrinter
if c.String("color") == "always" {
printer = ColorPrinter{colorDefs}
} else if c.String("color") == "never" {
printer = PlainTextPrinter{}
} else {
printer = AutoColorPrinter{colorDefs}
}

fnames := c.Args()
// if there's no args, read from stdin
if len(fnames) == 0 {
Expand All @@ -47,7 +77,7 @@ func runCCat(c *cli.Context) {

stdout := colorable.NewColorableStdout()
for _, fname := range fnames {
err := CCat(fname, colorDefs, stdout)
err := CCat(fname, printer, stdout)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 81a3b4a

Please sign in to comment.