From 674e76cc503c2943499df0a00fc91e932f4c5a6b Mon Sep 17 00:00:00 2001 From: SATOH Fumiyasu Date: Mon, 8 Sep 2014 17:25:20 +0900 Subject: [PATCH] Support Windows --- cmd/qrc/qrc.go | 6 ++++-- lib/aa.go | 23 +++++++++++++++-------- lib/sixel.go | 23 ++++++++++++----------- tty/tty.go | 2 ++ tty/tty_bsd.go | 2 +- tty/tty_da1.go | 2 ++ tty/tty_posix.go | 2 +- tty/tty_windows.go | 36 ++++++++++++++++++++++++++++++++++++ 8 files changed, 73 insertions(+), 23 deletions(-) diff --git a/cmd/qrc/qrc.go b/cmd/qrc/qrc.go index c0acac4..3db135a 100644 --- a/cmd/qrc/qrc.go +++ b/cmd/qrc/qrc.go @@ -3,6 +3,7 @@ package main import ( "bufio" "code.google.com/p/rsc/qr" + "github.com/mattn/go-colorable" "github.com/jessevdk/go-flags" "os" @@ -64,8 +65,9 @@ func main() { da1, err := tty.GetDeviceAttributes1(os.Stdout) if err == nil && da1[tty.DA1_SIXEL] { - qrc.PrintSixel(code, opts.Inverse) + qrc.PrintSixel(os.Stdout, code, opts.Inverse) } else { - qrc.PrintAA(code, opts.Inverse) + stdout := colorable.NewColorableStdout() + qrc.PrintAA(stdout, code, opts.Inverse) } } diff --git a/lib/aa.go b/lib/aa.go index fd1431d..2639104 100644 --- a/lib/aa.go +++ b/lib/aa.go @@ -1,12 +1,17 @@ package qrc import ( + "bufio" "code.google.com/p/rsc/qr" "fmt" "github.com/mgutz/ansi" + "io" ) -func PrintAA(code *qr.Code, inverse bool) { +func PrintAA(w_in io.Writer, code *qr.Code, inverse bool) { + // Buffering required for Windows (go-colorable) support + w := bufio.NewWriterSize(w_in, 1024) + reset := ansi.ColorCode("reset") black := ansi.ColorCode(":black") white := ansi.ColorCode(":white") @@ -16,25 +21,27 @@ func PrintAA(code *qr.Code, inverse bool) { line := white + fmt.Sprintf("%*s", code.Size*2+2, "") + reset + "\n" - fmt.Print(line) + fmt.Fprint(w, line) for y := 0; y < code.Size; y++ { - fmt.Print(white, " ") + fmt.Fprint(w, white, " ") color_prev := white for x := 0; x < code.Size; x++ { if code.Black(x, y) { if color_prev != black { - fmt.Print(black) + fmt.Fprint(w, black) color_prev = black } } else { if color_prev != white { - fmt.Print(white) + fmt.Fprint(w, white) color_prev = white } } - fmt.Print(" ") + fmt.Fprint(w, " ") } - fmt.Print(white, " ", reset, "\n") + fmt.Fprint(w, white, " ", reset, "\n") + w.Flush() } - fmt.Print(line) + fmt.Fprint(w, line) + w.Flush() } diff --git a/lib/sixel.go b/lib/sixel.go index 45ade49..1eb1c15 100644 --- a/lib/sixel.go +++ b/lib/sixel.go @@ -3,15 +3,16 @@ package qrc import ( "code.google.com/p/rsc/qr" "fmt" + "io" ) -func PrintSixel(code *qr.Code, inverse bool) { +func PrintSixel(w io.Writer, code *qr.Code, inverse bool) { black := "0" white := "1" - fmt.Print( + fmt.Fprint(w, "\x1BPq", - "#", black, ";2;0;0;0" , + "#", black, ";2;0;0;0", "#", white, ";2;100;100;100", ) @@ -20,10 +21,10 @@ func PrintSixel(code *qr.Code, inverse bool) { } line := "#" + white + "!" + fmt.Sprintf("%d", (code.Size+2)*6) + "~" - fmt.Print(line, "-") + fmt.Fprint(w, line, "-") for x := 0; x < code.Size; x++ { - fmt.Print("#", white) + fmt.Fprint(w, "#", white) color := white run := 6 var current string @@ -34,19 +35,19 @@ func PrintSixel(code *qr.Code, inverse bool) { current = white } if current != color { - fmt.Print("#", color, "!", run, "~") + fmt.Fprint(w, "#", color, "!", run, "~") color = current run = 0 } run += 6 } if color == white { - fmt.Printf("#%s!%d~", white, run+6) + fmt.Fprintf(w, "#%s!%d~", white, run+6) } else { - fmt.Printf("#%s!%d~#%s!6~", color, run, white) + fmt.Fprintf(w, "#%s!%d~#%s!6~", color, run, white) } - fmt.Print("-") + fmt.Fprint(w, "-") } - fmt.Print(line) - fmt.Print("\x1B\\") + fmt.Fprint(w, line) + fmt.Fprint(w, "\x1B\\") } diff --git a/tty/tty.go b/tty/tty.go index 3b35a53..3a47b5e 100644 --- a/tty/tty.go +++ b/tty/tty.go @@ -1,3 +1,5 @@ +// +build !windows + package tty import ( diff --git a/tty/tty_bsd.go b/tty/tty_bsd.go index ecac769..17cbb1a 100644 --- a/tty/tty_bsd.go +++ b/tty/tty_bsd.go @@ -1,4 +1,4 @@ -// +build darwin freebsd +// +build darwin freebsd netbsd openbsd package tty diff --git a/tty/tty_da1.go b/tty/tty_da1.go index d329c7e..bfc8377 100644 --- a/tty/tty_da1.go +++ b/tty/tty_da1.go @@ -1,3 +1,5 @@ +// +build !windows + package tty import ( diff --git a/tty/tty_posix.go b/tty/tty_posix.go index c40a641..35881ea 100644 --- a/tty/tty_posix.go +++ b/tty/tty_posix.go @@ -1,4 +1,4 @@ -// +build !darwin,!windows,!freebsd +// +build !darwin,!windows,!freebsd,!netbsd,!openbsd package tty diff --git a/tty/tty_windows.go b/tty/tty_windows.go index 3fd1641..c0454fa 100644 --- a/tty/tty_windows.go +++ b/tty/tty_windows.go @@ -1,3 +1,5 @@ +// +build windows + package tty import ( @@ -10,6 +12,34 @@ var ( kernel32 = syscall.MustLoadDLL("kernel32.dll") ) +const ( + DA1_132_COLUMNS = 0 + DA1_PRINTER_PORT = 0 + DA1_SIXEL = 0 + DA1_SELECTIVE_ERASE = 0 + DA1_DRCS = 0 + DA1_SOFT_CHARACTER_SET = 0 + DA1_UDKS = 0 + DA1_USER_DEFINED_KEYS = 0 + DA1_NRCS = 0 + DA1_NATIONAL_REPLACEMENT_CHARACTER_SETS = 0 + DA1_SCS = 0 + DA1_YUGOSLAVIAN = 0 + DA1_TECHNICAL_CHARACTER_SET = 0 + DA1_WINDOWING_CAPABILITY = 0 + DA1_HORIZONTAL_SCROLLING = 0 + DA1_GREEK = 0 + DA1_TURKISH = 0 + DA1_ISO_LATIN2_CHARACTER_SET = 0 + DA1_PCTERM = 0 + DA1_SOFT_KEY_MAP = 0 + DA1_ASCII_EMULATION = 0 + DA1_CLASS4 = 0 + DA1_MAX = 0 +) + +type DeviceAttributes1 [1]bool + // IsTty checks if the given fd is a tty func IsTty(file *os.File) bool { var st uint32 @@ -19,3 +49,9 @@ func IsTty(file *os.File) bool { return r1 != 0 && err != nil } + +func GetDeviceAttributes1(file *os.File) (DeviceAttributes1, error) { + var da1 DeviceAttributes1 + + return da1, nil +}