Skip to content

Commit

Permalink
Support Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiyas committed Sep 8, 2014
1 parent c1c9723 commit 674e76c
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 23 deletions.
6 changes: 4 additions & 2 deletions cmd/qrc/qrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
}
}
23 changes: 15 additions & 8 deletions lib/aa.go
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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()
}
23 changes: 12 additions & 11 deletions lib/sixel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)

Expand All @@ -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
Expand All @@ -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\\")
}
2 changes: 2 additions & 0 deletions tty/tty.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !windows

package tty

import (
Expand Down
2 changes: 1 addition & 1 deletion tty/tty_bsd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build darwin freebsd
// +build darwin freebsd netbsd openbsd

package tty

Expand Down
2 changes: 2 additions & 0 deletions tty/tty_da1.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !windows

package tty

import (
Expand Down
2 changes: 1 addition & 1 deletion tty/tty_posix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !darwin,!windows,!freebsd
// +build !darwin,!windows,!freebsd,!netbsd,!openbsd

package tty

Expand Down
36 changes: 36 additions & 0 deletions tty/tty_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build windows

package tty

import (
Expand All @@ -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
Expand All @@ -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
}

0 comments on commit 674e76c

Please sign in to comment.