-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
tui.rs
590 lines (533 loc) · 20.1 KB
/
tui.rs
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
mod context;
pub mod input;
mod message;
mod view;
use crate::{
collection::{Collection, CollectionFile, ProfileId, RecipeId},
config::Config,
db::{CollectionDatabase, Database},
http::{HttpEngine, Request, RequestBuilder},
template::{Prompter, Template, TemplateChunk, TemplateContext},
tui::{
context::TuiContext,
input::Action,
message::{Message, MessageSender, RequestConfig},
view::{ModalPriority, PreviewPrompter, RequestState, View},
},
util::Replaceable,
};
use anyhow::{anyhow, Context};
use crossterm::{
event::{DisableMouseCapture, EnableMouseCapture},
terminal::{EnterAlternateScreen, LeaveAlternateScreen},
};
use futures::Future;
use notify::{RecursiveMode, Watcher};
use ratatui::{prelude::CrosstermBackend, Terminal};
use signal_hook::{
consts::{SIGHUP, SIGINT, SIGQUIT, SIGTERM},
iterator::Signals,
};
use std::{
io::{self, Stdout},
ops::Deref,
path::PathBuf,
sync::{Arc, OnceLock},
time::{Duration, Instant},
};
use tokio::sync::mpsc::{self, UnboundedReceiver};
use tracing::{debug, error};
/// Main controller struct for the TUI. The app uses a React-like architecture
/// for the view, with a wrapping controller (this struct). The main loop goes
/// through the following phases on each iteration:
///
/// - Input phase: Check for input from the user
/// - Message phase: Process any async messages from input or external sources
/// (HTTP, file system, etc.)
/// - Draw phase: Draw the entire UI
/// - Signal phase: Check for process signals that should trigger an exit
#[derive(Debug)]
pub struct Tui {
terminal: Term,
messages_rx: UnboundedReceiver<Message>,
messages_tx: MessageSender,
http_engine: HttpEngine,
/// Replaceable allows us to enforce that the view is dropped before being
/// recreated. The view persists its state on drop, so that has to happen
/// before the new one is created.
view: Replaceable<View>,
collection_file: CollectionFile,
/// We only ever need to run DB ops related to our collection, so we can
/// use a collection-restricted DB handle
database: CollectionDatabase,
should_run: bool,
}
type Term = Terminal<CrosstermBackend<Stdout>>;
impl Tui {
/// Rough maximum time for each iteration of the main loop
const TICK_TIME: Duration = Duration::from_millis(250);
/// Start the TUI. Any errors that occur during startup will be panics,
/// because they prevent TUI execution.
pub async fn start(collection_path: PathBuf) -> anyhow::Result<()> {
initialize_panic_handler();
// ===== Initialize global state =====
// This stuff only needs to be set up *once per session*
let config = Config::load()?;
// Create a message queue for handling async tasks
let (messages_tx, messages_rx) = mpsc::unbounded_channel();
let messages_tx = MessageSender::new(messages_tx);
// Load a database for this particular collection
let database = Database::load()?.into_collection(&collection_path)?;
let http_engine = HttpEngine::new(&config, database.clone());
// Initialize global view context
TuiContext::init(config, messages_tx.clone(), database.clone());
// ===== Initialize collection & view =====
// If the collection fails to load, create an empty one just so we can
// move along. We'll watch the file and hopefully the user can fix it
let collection_file = CollectionFile::load(collection_path.clone())
.await
.unwrap_or_else(|error| {
messages_tx.send(Message::Error { error });
CollectionFile::with_path(collection_path)
});
let view = View::new(&collection_file.collection);
// The code to revert the terminal takeover is in `Tui::drop`, so we
// shouldn't take over the terminal until right before creating the
// `Tui`.
let terminal = initialize_terminal()?;
let app = Tui {
terminal,
messages_rx,
messages_tx,
http_engine,
collection_file,
should_run: true,
view: Replaceable::new(view),
database,
};
app.run()
}
/// Run the main TUI update loop. Any error returned from this is fatal. See
/// the struct definition for a description of the different phases of the
/// run loop.
fn run(mut self) -> anyhow::Result<()> {
// Listen for signals to stop the program
let mut quit_signals = Signals::new([SIGHUP, SIGINT, SIGTERM, SIGQUIT])
.context("Error creating signal handler")?;
// Hang onto this because it stops running when dropped
let _watcher = self.watch_collection()?;
let mut last_tick = Instant::now();
while self.should_run {
// ===== Input Phase =====
let timeout = Self::TICK_TIME
.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));
// This is where the tick rate is enforced
if crossterm::event::poll(timeout)? {
// Forward input to the view. Include the raw event for text
// editors and such
let event = crossterm::event::read()?;
let action = TuiContext::get().input_engine.action(&event);
if let Some(Action::ForceQuit) = action {
// Short-circuit the view/message cycle, to make sure this
// doesn't get ate
self.quit();
} else {
self.view.handle_input(event, action);
}
}
if last_tick.elapsed() >= Self::TICK_TIME {
last_tick = Instant::now();
}
// ===== Message Phase =====
while let Ok(message) = self.messages_rx.try_recv() {
// If an error occurs, store it so we can show the user
if let Err(error) = self.handle_message(message) {
self.view.open_modal(error, ModalPriority::High);
}
}
// ===== Draw Phase =====
self.terminal.draw(|f| self.view.draw(f))?;
// ===== Signal Phase =====
if quit_signals.pending().next().is_some() {
self.quit();
}
}
Ok(())
}
/// GOODBYE
fn quit(&mut self) {
self.should_run = false;
}
/// Handle an incoming message. Any error here will be displayed as a modal
fn handle_message(&mut self, message: Message) -> anyhow::Result<()> {
match message {
Message::CollectionStartReload => {
let messages_tx = self.messages_tx.clone();
let future = self.collection_file.reload();
self.spawn(async move {
let collection = future.await?;
messages_tx.send(Message::CollectionEndReload(collection));
Ok(())
});
}
Message::CollectionEndReload(collection) => {
self.reload_collection(collection);
}
Message::CollectionEdit => {
let path = self.collection_file.path();
open::that_detached(path).context("Error opening {path:?}")?;
}
Message::CopyRequestUrl(request_config) => {
self.copy_request_url(request_config)?;
}
Message::CopyRequestBody(request_config) => {
self.copy_request_body(request_config)?;
}
Message::CopyRequestCurl(request_config) => {
self.copy_request_curl(request_config)?;
}
Message::CopyText(text) => self.view.copy_text(text),
Message::Error { error } => {
self.view.open_modal(error, ModalPriority::High)
}
// Manage HTTP life cycle
Message::HttpBeginRequest(request_config) => {
self.send_request(request_config)?
}
Message::HttpBuildError {
profile_id,
recipe_id,
error,
} => {
self.view.set_request_state(
profile_id,
recipe_id,
RequestState::BuildError { error },
);
}
Message::HttpLoading {
profile_id,
recipe_id,
request,
} => {
self.view.set_request_state(
profile_id,
recipe_id,
RequestState::loading(request),
);
}
Message::HttpComplete(result) => {
let (profile_id, recipe_id, state) = match result {
Ok(record) => (
record.request.profile_id.clone(),
record.request.recipe_id.clone(),
RequestState::response(record),
),
Err(error) => (
error.request.profile_id.clone(),
error.request.recipe_id.clone(),
RequestState::RequestError { error },
),
};
self.view.set_request_state(profile_id, recipe_id, state);
}
Message::RequestLoad {
profile_id,
recipe_id,
} => {
self.load_request(profile_id.as_ref(), &recipe_id)?;
}
Message::PromptStart(prompt) => {
self.view.open_modal(prompt, ModalPriority::Low);
}
Message::Quit => self.quit(),
Message::TemplatePreview {
template,
profile_id,
destination,
} => {
self.render_template_preview(
template,
profile_id.as_ref(),
destination,
)?;
}
}
Ok(())
}
/// Spawn a watcher to automatically reload the collection when the file
/// changes. Return the watcher because it stops when dropped.
fn watch_collection(&self) -> anyhow::Result<impl Watcher> {
// Spawn a watcher for the collection file
let messages_tx = self.messages_tx.clone();
let mut watcher =
notify::recommended_watcher(move |result: notify::Result<_>| {
match result {
Ok(_) => messages_tx.send(Message::CollectionStartReload),
Err(err) => {
error!(error = %err, "Error watching collection file");
}
}
})?;
watcher
.watch(self.collection_file.path(), RecursiveMode::NonRecursive)?;
Ok(watcher)
}
/// Reload state with a new collection
fn reload_collection(&mut self, collection: Collection) {
self.collection_file.collection = collection;
// Rebuild the whole view, because tons of things can change. Drop the
// old one *first* to make sure UI state is saved before being restored
self.view.replace(|old| {
drop(old);
View::new(&self.collection_file.collection)
});
self.view.notify(format!(
"Reloaded collection from {}",
self.collection_file.path().to_string_lossy()
));
}
/// Render URL for a request, then copy it to the clipboard
fn copy_request_url(
&self,
request_config: RequestConfig,
) -> anyhow::Result<()> {
let builder = self.get_request_builder(request_config)?;
let messages_tx = self.messages_tx.clone();
// Spawn a task to do the render+copy
self.spawn(async move {
let url = builder.build_url().await?;
messages_tx.send(Message::CopyText(url.to_string()));
Ok(())
});
Ok(())
}
/// Render body for a request, then copy it to the clipboard
fn copy_request_body(
&self,
request_config: RequestConfig,
) -> anyhow::Result<()> {
let builder = self.get_request_builder(request_config)?;
let messages_tx = self.messages_tx.clone();
// Spawn a task to do the render+copy
self.spawn(async move {
let body = builder
.build_body()
.await?
.ok_or(anyhow!("Request has no body"))?;
let body = String::from_utf8(body.into())
.context("Cannot copy request body")?;
messages_tx.send(Message::CopyText(body));
Ok(())
});
Ok(())
}
/// Render a request, then copy the equivalent curl command to the clipboard
fn copy_request_curl(
&self,
request_config: RequestConfig,
) -> anyhow::Result<()> {
let builder = self.get_request_builder(request_config)?;
let messages_tx = self.messages_tx.clone();
// Spawn a task to do the render+copy
self.spawn(async move {
let request = builder.build().await?;
let command = request.to_curl()?;
messages_tx.send(Message::CopyText(command));
Ok(())
});
Ok(())
}
/// Launch an HTTP request in a separate task
fn send_request(
&mut self,
request_config: RequestConfig,
) -> anyhow::Result<()> {
// Launch the request in a separate task so it doesn't block.
// These clones are all cheap.
let http_engine = self.http_engine.clone();
let builder = self.get_request_builder(request_config.clone())?;
let messages_tx = self.messages_tx.clone();
let RequestConfig {
profile_id,
recipe_id,
..
} = request_config;
// Mark request state as building
let request_id = builder.id();
self.view.set_request_state(
profile_id.clone(),
recipe_id.clone(),
RequestState::building(request_id),
);
// We can't use self.spawn here because HTTP errors are handled
// differently from all other error types
tokio::spawn(async move {
// Build the request
let request: Arc<Request> = builder
.build()
.await
.map_err(|error| {
// Report the error, but don't actually return anything
messages_tx.send(Message::HttpBuildError {
profile_id: profile_id.clone(),
recipe_id: recipe_id.clone(),
error,
});
})?
.into();
// Report liftoff
messages_tx.send(Message::HttpLoading {
profile_id,
recipe_id,
request: Arc::clone(&request),
});
// Send the request and report the result to the main thread
let result = http_engine.send(request).await;
messages_tx.send(Message::HttpComplete(result));
// By returning an empty result, we can use `?` to break out early.
// `return` and `break` don't work in an async block :/
Ok::<(), ()>(())
});
Ok(())
}
/// Load the most recent request+response for a particular recipe from the
/// database, and store it in state.
fn load_request(
&mut self,
profile_id: Option<&ProfileId>,
recipe_id: &RecipeId,
) -> anyhow::Result<()> {
if let Some(record) =
self.database.get_last_request(profile_id, recipe_id)?
{
self.view.set_request_state(
profile_id.cloned(),
record.request.recipe_id.clone(),
RequestState::response(record),
);
}
Ok(())
}
/// Helper to create a [RequestBuilder] based on request parameters
fn get_request_builder(
&self,
RequestConfig {
profile_id,
recipe_id,
options,
}: RequestConfig,
) -> anyhow::Result<RequestBuilder> {
let recipe = self
.collection_file
.collection
.recipes
.get(&recipe_id)
.ok_or_else(|| anyhow!("No recipe with ID `{recipe_id}`"))?
.clone();
let template_context = self
.template_context(profile_id.as_ref(), self.messages_tx.clone())?;
Ok(RequestBuilder::new(recipe, options, template_context))
}
/// Spawn a task to render a template, storing the result in a pre-defined
/// lock. As this is a preview, the user will *not* be prompted for any
/// input. A placeholder value will be used for any prompts.
fn render_template_preview(
&self,
template: Template,
profile_id: Option<&ProfileId>,
destination: Arc<OnceLock<Vec<TemplateChunk>>>,
) -> anyhow::Result<()> {
let context = self.template_context(profile_id, PreviewPrompter)?;
self.spawn(async move {
// Render chunks, then write them to the output destination
let chunks = template.render_chunks(&context).await;
// If this fails, it's a logic error somewhere. Only one task should
// exist per lock
destination.set(chunks).map_err(|_| {
anyhow!("Multiple writes to template preview lock")
})
});
Ok(())
}
/// Helper for spawning a fallible task. Any error in the resolved future
/// will be shown to the user in a modal.
fn spawn(
&self,
future: impl Future<Output = anyhow::Result<()>> + Send + 'static,
) {
let messages_tx = self.messages_tx.clone();
tokio::spawn(async move {
if let Err(err) = future.await {
messages_tx.send(Message::Error { error: err })
}
});
}
/// Expose app state to the templater. Most of the data has to be cloned out
/// to be passed across async boundaries. This is annoying but in reality
/// it should be small data.
fn template_context(
&self,
profile_id: Option<&ProfileId>,
prompter: impl 'static + Prompter,
) -> anyhow::Result<TemplateContext> {
let collection = &self.collection_file.collection;
// Find profile by ID
let profile = profile_id
.map(|profile_id| {
Ok::<_, anyhow::Error>(
collection
.profiles
.get(profile_id)
.ok_or_else(|| {
anyhow!("No profile with ID `{profile_id}`")
})?
.clone(),
)
})
.transpose()?;
Ok(TemplateContext {
profile,
chains: collection.chains.clone(),
database: self.database.clone(),
overrides: Default::default(),
prompter: Box::new(prompter),
})
}
}
/// Restore terminal on app exit
impl Drop for Tui {
fn drop(&mut self) {
if let Err(err) = restore_terminal() {
error!(error = err.deref(), "Error restoring terminal, sorry!");
}
}
}
/// Restore terminal state during a panic
fn initialize_panic_handler() {
let original_hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |panic_info| {
restore_terminal().unwrap();
original_hook(panic_info);
}));
}
/// Set up terminal for TUI
fn initialize_terminal() -> anyhow::Result<Term> {
crossterm::terminal::enable_raw_mode()?;
let mut stdout = io::stdout();
crossterm::execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
let backend = CrosstermBackend::new(stdout);
Ok(Terminal::new(backend)?)
}
/// Return terminal to initial state
fn restore_terminal() -> anyhow::Result<()> {
debug!("Restoring terminal");
crossterm::terminal::disable_raw_mode()?;
crossterm::execute!(
io::stdout(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
Ok(())
}