Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to control printing of colors #24

Merged
merged 1 commit into from
May 22, 2015
Merged
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
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