-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_windows.go
82 lines (66 loc) · 1.89 KB
/
print_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
package main
import (
"fmt"
"image/color"
"log/slog"
"os/exec"
"slices"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
"github.com/alexbrainman/printer"
)
func print(a fyne.App, w fyne.Window) {
printers, err := printer.ReadNames()
if err != nil {
slog.Error(err.Error())
dialog.NewError(err, w).Show()
return
}
entry := widget.NewSelectEntry(printers)
dialog.NewForm("Print", "OK", "Cancel", []*widget.FormItem{widget.NewFormItem("Printer", entry)}, func(b bool) {
if !b {
return
}
slog.Info("printing", "printer", entry.Text)
dlg := dialog.NewCustomWithoutButtons("Printing...", container.NewStack(canvas.NewRectangle(color.Transparent), widget.NewProgressBarInfinite()), w)
dlg.Show()
var lines []string
lines = append(lines, "AUTO-GENERATED by gecfg-editor version "+a.Metadata().Version)
lines = append(lines, "<github.com/MatusOllah/gecfg-editor>")
lines = append(lines, "")
lines = append(lines, "File name: "+openFileName)
lines = append(lines, "")
var keys []string
for k, _ := range theMap {
keys = append(keys, k)
}
slices.Sort(keys)
for _, k := range keys {
lines = append(lines, fmt.Sprintf("%s (%T) = %v", k, theMap[k], theMap[k]))
}
if err := printOneDocument(entry.Text, lines); err != nil {
slog.Error(err.Error())
dlg.Hide()
dialog.NewError(err, w).Show()
return
}
dlg.Hide()
}, w).Show()
}
func printOneDocument(printerName string, lines []string) error {
var _data []string
for _, line := range lines {
_data = append(_data, line+"`r`n")
}
data := strings.Join(_data, "")
cmd := exec.Command("powershell", "-Command", fmt.Sprintf("Out-Printer -Name \"%s\" -InputObject \"%s\"", printerName, data))
slog.Info("executing", "path", cmd.Path, "args", cmd.Args)
if err := cmd.Run(); err != nil {
return err
}
return nil
}