-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
120 lines (106 loc) · 3.16 KB
/
main.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
package main
import (
"fmt"
"io"
"log"
"os"
tea "github.com/charmbracelet/bubbletea"
"github.com/jon4hz/fztea/flipperui"
"github.com/jon4hz/fztea/internal/version"
"github.com/jon4hz/fztea/recfz"
"github.com/muesli/coral"
mcoral "github.com/muesli/mango-coral"
"github.com/muesli/roff"
)
var rootFlags struct {
port string
screenshotResolution string
fgColor string
bgColor string
}
var rootCmd = &coral.Command{
Use: "fztea",
Short: "TUI to interact with your flipper zero",
Version: version.Version,
Run: root,
}
func init() {
rootCmd.PersistentFlags().StringVarP(&rootFlags.port, "port", "p", "", "serial port to connect to (default: auto-detected)")
rootCmd.PersistentFlags().StringVar(&rootFlags.screenshotResolution, "screenshot-resolution", "1024x512", "screenshot resolution")
rootCmd.PersistentFlags().StringVar(&rootFlags.fgColor, "fg-color", "#000000", "foreground color")
rootCmd.PersistentFlags().StringVar(&rootFlags.bgColor, "bg-color", "#FF8C00", "background color")
rootCmd.AddCommand(serverCmd, versionCmd, manCmd)
}
func root(cmd *coral.Command, _ []string) {
// parse screenshot resolution
screenshotResolution, err := parseScreenshotResolution()
if err != nil {
log.Fatalf("failed to parse screenshot resolution: %s", err)
}
screenUpdates := make(chan flipperui.ScreenMsg)
fz, err := recfz.NewFlipperZero(
recfz.WithContext(cmd.Context()),
recfz.WithPort(rootFlags.port),
recfz.WithStreamScreenCallback(flipperui.UpdateScreen(screenUpdates)),
recfz.WithLogger(log.New(io.Discard, "", 0)),
)
if err != nil {
log.Fatal(err)
}
defer fz.Close()
if err := fz.Connect(); err != nil {
log.Fatal(err)
}
m := model{
flipper: flipperui.New(fz, screenUpdates,
flipperui.WithScreenshotResolution(screenshotResolution.width, screenshotResolution.height),
flipperui.WithFgColor(rootFlags.fgColor),
flipperui.WithBgColor(rootFlags.bgColor),
),
}
if _, err := tea.NewProgram(m, tea.WithMouseCellMotion()).Run(); err != nil {
log.Fatalln(err)
}
}
func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
var manCmd = &coral.Command{
Use: "man",
Short: "generates the manpages",
SilenceUsage: true,
DisableFlagsInUseLine: true,
Hidden: true,
Args: coral.NoArgs,
RunE: func(_ *coral.Command, _ []string) error {
manPage, err := mcoral.NewManPage(1, rootCmd)
if err != nil {
return err
}
_, err = fmt.Fprint(os.Stdout, manPage.Build(roff.NewDocument()))
return err
},
}
var versionCmd = &coral.Command{
Use: "version",
Short: "Print the version info",
Run: func(_ *coral.Command, _ []string) {
fmt.Printf("Version: %s\n", version.Version)
fmt.Printf("Commit: %s\n", version.Commit)
fmt.Printf("Date: %s\n", version.Date)
fmt.Printf("Build by: %s\n", version.BuiltBy)
},
}
func parseScreenshotResolution() (struct {
width int
height int
}, error) {
var screenshotResolution struct {
width int
height int
}
_, err := fmt.Sscanf(rootFlags.screenshotResolution, "%dx%d", &screenshotResolution.width, &screenshotResolution.height)
return screenshotResolution, err
}