Repository navigation
Expand file tree
/
Copy pathprogram.go
More file actions
206 lines (179 loc) · 8.93 KB
/
Copy pathprogram.go
File metadata and controls
206 lines (179 loc) · 8.93 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
package limoni
import (
"context"
"os/exec"
"github.com/thebanri/limoni/core/driver"
"github.com/thebanri/limoni/core/engine"
)
// Limoni ships two application models. Both drive the same renderer, the same
// widgets, and the same zero-allocation buffer pipeline; they differ only in how
// application state is owned.
//
// Immediate mode (Run / Start) keeps state in your own closure and redraws the
// whole frame on every event. It suits render loops that are naturally
// continuous: dashboards, 3D viewers, games, animations.
//
// limoni.Run(func(f *limoni.Frame, ev *limoni.Event) bool {
// f.Draw(limoni.Label("hello"))
// return ev == nil || ev.Key.Type != limoni.KeyEsc
// })
//
// Declarative mode (NewProgram) follows The Elm Architecture: a Model owns the
// state, Update folds messages into it, and View renders. It suits
// forms, wizards, CRUD tools, and anything with asynchronous commands, because
// the runtime handles message scheduling, command cancellation, deterministic
// ordering, and panic recovery for you.
//
// type model struct{ count int }
//
// func (m *model) Init() []limoni.Cmd { return nil }
// func (m *model) Update(msg limoni.Msg) limoni.UpdateResult {
// if key, ok := msg.(limoni.KeyPressMsg); ok && key.Key.Ch == 'q' {
// return limoni.UpdateResult{Quit: true}
// }
// return limoni.UpdateResult{Redraw: true}
// }
// func (m *model) View(f *limoni.Frame) { /* ... */ }
//
// prog := limoni.NewProgram(&model{}, limoni.WithAltScreen())
// err := prog.Run(ctx)
//
// Declarative mode is context-aware today: Program.Run and Program.RunTerminal
// both take a context.Context and shut down cleanly when it is cancelled.
//
// The full runtime lives in core/engine; the aliases below exist so the common
// path never needs a second import.
// Re-exported application runtime types.
type (
// Model is the Init/Update/View contract implemented by declarative applications.
Model = engine.Model
// Msg is an application message delivered to Model.Update.
Msg = engine.Msg
// Cmd is a cancellable side effect that eventually produces a Msg.
Cmd = engine.Cmd
// UpdateResult describes the work requested after handling a message.
UpdateResult = engine.UpdateResult
// Program is the declarative application runtime.
Program = engine.Program
// ProgramOption configures a Program created with NewProgram.
ProgramOption = engine.Option
)
// Re-exported runtime messages delivered to Model.Update.
type (
KeyPressMsg = engine.KeyPressMsg
KeyReleaseMsg = engine.KeyReleaseMsg
MousePressMsg = engine.MousePressMsg
MouseReleaseMsg = engine.MouseReleaseMsg
MouseMotionMsg = engine.MouseMotionMsg
MouseWheelMsg = engine.MouseWheelMsg
PasteMsg = engine.PasteMsg
ResizeMsg = engine.ResizeMsg
FocusMsg = engine.FocusMsg
BlurMsg = engine.BlurMsg
TimeMsg = engine.TimeMsg
)
// Observer sees every message and frame of a Program; see engine.Observer.
// session.Recorder implements it.
type Observer = engine.Observer
// NowCmd delivers the current time as a TimeMsg. A model that needs the clock
// should ask for it this way rather than calling time.Now inside Update, so
// that a session recording captures the time it saw and a replay sees the
// same.
func NowCmd() Cmd { return engine.NowCmd() }
// NotifyCmd shows a desktop notification in terminals that support one
// (kitty, iTerm2, WezTerm, Ghostty, foot) and does nothing elsewhere.
func NotifyCmd(title, body string) Cmd { return engine.NotifyCmd(title, body) }
// ExecCmd hands the terminal to cmd — an editor, a pager, a shell — and takes
// it back when cmd exits; done turns cmd's error into a message for Update
// (nil for none). A nil Stdin, Stdout or Stderr is the process's own. Where
// there is no terminal to hand over (a remote session, the browser) done
// receives ErrReleaseUnsupported and cmd is not started.
//
// return limoni.UpdateResult{Commands: []limoni.Cmd{
// limoni.ExecCmd(exec.Command(editor, path), func(err error) limoni.Msg {
// return editedMsg{err}
// }),
// }}
func ExecCmd(cmd *exec.Cmd, done func(error) Msg) Cmd { return engine.ExecCmd(cmd, done) }
// SuspendCmd stops the application and hands the terminal back to the shell,
// as Ctrl+Z does elsewhere; `fg` resumes it with the screen repainted.
func SuspendCmd() Cmd { return engine.SuspendCmd() }
// ErrReleaseUnsupported is what ExecCmd reports where the terminal cannot be
// handed to another program.
var ErrReleaseUnsupported = driver.ErrReleaseUnsupported
// WithProgramObserver attaches an observer, such as a session.Recorder, to a
// Program.
//
// Recording is not wired into this package any further than this: a binary
// only contains the session recorder if the application imports
// github.com/thebanri/limoni/session itself, which makes recording a decision
// visible in the application's own imports rather than a flag.
func WithProgramObserver(observer Observer) ProgramOption { return engine.WithObserver(observer) }
// NewProgram creates a declarative (Elm architecture) application runtime for
// model. Run it with Program.Run for full control over the terminal, or
// Program.RunTerminal for the batteries-included path.
func NewProgram(model Model, opts ...ProgramOption) *Program {
options := make([]ProgramOption, 0, len(opts)+1)
options = append(options, engine.WithModel(model))
options = append(options, opts...)
return engine.New(options...)
}
// RunProgram is the batteries-included entry point for declarative
// applications. It initialises the terminal, runs model until it quits or ctx is
// cancelled, and restores the terminal state on the way out.
//
// if err := limoni.RunProgram(context.Background(), &myModel{}); err != nil {
// log.Fatal(err)
// }
//
// Use NewProgram directly when you need to own the terminal lifecycle, drive a
// custom backend, or render into an SSH session.
func RunProgram(ctx context.Context, model Model, opts ...ProgramOption) error {
term, err := New()
if err != nil {
return err
}
defer term.Close()
return NewProgram(model, opts...).RunTerminal(ctx, term, term.Driver())
}
// MessageFromEvent converts a driver event into the corresponding runtime Msg.
// Useful when feeding a Program from a custom backend or from a test harness.
func MessageFromEvent(event Event) Msg { return engine.MessageFromDriver(event) }
// WithAltScreen renders the Program on the terminal's alternate screen buffer.
func WithAltScreen() ProgramOption { return engine.WithAltScreen() }
// WithMessageQueue sets the Program's inbound message queue capacity.
func WithMessageQueue(capacity int) ProgramOption { return engine.WithMessageQueue(capacity) }
// WithCommandQueue sets the Program's outbound command queue capacity.
func WithCommandQueue(capacity int) ProgramOption { return engine.WithCommandQueue(capacity) }
// WithPanicHandler installs a recovery handler invoked when a Model method or a
// Cmd panics, instead of tearing down the process.
func WithPanicHandler(handler func(any)) ProgramOption { return engine.WithPanicHandler(handler) }
// WithProgramBackdrop draws an animated scene behind the Program's View,
// such as one from package backdrop. It mirrors WithBackdrop for Run.
func WithProgramBackdrop(bg Backdrop) ProgramOption { return engine.WithBackdrop(bg) }
// WithProgramOverlayPictures shows pictures at full resolution in terminals
// with no picture protocol of their own; it mirrors WithOverlayPictures for
// Run.
func WithProgramOverlayPictures() ProgramOption { return engine.WithOverlayPictures() }
// WithoutProgramMouse leaves the mouse to the terminal: no clicks, wheel or
// pointer movement reach the model, and text can be selected as in a shell,
// without holding Shift. Most terminals turn the wheel into arrow keys on the
// alternate screen.
func WithoutProgramMouse() ProgramOption { return engine.WithoutMouse() }
// WithProgramMouseMode sets how much of the mouse is reported: every pointer
// move (MouseMotion, the default), moves with a button held (MouseDrag), or
// clicks and the wheel alone (MouseClicks).
func WithProgramMouseMode(mode MouseMode) ProgramOption { return engine.WithMouseMode(mode) }
// WithProgramFPS sets a continuous redraw rate for a Program, for a View that
// changes without messages. Without it a Program draws only when something
// happens (input, a redraw request, an Update) and uses no CPU while idle.
//
// It mirrors WithFPS, which configures immediate-mode Run instead. The two
// application models take different option types, so the names cannot be shared.
func WithProgramFPS(fps int) ProgramOption { return engine.WithFPS(fps) }
// WithProgramCatchCtrlC configures whether a Program receives Ctrl+C as a normal
// key message instead of terminating. It mirrors WithCatchCtrlC for Run.
func WithProgramCatchCtrlC(catch bool) ProgramOption { return engine.WithCatchCtrlC(catch) }
// WithoutProgramQuitKeys disables a Program's automatic termination on Ctrl+C.
// It mirrors WithoutDefaultQuitKeys for Run.
func WithoutProgramQuitKeys() ProgramOption { return engine.WithoutDefaultQuitKeys() }