-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_windows.go
More file actions
99 lines (89 loc) · 2.4 KB
/
Copy pathprint_windows.go
File metadata and controls
99 lines (89 loc) · 2.4 KB
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
package main
import (
"errors"
"fmt"
"os/exec"
"path/filepath"
"strconv"
"github.com/FurqanSoftware/pog"
"github.com/alexbrainman/printer"
)
func printPDF(cfg Config, name string) error {
var cmd *exec.Cmd
switch cfg.Windows.PrintHelper {
case PrintHelperSumatraPDF:
args := []string{"-print-to-default", "-silent", "-exit-when-done", name}
if cfg.Printer.Name != "" {
args = []string{"-print-to", cfg.Printer.Name, "-silent", "-exit-when-done", name}
}
cmd = exec.Command(cfg.Windows.PrintHelperPath, args...)
default:
args := []string{name}
if cfg.Printer.Name != "" {
args = append(args, strconv.Quote(cfg.Printer.Name))
}
cmd = exec.Command(cfg.Windows.PrintHelperPath, args...)
}
_, err := cmd.Output()
if err != nil {
var exiterr *exec.ExitError
if errors.As(err, &exiterr) {
return printDispatchError{fmt.Errorf("%w: %s", err, ellipsize(string(exiterr.Stderr), 50, "..."))}
}
return printDispatchError{err}
}
return nil
}
var printHelperExe = map[PrintHelper]string{
PrintHelperPDFtoPrinter: `.\PDFtoPrinter.exe`,
PrintHelperSumatraPDF: `.\SumatraPDF.exe`,
}
func resolvePrintHelper(cfg *Config) {
if cfg.Windows.PrintHelper == PrintHelperAuto {
for _, h := range []PrintHelper{PrintHelperPDFtoPrinter, PrintHelperSumatraPDF} {
if _, err := exec.LookPath(printHelperExe[h]); err == nil {
cfg.Windows.PrintHelper = h
goto PinPath
}
}
pog.Fatal("Missing dependency: could not find PDFtoPrinter.exe or SumatraPDF.exe")
return
}
PinPath:
exe, ok := printHelperExe[cfg.Windows.PrintHelper]
if !ok {
pog.Fatal("Invalid print helper: " + string(cfg.Windows.PrintHelper))
}
p, err := exec.LookPath(exe)
if err != nil {
pog.Fatal("Missing dependency: could not find " + exe)
}
cfg.Windows.PrintHelperPath, _ = filepath.Abs(p)
}
func checkDependencies(cfg Config) {}
var printHelperNames = map[PrintHelper]string{
PrintHelperPDFtoPrinter: "PDFtoPrinter",
PrintHelperSumatraPDF: "SumatraPDF",
}
func logPlatformConfigSummary(cfg Config) {
name := printHelperNames[cfg.Windows.PrintHelper]
if name == "" {
name = string(cfg.Windows.PrintHelper)
}
pog.Infof("∟ Print Helper: %s", name)
}
func checkPrinter(cfg Config) error {
names, err := printer.ReadNames()
if err != nil {
return err
}
for _, name := range names {
if cfg.Printer.Name == "" {
return nil
}
if name == cfg.Printer.Name {
return nil
}
}
return errPrinterNotExist
}