プロトコル経由のshutdow経路
This commit is contained in:
@@ -12,6 +12,7 @@ pub struct App {
|
||||
pub input: String,
|
||||
pub cursor: usize,
|
||||
pub quit: bool,
|
||||
pub shutdown_confirm: Option<std::time::Instant>,
|
||||
/// Lines waiting to be flushed to terminal via insert_before.
|
||||
pub output_queue: Vec<OutputItem>,
|
||||
/// Partial streaming text not yet terminated by newline.
|
||||
@@ -55,6 +56,7 @@ impl App {
|
||||
input: String::new(),
|
||||
cursor: 0,
|
||||
quit: false,
|
||||
shutdown_confirm: None,
|
||||
output_queue: Vec::new(),
|
||||
pending_text: String::new(),
|
||||
}
|
||||
@@ -193,6 +195,9 @@ impl App {
|
||||
self.output_queue.insert(1, OutputItem::Blank);
|
||||
}
|
||||
}
|
||||
Event::Shutdown => {
|
||||
self.quit = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -168,6 +168,9 @@ fn handle_key(app: &mut App, key: KeyEvent) -> Option<Method> {
|
||||
}
|
||||
KeyCode::Char('r') if key.modifiers.contains(KeyModifiers::CONTROL) => Some(Method::Resume),
|
||||
KeyCode::Char('x') if key.modifiers.contains(KeyModifiers::CONTROL) => Some(Method::Cancel),
|
||||
KeyCode::Char('d') if key.modifiers.contains(KeyModifiers::CONTROL) => {
|
||||
return handle_shutdown(app);
|
||||
}
|
||||
KeyCode::Enter => app.submit_input(),
|
||||
KeyCode::Backspace => {
|
||||
app.delete_char_before();
|
||||
@@ -200,3 +203,23 @@ fn handle_key(app: &mut App, key: KeyEvent) -> Option<Method> {
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
const SHUTDOWN_CONFIRM_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(3);
|
||||
|
||||
fn handle_shutdown(app: &mut App) -> Option<Method> {
|
||||
if !app.running {
|
||||
return Some(Method::Shutdown);
|
||||
}
|
||||
if let Some(t) = app.shutdown_confirm {
|
||||
if t.elapsed() < SHUTDOWN_CONFIRM_TIMEOUT {
|
||||
app.shutdown_confirm = None;
|
||||
return Some(Method::Shutdown);
|
||||
}
|
||||
}
|
||||
app.shutdown_confirm = Some(std::time::Instant::now());
|
||||
app.output_queue.push(app::OutputItem::Padded(
|
||||
app::MessageKind::Error,
|
||||
"Turn is running. Press Ctrl-D again to cancel and shut down.".into(),
|
||||
));
|
||||
None
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user