-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_windows.go
107 lines (84 loc) · 1.66 KB
/
main_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
winPrint "github.com/alexbrainman/printer"
)
var printer *Printer
func main() {
fmt.Println("Point Of Sale Ticket Printer")
fmt.Println("Version 1.03.3 for Windows - Amura Systems")
fmt.Println()
var path string
var cups bool
flag.StringVar(&path, "p", "", "The printer name")
flag.BoolVar(&cups, "cups", false, "Use CUPS")
list := flag.Bool("l", false, "List printers")
flag.Parse()
if *list {
names, err := winPrint.ReadNames()
if err != nil {
log.Fatal(err.Error())
}
fmt.Println("Printers:")
for _, n := range names {
fmt.Println(" - " + n)
}
return
}
c, err := NewConfig("posprinter.conf")
if err != nil {
log.Fatal(err)
}
if path == "" {
path = c.String("path", "")
cups = c.Bool("cups", false)
} else {
c.Set("path", path)
c.Set("cups", strconv.FormatBool(cups))
c.Save()
}
if path == "" {
usage()
}
printer = &Printer{Name: path}
listen()
}
func usage() {
fmt.Println(`Usage: posprinter -p name`)
os.Exit(1)
}
func (p *Printer) Write(b []byte) error {
d, err := decode(b)
if err != nil {
return err
}
if p.CUPS {
return p.sendToCUPS(d)
}
pr, err := winPrint.Open(p.Name)
if err != nil {
log.Fatalf("Open failed: %v", err)
}
defer pr.Close()
err = pr.StartDocument("POS_Ticket", "RAW")
if err != nil {
log.Fatalf("StartDocument failed: %v", err)
}
defer pr.EndDocument()
err = pr.StartPage()
if err != nil {
log.Fatalf("StartPage failed: %v", err)
}
if _, err := pr.Write(d); err != nil {
log.Fatalf("Write failed: %v", err)
}
err = pr.EndPage()
if err != nil {
log.Fatalf("EndPage failed: %v", err)
}
return nil
}