-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.go
More file actions
217 lines (179 loc) · 4.95 KB
/
system.go
File metadata and controls
217 lines (179 loc) · 4.95 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
)
// tickMsg is sent periodically to refresh the print queue
type tickMsg time.Time
// jobsRefreshedMsg contains the refreshed list of print jobs
type jobsRefreshedMsg struct {
jobs []PrintJob
}
// PrinterInfo holds the default printer name and status
type PrinterInfo struct {
Name string
Status string // "idle", "printing", etc.
}
// getDefaultPrinter returns info about the default printer
func getDefaultPrinter() PrinterInfo {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "lpstat", "-p", "-d")
output, err := cmd.Output()
if err != nil {
return PrinterInfo{Name: "Unknown", Status: ""}
}
info := PrinterInfo{}
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "printer ") {
// "printer EPSON_ET_2810_Series is idle. enabled since..."
parts := strings.Fields(line)
if len(parts) >= 4 {
info.Name = parts[1]
info.Status = parts[3]
// Remove trailing period
info.Status = strings.TrimSuffix(info.Status, ".")
}
}
}
if info.Name == "" {
info.Name = "No printer"
}
return info
}
// tickCmd returns a command that sends a tickMsg every second
func tickCmd() tea.Cmd {
return tea.Tick(time.Second, func(t time.Time) tea.Msg {
return tickMsg(t)
})
}
// refreshJobsCmd runs lpstat asynchronously and returns the jobs
func refreshJobsCmd() tea.Cmd {
return func() tea.Msg {
// This runs in a background goroutine, not blocking the UI
jobs := getSystemPrintJobs()
return jobsRefreshedMsg{jobs: jobs}
}
}
// getSystemPrintJobs retrieves the current print queue from the system
// Uses lpq which shows job titles (filenames) set via lp -t
func getSystemPrintJobs() []PrintJob {
// Add timeout to prevent hanging when print spooler is stuck
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "lpq", "-a")
output, err := cmd.Output()
if err != nil {
return []PrintJob{}
}
var jobs []PrintJob
lines := strings.Split(string(output), "\n")
// lpq output format:
// Rank Owner Job File(s) Total Size
// active adrian 210 filename.pdf 155648 bytes
// 1st adrian 212 another.pdf 1024 bytes
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
// Skip header line
if strings.HasPrefix(line, "Rank") {
continue
}
// Parse: rank owner job filename size "bytes"
parts := strings.Fields(line)
if len(parts) < 5 {
continue
}
rank := parts[0]
// Skip if not a job line (active, 1st, 2nd, etc.)
if rank != "active" && !strings.HasSuffix(rank, "st") &&
!strings.HasSuffix(rank, "nd") && !strings.HasSuffix(rank, "rd") &&
!strings.HasSuffix(rank, "th") {
continue
}
jobNum := parts[2]
// Filename is everything between job number and size
// Find the "bytes" suffix to locate size
bytesIdx := -1
for i := len(parts) - 1; i >= 0; i-- {
if parts[i] == "bytes" {
bytesIdx = i
break
}
}
if bytesIdx < 4 {
continue
}
// Size is the field before "bytes"
size, _ := strconv.ParseInt(parts[bytesIdx-1], 10, 64)
// Filename is fields 3 to (bytesIdx-2)
fileName := strings.Join(parts[3:bytesIdx-1], " ")
if fileName == "" {
fileName = fmt.Sprintf("Job %s", jobNum)
}
job := PrintJob{
ID: jobNum,
FileName: fileName,
Size: size,
Status: rank,
}
jobs = append(jobs, job)
}
return jobs
}
// cancelPrintJob cancels a specific print job
func cancelPrintJob(jobID string) error {
cmd := exec.Command("cancel", jobID)
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("failed to cancel job %s: %v - %s", jobID, err, stderr.String())
}
return nil
}
// addToPrintQueue adds a file to the print queue
func addToPrintQueue(filePath string) error {
// Check if file exists
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return fmt.Errorf("file does not exist: %s", filePath)
}
// Send to printer using lp with -t to set job title (filename)
fileName := filepath.Base(filePath)
cmd := exec.Command("lp", "-t", fileName, filePath)
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("failed to print %s: %v - %s", filePath, err, stderr.String())
}
return nil
}
// openFile opens a file with the default application
func openFile(filePath string) error {
if filePath == "" {
return nil
}
cmd := exec.Command("open", filePath)
return cmd.Start()
}
// openFolder opens the containing folder of a file
func openFolder(filePath string) error {
if filePath == "" {
return nil
}
dir := filepath.Dir(filePath)
cmd := exec.Command("open", dir)
return cmd.Start()
}