/
resolution.go
177 lines (161 loc) · 4.76 KB
/
resolution.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
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
package main
import (
"errors"
"fmt"
"log/slog"
"math"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/srlehn/termimg"
"github.com/srlehn/termimg/internal/logx"
"github.com/srlehn/termimg/term"
)
var (
resolutionFormat string
)
func init() {
resolutionCmd.Flags().StringVarP(&resolutionFormat, `format`, `f`, ``, `format`)
rootCmd.AddCommand(resolutionCmd)
}
var resolutionCmd = &cobra.Command{
Use: resolutionCmdStr,
Short: `print terminal resolution`,
Long: `print terminal resolution
` + resolutionUsageStr + `
-f <fmt>, --format <fmt> print format - %<letter> verbs are replaced:
%c terminal resolution in cells (width)
%d terminal resolution in cells (height)
%e terminal resolution in pixels (width)
%f terminal resolution in pixels (height)
%a terminal cell resolution in pixels (width) (floating number)
%b terminal cell resolution in pixels (height) (floating number)`,
Args: cobra.NoArgs,
TraverseChildren: true,
Run: func(cmd *cobra.Command, args []string) {
run(resolutionFunc(cmd, args))
},
}
var (
resolutionCmdStr = "resolution"
resolutionUsageStr = `usage: ` + os.Args[0] + ` ` + resolutionCmdStr + ` (-f <format>)`
)
func resolutionFunc(cmd *cobra.Command, args []string) terminalSwapper {
return func(tm **term.Terminal) error {
opts := []term.Option{
logFileOption,
termimg.DefaultConfig,
}
tm2, err := term.NewTerminal(opts...)
if err != nil {
return err
}
defer tm2.Close()
*tm = tm2
fmtFlag := cmd.Flags().Lookup(`format`)
customFormat := fmtFlag != nil && fmtFlag.Changed
res := &resolution{}
resCellsX, resCellsY, errResCells := tm2.SizeInCells()
if customFormat {
res.errResCells = errResCells
if !logx.IsErr(errResCells, tm2, slog.LevelInfo) {
res.resCellsX = resCellsX
res.resCellsY = resCellsY
}
} else {
if !logx.IsErr(errResCells, tm2, slog.LevelInfo) && resCellsX != 0 && resCellsY != 0 {
fmt.Printf("terminal resolution: %dx%d (cells)\r\n", resCellsX, resCellsY)
}
}
resPixelsX, resPixelsY, errResPixels := tm2.SizeInPixels()
if customFormat {
res.errResPixels = errResPixels
if errResPixels == nil {
res.resPixelsX = resPixelsX
res.resPixelsY = resPixelsY
}
} else {
if !logx.IsErr(errResPixels, tm2, slog.LevelInfo) && resPixelsX != 0 && resPixelsY != 0 {
fmt.Printf("terminal resolution: %dx%d (pixels)\r\n", resPixelsX, resPixelsY)
}
}
// TODO tm.Window().Size()
cpw, cph, errCellRes := tm2.CellSize()
if customFormat {
res.errCellRes = errCellRes
if !logx.IsErr(errCellRes, tm2, slog.LevelInfo) {
if cpw >= 0 && !math.IsNaN(cpw) {
res.cpw = cpw
}
if cph >= 0 && !math.IsNaN(cph) {
res.cph = cph
}
}
var resRepeats []any
cnt := int(countFmtVerbs(resolutionFormat))
for i := 0; i < cnt; i++ {
resRepeats = append(resRepeats, res)
}
resolutionFormat = strings.NewReplacer(
`\\`, "\\",
`\t`, "\t",
`\n`, "\n",
`\r`, "\r",
).Replace(resolutionFormat)
fmt.Printf(resolutionFormat, resRepeats...)
} else {
if errCellRes == nil &&
cpw >= 1 && cph >= 1 &&
!math.IsNaN(cpw) && !math.IsNaN(cph) &&
!math.IsInf(cpw, 0) && !math.IsInf(cph, 0) {
fmt.Printf("terminal cell resolution: %.2fx%.2f (pixels)\r\n", cpw, cph)
}
}
err = errors.Join(errResCells, errResPixels, errCellRes)
return err
}
}
type resolution struct {
resCellsX, resCellsY uint
errResCells error
resCellsNeeded bool
resPixelsX, resPixelsY uint
errResPixels error
resPixelsNeeded bool
cpw, cph float64
errCellRes error
cellResNeeded bool
}
func (r *resolution) Format(f fmt.State, c rune) {
if r == nil {
return
}
switch c {
case 'c':
r.resCellsNeeded = true
fs := fmt.FormatString(f, c)
fmt.Fprintf(f, `%`+fs[:len(fs)-2]+`d`, r.resCellsX)
case 'd':
r.resCellsNeeded = true
fs := fmt.FormatString(f, c)
fmt.Fprintf(f, `%`+fs[:len(fs)-2]+`d`, r.resCellsY)
case 'e':
r.resPixelsNeeded = true
fs := fmt.FormatString(f, c)
fmt.Fprintf(f, `%`+fs[:len(fs)-2]+`d`, r.resPixelsX)
case 'f':
r.resPixelsNeeded = true
fs := fmt.FormatString(f, c)
fmt.Fprintf(f, `%`+fs[:len(fs)-2]+`d`, r.resPixelsY)
// TODO don't enforce precision
case 'a':
r.cellResNeeded = true
fs := fmt.FormatString(f, c)
fmt.Fprintf(f, `%`+fs[:len(fs)-2]+`.02f`, r.cpw)
case 'b':
r.cellResNeeded = true
fs := fmt.FormatString(f, c)
fmt.Fprintf(f, `%`+fs[:len(fs)-2]+`.02f`, r.cph)
}
}
func countFmtVerbs(s string) uint { return uint(strings.Count(strings.ReplaceAll(s, `%%`, ``), `%`)) }