|
1 |
+ |
//! {{title}}, in a terminal.
|
|
2 |
+ |
//!
|
|
3 |
+ |
//! The third host over the same router, and the one that proves the claim the
|
|
4 |
+ |
//! other two cannot. `{{name}}-server` and `{{name}}-desktop` are both webview
|
|
5 |
+ |
//! hosts: one serves the markup over HTTP and the other over a custom protocol,
|
|
6 |
+ |
//! and a webview can express anything, so neither of them ever pushes back on a
|
|
7 |
+ |
//! description. This one draws cells. What it cannot honour is the useful part.
|
|
8 |
+ |
//!
|
|
9 |
+ |
//! Nothing here describes a screen. The router is the same table, the state is
|
|
10 |
+ |
//! the same state, and the difference is the renderer and the loop around it.
|
|
11 |
+ |
//!
|
|
12 |
+ |
//! # The loop
|
|
13 |
+ |
//!
|
|
14 |
+ |
//! `quasi_tui::Runtime` holds the screen, what the user has typed into it, and
|
|
15 |
+ |
//! how they got here; it turns a key into a `Step` and never calls a handler.
|
|
16 |
+ |
//! The calling is here, which is what keeps the runtime free of this app's
|
|
17 |
+ |
//! state type:
|
|
18 |
+ |
//!
|
|
19 |
+ |
//! ```text
|
|
20 |
+ |
//! key ──► runtime.key ──► Step::Call(request) ──► router.handle ──► response
|
|
21 |
+ |
//! │
|
|
22 |
+ |
//! runtime.apply ◄─────────┘
|
|
23 |
+ |
//! ```
|
|
24 |
+ |
//!
|
|
25 |
+ |
//! # Authentication
|
|
26 |
+ |
//!
|
|
27 |
+ |
//! None. See the module docs of `{{name}}_core`. For this host the answer is
|
|
28 |
+ |
//! whatever the machine's user already is, the same as the desktop binary: a
|
|
29 |
+ |
//! terminal has no session and no cookie to put one in.
|
|
30 |
+ |
|
|
31 |
+ |
use std::io;
|
|
32 |
+ |
use std::path::PathBuf;
|
|
33 |
+ |
use std::sync::Arc;
|
|
34 |
+ |
|
|
35 |
+ |
use quasi_router::{Request, RouteError};
|
|
36 |
+ |
use quasi_tui::{Key, Runtime, Step, Tui};
|
|
37 |
+ |
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
|
|
38 |
+ |
use ratatui::layout::Rect;
|
|
39 |
+ |
use ratatui::style::Style;
|
|
40 |
+ |
|
|
41 |
+ |
use {{snake}}_core::{AppState, router};
|
|
42 |
+ |
|
|
43 |
+ |
/// Where the database lives, unless `{{SCREAM}}_DB` says otherwise.
|
|
44 |
+ |
const DEFAULT_DB: &str = "{{name}}.db";
|
|
45 |
+ |
|
|
46 |
+ |
/// The screen the app opens on.
|
|
47 |
+ |
const HOME: &str = "/";
|
|
48 |
+ |
|
|
49 |
+ |
/// Which theme to draw in, unless `{{SCREAM}}_THEME` says otherwise.
|
|
50 |
+ |
const DEFAULT_THEME: &str = "goingson";
|
|
51 |
+ |
|
|
52 |
+ |
fn main() -> io::Result<()> {
|
|
53 |
+ |
let db = std::env::var("{{SCREAM}}_DB").unwrap_or_else(|_| DEFAULT_DB.to_owned());
|
|
54 |
+ |
let state = Arc::new(
|
|
55 |
+ |
AppState::open(&PathBuf::from(&db)).unwrap_or_else(|error| {
|
|
56 |
+ |
// Before there is a screen to put a message on, so this goes to
|
|
57 |
+ |
// whoever launched the binary.
|
|
58 |
+ |
eprintln!("{{name}}: {error}");
|
|
59 |
+ |
std::process::exit(1);
|
|
60 |
+ |
}),
|
|
61 |
+ |
);
|
|
62 |
+ |
|
|
63 |
+ |
let router = router();
|
|
64 |
+ |
let tui = renderer();
|
|
65 |
+ |
|
|
66 |
+ |
// The first screen, before the terminal is taken over, so that a router
|
|
67 |
+ |
// that cannot answer its own home route says so on an ordinary terminal
|
|
68 |
+ |
// rather than inside an alternate screen that is about to be torn down.
|
|
69 |
+ |
let screen = match router.handle(&state, Request::get(HOME)) {
|
|
70 |
+ |
Ok(response) => match response.outcome {
|
|
71 |
+ |
quasi_router::Outcome::Screen(screen) => screen,
|
|
72 |
+ |
other => {
|
|
73 |
+ |
eprintln!("{{name}}: the home route answered {other:?} rather than a screen");
|
|
74 |
+ |
std::process::exit(1);
|
|
75 |
+ |
}
|
|
76 |
+ |
},
|
|
77 |
+ |
Err(error) => {
|
|
78 |
+ |
eprintln!("{{name}}: {error}");
|
|
79 |
+ |
std::process::exit(1);
|
|
80 |
+ |
}
|
|
81 |
+ |
};
|
|
82 |
+ |
|
|
83 |
+ |
let mut runtime = Runtime::new(screen);
|
|
84 |
+ |
let mut terminal = ratatui::init();
|
|
85 |
+ |
let outcome = run(&mut terminal, &tui, &mut runtime, &router, &state);
|
|
86 |
+ |
ratatui::restore();
|
|
87 |
+ |
outcome
|
|
88 |
+ |
}
|
|
89 |
+ |
|
|
90 |
+ |
/// Draw, wait for a key, act on it, repeat.
|
|
91 |
+ |
fn run(
|
|
92 |
+ |
terminal: &mut ratatui::DefaultTerminal,
|
|
93 |
+ |
tui: &Tui,
|
|
94 |
+ |
runtime: &mut Runtime,
|
|
95 |
+ |
router: &quasi_router::Router<AppState>,
|
|
96 |
+ |
state: &AppState,
|
|
97 |
+ |
) -> io::Result<()> {
|
|
98 |
+ |
// What a control asked before doing itself, held while the answer is typed.
|
|
99 |
+ |
let mut question: Option<String> = None;
|
|
100 |
+ |
|
|
101 |
+ |
loop {
|
|
102 |
+ |
terminal.draw(|frame| {
|
|
103 |
+ |
let area = frame.area();
|
|
104 |
+ |
let (screen, prompt) = split_bottom(area, question.is_some());
|
|
105 |
+ |
runtime.draw(tui, screen, frame.buffer_mut());
|
|
106 |
+ |
if let Some(text) = &question {
|
|
107 |
+ |
// `Act::confirm` is a question asked after the control is
|
|
108 |
+ |
// pressed, which is why the drawing declined it: there is
|
|
109 |
+ |
// nothing to draw until it has been. A terminal asks it on the
|
|
110 |
+ |
// last row, which is where a terminal asks everything.
|
|
111 |
+ |
frame.buffer_mut().set_stringn(
|
|
112 |
+ |
prompt.x,
|
|
113 |
+ |
prompt.y,
|
|
114 |
+ |
format!("{text} [y/n]"),
|
|
115 |
+ |
prompt.width as usize,
|
|
116 |
+ |
Style::default().add_modifier(ratatui::style::Modifier::REVERSED),
|
|
117 |
+ |
);
|
|
118 |
+ |
}
|
|
119 |
+ |
})?;
|
|
120 |
+ |
|
|
121 |
+ |
let Event::Key(pressed) = event::read()? else {
|
|
122 |
+ |
continue;
|
|
123 |
+ |
};
|
|
124 |
+ |
if pressed.kind != KeyEventKind::Press {
|
|
125 |
+ |
continue;
|
|
126 |
+ |
}
|
|
127 |
+ |
|
|
128 |
+ |
// Quitting is the app's and not the runtime's: a key that closes the
|
|
129 |
+ |
// binary is a fact about the binary rather than about any screen. It is
|
|
130 |
+ |
// asked first, and only when the caret is not in a box, or `q` would be
|
|
131 |
+ |
// a letter nobody could type.
|
|
132 |
+ |
if !runtime.editing()
|
|
133 |
+ |
&& !runtime.asking()
|
|
134 |
+ |
&& (matches!(pressed.code, KeyCode::Char('q'))
|
|
135 |
+ |
|| (pressed.modifiers.contains(KeyModifiers::CONTROL)
|
|
136 |
+ |
&& matches!(pressed.code, KeyCode::Char('c'))))
|
|
137 |
+ |
{
|
|
138 |
+ |
return Ok(());
|
|
139 |
+ |
}
|
|
140 |
+ |
|
|
141 |
+ |
let Some(key) = translate(pressed.code, pressed.modifiers) else {
|
|
142 |
+ |
continue;
|
|
143 |
+ |
};
|
|
144 |
+ |
|
|
145 |
+ |
let mut step = runtime.key(key);
|
|
146 |
+ |
question = None;
|
|
147 |
+ |
loop {
|
|
148 |
+ |
match step {
|
|
149 |
+ |
Step::Idle => break,
|
|
150 |
+ |
Step::Ask(prompt) => {
|
|
151 |
+ |
question = Some(prompt);
|
|
152 |
+ |
break;
|
|
153 |
+ |
}
|
|
154 |
+ |
// Nothing comes back from an external address, and a terminal
|
|
155 |
+ |
// has no browser to hand it to. Saying so beats opening
|
|
156 |
+ |
// something the user did not ask for.
|
|
157 |
+ |
Step::Open(address) => {
|
|
158 |
+ |
runtime.say(format!("this goes outside the app: {address}"));
|
|
159 |
+ |
break;
|
|
160 |
+ |
}
|
|
161 |
+ |
Step::Call(request) => {
|
|
162 |
+ |
match router.handle(state, request.clone()) {
|
|
163 |
+ |
Ok(response) => match runtime.apply(&request, response) {
|
|
164 |
+ |
// A response that says to go somewhere else is a
|
|
165 |
+ |
// second request, performed the same way.
|
|
166 |
+ |
Some(next) => step = Step::Call(next),
|
|
167 |
+ |
None => break,
|
|
168 |
+ |
},
|
|
169 |
+ |
Err(error) => {
|
|
170 |
+ |
runtime.say(said(&error));
|
|
171 |
+ |
break;
|
|
172 |
+ |
}
|
|
173 |
+ |
}
|
|
174 |
+ |
}
|
|
175 |
+ |
}
|
|
176 |
+ |
}
|
|
177 |
+ |
}
|
|
178 |
+ |
}
|
|
179 |
+ |
|
|
180 |
+ |
/// The renderer, in this terminal's colours.
|
|
181 |
+ |
///
|
|
182 |
+ |
/// Constructed here rather than in `{{name}}-core`, the same way the other two
|
|
183 |
+ |
/// hosts construct theirs. The core names no renderer, which is the whole
|
|
184 |
+ |
/// reason a third one could be added without touching a screen.
|
|
185 |
+ |
fn renderer() -> Tui {
|
|
186 |
+ |
let fidelity = makeover_tui::Fidelity::detect();
|
|
187 |
+ |
let wanted = std::env::var("{{SCREAM}}_THEME").unwrap_or_else(|_| DEFAULT_THEME.to_owned());
|
|
188 |
+ |
|
|
189 |
+ |
let theme = makeover::bundled_themes_dir()
|
|
190 |
+ |
.ok()
|
|
191 |
+ |
.and_then(|dir| makeover::load_theme(&[(dir, false)], &wanted).ok())
|
|
192 |
+ |
.and_then(|colours| makeover_tui::Theme::from_theme(&colours).ok());
|
|
193 |
+ |
|
|
194 |
+ |
match theme {
|
|
195 |
+ |
Some(theme) => Tui::new(theme, fidelity),
|
|
196 |
+ |
None => {
|
|
197 |
+ |
eprintln!("{{name}}: no theme called `{wanted}`");
|
|
198 |
+ |
std::process::exit(1);
|
|
199 |
+ |
}
|
|
200 |
+ |
}
|
|
201 |
+ |
}
|
|
202 |
+ |
|
|
203 |
+ |
/// A route's failure as a sentence for the screen.
|
|
204 |
+ |
fn said(error: &RouteError) -> String {
|
|
205 |
+ |
error.to_string()
|
|
206 |
+ |
}
|
|
207 |
+ |
|
|
208 |
+ |
/// The screen, and the row a question is asked on.
|
|
209 |
+ |
fn split_bottom(area: Rect, asking: bool) -> (Rect, Rect) {
|
|
210 |
+ |
if !asking || area.height == 0 {
|
|
211 |
+ |
return (area, Rect::new(area.x, area.y, area.width, 0));
|
|
212 |
+ |
}
|
|
213 |
+ |
(
|
|
214 |
+ |
Rect {
|
|
215 |
+ |
height: area.height - 1,
|
|
216 |
+ |
..area
|
|
217 |
+ |
},
|
|
218 |
+ |
Rect {
|
|
219 |
+ |
y: area.y + area.height - 1,
|
|
220 |
+ |
height: 1,
|
|
221 |
+ |
..area
|
|
222 |
+ |
},
|
|
223 |
+ |
)
|
|
224 |
+ |
}
|
|
225 |
+ |
|
|
226 |
+ |
/// This terminal's keys, as the ones the runtime names.
|
|
227 |
+ |
///
|
|
228 |
+ |
/// The whole of what a host owes the runtime, and the reason `quasi_tui::Key`
|
|
229 |
+ |
/// is its own type rather than crossterm's: the bindings stay testable without
|
|
230 |
+ |
/// a terminal, and a host on another backend writes this function instead of
|
|
231 |
+ |
/// taking a dependency it is not using.
|
|
232 |
+ |
fn translate(code: KeyCode, modifiers: KeyModifiers) -> Option<Key> {
|
|
233 |
+ |
Some(match code {
|
|
234 |
+ |
KeyCode::Char(ch) => Key::Char(ch),
|
|
235 |
+ |
KeyCode::Enter => Key::Enter,
|
|
236 |
+ |
// Shift+Tab arrives as BackTab on most terminals and as Tab carrying
|
|
237 |
+ |
// the modifier on others, so both reach the same binding.
|
|
238 |
+ |
KeyCode::Tab if modifiers.contains(KeyModifiers::SHIFT) => Key::BackTab,
|
|
239 |
+ |
KeyCode::Tab => Key::Tab,
|
|
240 |
+ |
KeyCode::BackTab => Key::BackTab,
|
|
241 |
+ |
KeyCode::Backspace => Key::Backspace,
|
|
242 |
+ |
KeyCode::Esc => Key::Escape,
|
|
243 |
+ |
KeyCode::Up => Key::Up,
|
|
244 |
+ |
KeyCode::Down => Key::Down,
|
|
245 |
+ |
KeyCode::PageUp => Key::PageUp,
|
|
246 |
+ |
KeyCode::PageDown => Key::PageDown,
|
|
247 |
+ |
_ => return None,
|
|
248 |
+ |
})
|
|
249 |
+ |
}
|