|
| 1 | +package ink |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "sync" |
| 8 | + "sync/atomic" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +var DefaultFontHeight = 14 |
| 13 | + |
| 14 | +type RunFunc func(ctx context.Context, w io.Writer) error |
| 15 | + |
| 16 | +func newLogWriter(log *Log, update func()) *logWriter { |
| 17 | + return &logWriter{log: log, update: update} |
| 18 | +} |
| 19 | + |
| 20 | +type logWriter struct { |
| 21 | + log *Log |
| 22 | + update func() |
| 23 | +} |
| 24 | + |
| 25 | +func (w *logWriter) Write(p []byte) (int, error) { |
| 26 | + defer w.update() |
| 27 | + return w.log.Write(p) |
| 28 | +} |
| 29 | +func (w *logWriter) draw() { |
| 30 | + w.log.Draw() |
| 31 | +} |
| 32 | +func (w *logWriter) close() { |
| 33 | + w.log.Close() |
| 34 | +} |
| 35 | + |
| 36 | +func newCliApp(fnc RunFunc, c RunConfig) *cliApp { |
| 37 | + return &cliApp{cli: fnc, conf: c} |
| 38 | +} |
| 39 | + |
| 40 | +type cliApp struct { |
| 41 | + redraws int32 // atomic |
| 42 | + |
| 43 | + conf RunConfig |
| 44 | + cli RunFunc |
| 45 | + |
| 46 | + wg sync.WaitGroup |
| 47 | + err error |
| 48 | + stop func() |
| 49 | + |
| 50 | + log *logWriter |
| 51 | + |
| 52 | + stopNet func() |
| 53 | + |
| 54 | + rmu sync.Mutex |
| 55 | + running bool |
| 56 | +} |
| 57 | + |
| 58 | +func (app *cliApp) setRunning(v bool) { |
| 59 | + app.rmu.Lock() |
| 60 | + app.running = v |
| 61 | + app.rmu.Unlock() |
| 62 | +} |
| 63 | + |
| 64 | +func (app *cliApp) isRunning() bool { |
| 65 | + app.rmu.Lock() |
| 66 | + v := app.running |
| 67 | + app.rmu.Unlock() |
| 68 | + return v |
| 69 | +} |
| 70 | + |
| 71 | +func (app *cliApp) redraw() { |
| 72 | + // allow only one repaint in queue |
| 73 | + if atomic.CompareAndSwapInt32(&app.redraws, 0, 1) { |
| 74 | + Repaint() |
| 75 | + } |
| 76 | +} |
| 77 | +func (app *cliApp) draw() { |
| 78 | + ClearScreen() |
| 79 | + app.log.draw() |
| 80 | + FullUpdate() |
| 81 | + atomic.StoreInt32(&app.redraws, 0) |
| 82 | +} |
| 83 | + |
| 84 | +func (app *cliApp) println(args ...interface{}) { |
| 85 | + fmt.Fprintln(app.log, args...) |
| 86 | +} |
| 87 | + |
| 88 | +func (app *cliApp) Init() error { |
| 89 | + ClearScreen() |
| 90 | + l := NewLog(Pad(Screen(), 10), DefaultFontHeight) |
| 91 | + app.log = newLogWriter(l, app.redraw) |
| 92 | + |
| 93 | + if app.conf.Certs { |
| 94 | + now := time.Now() |
| 95 | + app.println("reading certs...") |
| 96 | + app.draw() |
| 97 | + if err := InitCerts(); err != nil { |
| 98 | + app.println("error reading certs:", err) |
| 99 | + } else { |
| 100 | + app.println("loaded certs in", time.Since(now)) |
| 101 | + } |
| 102 | + app.draw() |
| 103 | + } |
| 104 | + |
| 105 | + if app.conf.Network { |
| 106 | + var err error |
| 107 | + app.stopNet, err = KeepNetwork() |
| 108 | + if err != nil { |
| 109 | + app.println("cannot connect to the network:", err) |
| 110 | + } else { |
| 111 | + app.println("network connected") |
| 112 | + } |
| 113 | + app.draw() |
| 114 | + } |
| 115 | + |
| 116 | + app.setRunning(true) |
| 117 | + |
| 118 | + ctx, cancel := context.WithCancel(context.Background()) |
| 119 | + app.stop = cancel |
| 120 | + app.wg.Add(1) |
| 121 | + go func() { |
| 122 | + defer app.wg.Done() |
| 123 | + err := app.cli(ctx, app.log) |
| 124 | + if app.stopNet != nil { |
| 125 | + app.stopNet() |
| 126 | + } |
| 127 | + app.err = err |
| 128 | + if err != nil { |
| 129 | + app.println("error:", err) |
| 130 | + } |
| 131 | + app.println("<press any key to exit>") |
| 132 | + app.redraw() |
| 133 | + }() |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func (app *cliApp) stopCli() error { |
| 138 | + if !app.isRunning() { |
| 139 | + return app.err |
| 140 | + } |
| 141 | + app.stop() |
| 142 | + app.wg.Wait() |
| 143 | + app.setRunning(false) |
| 144 | + return app.err |
| 145 | +} |
| 146 | + |
| 147 | +func (app *cliApp) Close() error { |
| 148 | + err := app.stopCli() |
| 149 | + app.log.close() |
| 150 | + if app.stopNet != nil { |
| 151 | + app.stopNet() |
| 152 | + } |
| 153 | + return err |
| 154 | +} |
| 155 | + |
| 156 | +func (app *cliApp) Draw() { |
| 157 | + app.draw() |
| 158 | +} |
| 159 | + |
| 160 | +func (*cliApp) Show() bool { |
| 161 | + return false |
| 162 | +} |
| 163 | + |
| 164 | +func (*cliApp) Hide() bool { |
| 165 | + return false |
| 166 | +} |
| 167 | + |
| 168 | +func (app *cliApp) Key(e KeyEvent) bool { |
| 169 | + if app.isRunning() || (e.Key == KeyPrev && e.State == KeyStateDown) { |
| 170 | + Exit() |
| 171 | + return true |
| 172 | + } |
| 173 | + return false |
| 174 | +} |
| 175 | + |
| 176 | +func (app *cliApp) Pointer(e PointerEvent) bool { |
| 177 | + if app.isRunning() { |
| 178 | + Exit() |
| 179 | + return true |
| 180 | + } |
| 181 | + if e.State == PointerDown { |
| 182 | + app.redraw() |
| 183 | + } |
| 184 | + return true |
| 185 | +} |
| 186 | + |
| 187 | +func (*cliApp) Touch(e TouchEvent) bool { |
| 188 | + return false |
| 189 | +} |
| 190 | + |
| 191 | +func (*cliApp) Orientation(o Orientation) bool { |
| 192 | + return false |
| 193 | +} |
| 194 | + |
| 195 | +type RunConfig struct { |
| 196 | + Certs bool // initialize certificate pool |
| 197 | + Network bool // keep networking enabled while app is running |
| 198 | +} |
| 199 | + |
| 200 | +// RunCLI starts a command-line application that can write to device display. |
| 201 | +// Context will be cancelled when application is closed. |
| 202 | +// Provided callback can use any SDK functions. |
| 203 | +func RunCLI(fnc RunFunc, c *RunConfig) error { |
| 204 | + if c == nil { |
| 205 | + c = &RunConfig{} |
| 206 | + } |
| 207 | + return Run(newCliApp(fnc, *c)) |
| 208 | +} |
0 commit comments