Skip to content
Browse files

Add terminal package

  • Loading branch information...
1 parent a09f255 commit 3b7fb03cdb87a44182105429a4858776d24047f6 Meng Zhang committed
Showing with 168 additions and 28 deletions.
  1. +1 −0 .gitignore
  2. +11 −7 README.md
  3. +50 −21 colors.go → color/color.go
  4. +15 −0 example/hello.go
  5. +91 −0 terminal.go
View
1 .gitignore
@@ -0,0 +1 @@
+example/example
View
18 README.md
@@ -1,20 +1,24 @@
-## Colors ##
-Colors is a simple golang package that provides basic functions on colorful outputing in terminal.
-![Golang with colors](http://farm7.staticflickr.com/6051/6382022437_1f60b4130f.jpg)
+## Terminal ##
+Terminal is a simple golang package that provides basic terminal handling.
-Colors wraps color/format functions provided by [ANSI escape code](http://en.wikipedia.org/wiki/ANSI_escape_code)
+Terminal wraps color/format functions provided by [ANSI escape code](http://en.wikipedia.org/wiki/ANSI_escape_code)
## Usage ##
```go
package main
import (
- "github.com/wsxiaoys/colors"
+ "github.com/wsxiaoys/terminal"
+ "github.com/wsxiaoys/terminal/color"
)
func main() {
- colors.Println("@rHello world")
- colors.Printf("@rHello @b%s", "world!")
+ terminal.Stdout.Color("y").
+ Print("Hello world").Nl().
+ Reset().
+ Colorf("@{kW}Hello world\n")
+
+ color.Print("@rHello world")
}
```
Check the godoc result for more details.
View
71 colors.go → color/color.go
@@ -1,35 +1,43 @@
// The colors package provide a simple way to bring colorful charcaters to terminal interface.
//
// This example will output the text with a Blue foreground and a Black background
-// colors.Println("@{bK}Example Text")
+// color.Println("@{bK}Example Text")
//
// This one will output the text with a red foreground
-// colors.Println("@rExample Text")
+// color.Println("@rExample Text")
//
// This one will escape the @
-// colors.Println("@@")
+// color.Println("@@")
//
// Full color syntax code
// @{rgbcmykwRGBCMYKW} foreground/background color
+// r/R Red
+// g/G Green
+// b/B Blue
+// c/C Cyan
+// m/M Magenta
+// y/Y Yellow
+// k/K Black
+// w/W White
// @{|} Reset format style
-// @{!./_} Bold / Dim / Italic / underline
+// @{!./_} Bold / Dim / Italic / Underline
// @{^&} Blink / Fast blink
// @{?} Reverse the foreground and background color
// @{-} Hide the text
// Note some of the functions are not widely supported, like "Fast blink" and "Italic".
-package colors
+package color
import (
"bytes"
"fmt"
+ "io"
"log"
)
-// Escape character for color syntax
-const escapeChar = '@'
-
-// Short for reset to default style
-var resetChar = fmt.Sprintf("%c|", escapeChar)
+const (
+ EscapeChar = '@' // Escape character for color syntax
+ ResetCode = "\033[0m" // Short for reset to default style
+)
// Mapping from character to concrete escape code.
var codeMap = map[int]int{
@@ -65,7 +73,7 @@ var codeMap = map[int]int{
}
// Compile color syntax string like "rG" to escape code.
-func colorMap(x string) string {
+func Colorize(x string) string {
attr := 0
fg := 39
bg := 49
@@ -96,7 +104,7 @@ func compileColorSyntax(input, output *bytes.Buffer) {
switch i {
default:
- output.WriteString(colorMap(string(i)))
+ output.WriteString(Colorize(string(i)))
case '{':
color := bytes.NewBufferString("")
for {
@@ -109,9 +117,9 @@ func compileColorSyntax(input, output *bytes.Buffer) {
}
color.WriteRune(i)
}
- output.WriteString(colorMap(color.String()))
- case escapeChar:
- output.WriteRune(escapeChar)
+ output.WriteString(Colorize(color.String()))
+ case EscapeChar:
+ output.WriteRune(EscapeChar)
}
}
@@ -132,7 +140,7 @@ func compile(x string) string {
switch i {
default:
output.WriteRune(i)
- case escapeChar:
+ case EscapeChar:
compileColorSyntax(input, output)
}
}
@@ -150,35 +158,56 @@ func compileValues(a *[]interface{}) {
// Similar to fmt.Print, will reset the color at the end.
func Print(a ...interface{}) (int, error) {
- a = append(a, resetChar)
+ a = append(a, ResetCode)
compileValues(&a)
return fmt.Print(a...)
}
// Similar to fmt.Println, will reset the color at the end.
func Println(a ...interface{}) (int, error) {
- a = append(a, resetChar)
+ a = append(a, ResetCode)
compileValues(&a)
return fmt.Println(a...)
}
// Similar to fmt.Printf, will reset the color at the end.
func Printf(format string, a ...interface{}) (int, error) {
- format += resetChar
+ format += ResetCode
format = compile(format)
return fmt.Printf(format, a...)
}
+// Similar to fmt.Fprint, will reset the color at the end.
+func Fprint(w io.Writer, a ...interface{}) (int, error) {
+ a = append(a, ResetCode)
+ compileValues(&a)
+ return fmt.Fprint(w, a...)
+}
+
+// Similar to fmt.Fprintln, will reset the color at the end.
+func Fprintln(w io.Writer, a ...interface{}) (int, error) {
+ a = append(a, ResetCode)
+ compileValues(&a)
+ return fmt.Fprintln(w, a...)
+}
+
+// Similar to fmt.Fprintf, will reset the color at the end.
+func Fprintf(w io.Writer, format string, a ...interface{}) (int, error) {
+ format += ResetCode
+ format = compile(format)
+ return fmt.Fprintf(w, format, a...)
+}
+
// Similar to fmt.Sprint, will reset the color at the end.
func Sprint(a ...interface{}) string {
- a = append(a, resetChar)
+ a = append(a, ResetCode)
compileValues(&a)
return fmt.Sprint(a...)
}
// Similar to fmt.Sprintf, will reset the color at the end.
func Sprintf(format string, a ...interface{}) string {
- format += resetChar
+ format += ResetCode
format = compile(format)
return fmt.Sprintf(format, a...)
}
View
15 example/hello.go
@@ -0,0 +1,15 @@
+package main
+
+import (
+ "github.com/wsxiaoys/terminal"
+ "github.com/wsxiaoys/terminal/color"
+)
+
+func main() {
+ terminal.Stdout.Color("y").
+ Print("Hello world").Nl().
+ Reset().
+ Colorf("@{kW}Hello world\n")
+
+ color.Print("@rHello world")
+}
View
91 terminal.go
@@ -0,0 +1,91 @@
+package terminal
+
+import (
+ "fmt"
+ "github.com/wsxiaoys/terminal/color"
+ "io"
+ "log"
+ "os"
+)
+
+type TerminalWriter struct {
+ io.Writer
+}
+
+var (
+ Stdout = &TerminalWriter{os.Stdout}
+ Stderr = &TerminalWriter{os.Stderr}
+)
+
+func (w *TerminalWriter) checkOutput(s string) {
+ if _, err := io.WriteString(w, s); err != nil {
+ log.Fatal("Write to %v failed.", w)
+ }
+}
+
+func (w *TerminalWriter) Color(syntax string) *TerminalWriter {
+ escapeCode := color.Colorize(syntax)
+ w.checkOutput(escapeCode)
+ return w
+}
+
+func (w *TerminalWriter) Reset() *TerminalWriter {
+ w.checkOutput(color.ResetCode)
+ return w
+}
+
+func (w *TerminalWriter) Print(a ...interface{}) *TerminalWriter {
+ fmt.Fprint(w, a...)
+ return w
+}
+
+func (w *TerminalWriter) Nl(a ...interface{}) *TerminalWriter {
+ length := 1
+ if len(a) > 0 {
+ length = a[0].(int)
+ }
+ for i := 0; i < length; i++ {
+ w.checkOutput("\n")
+ }
+ return w
+}
+
+func (w *TerminalWriter) Colorf(format string, a ...interface{}) *TerminalWriter {
+ w.checkOutput(color.Sprintf(format, a...))
+ return w
+}
+
+func (w *TerminalWriter) Clear() *TerminalWriter {
+ w.checkOutput("\033[2J")
+ return w
+}
+
+func (w *TerminalWriter) ClearLine() *TerminalWriter {
+ w.checkOutput("\033[2K")
+ return w
+}
+
+func (w *TerminalWriter) Move(x, y int) *TerminalWriter {
+ w.checkOutput(fmt.Sprintf("\033[%d;%dH", x, y))
+ return w
+}
+
+func (w *TerminalWriter) Up(x int) *TerminalWriter {
+ w.checkOutput(fmt.Sprintf("\033[%dA", x))
+ return w
+}
+
+func (w *TerminalWriter) Down(x int) *TerminalWriter {
+ w.checkOutput(fmt.Sprintf("\033[%dB", x))
+ return w
+}
+
+func (w *TerminalWriter) Right(x int) *TerminalWriter {
+ w.checkOutput(fmt.Sprintf("\033[%dC", x))
+ return w
+}
+
+func (w *TerminalWriter) Left(x int) *TerminalWriter {
+ w.checkOutput(fmt.Sprintf("\033[%dD", x))
+ return w
+}

0 comments on commit 3b7fb03

Please sign in to comment.
Something went wrong with that request. Please try again.