2025-09-26 16:35:56 -07:00
|
|
|
use std::io::IsTerminal;
|
2025-05-16 16:16:50 -07:00
|
|
|
use std::io::Result;
|
feat: initial import of Rust implementation of Codex CLI in codex-rs/ (#629)
As stated in `codex-rs/README.md`:
Today, Codex CLI is written in TypeScript and requires Node.js 22+ to
run it. For a number of users, this runtime requirement inhibits
adoption: they would be better served by a standalone executable. As
maintainers, we want Codex to run efficiently in a wide range of
environments with minimal overhead. We also want to take advantage of
operating system-specific APIs to provide better sandboxing, where
possible.
To that end, we are moving forward with a Rust implementation of Codex
CLI contained in this folder, which has the following benefits:
- The CLI compiles to small, standalone, platform-specific binaries.
- Can make direct, native calls to
[seccomp](https://man7.org/linux/man-pages/man2/seccomp.2.html) and
[landlock](https://man7.org/linux/man-pages/man7/landlock.7.html) in
order to support sandboxing on Linux.
- No runtime garbage collection, resulting in lower memory consumption
and better, more predictable performance.
Currently, the Rust implementation is materially behind the TypeScript
implementation in functionality, so continue to use the TypeScript
implmentation for the time being. We will publish native executables via
GitHub Releases as soon as we feel the Rust version is usable.
2025-04-24 13:31:40 -07:00
|
|
|
use std::io::Stdout;
|
2025-05-07 08:37:48 -07:00
|
|
|
use std::io::stdout;
|
2025-08-20 13:47:24 -07:00
|
|
|
use std::pin::Pin;
|
2025-08-22 09:41:15 -07:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
use std::sync::atomic::AtomicBool;
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
use std::sync::atomic::AtomicU8;
|
2025-08-27 14:17:10 -07:00
|
|
|
#[cfg(unix)]
|
|
|
|
|
use std::sync::atomic::AtomicU16;
|
2025-08-22 09:41:15 -07:00
|
|
|
use std::sync::atomic::Ordering;
|
2025-08-20 13:47:24 -07:00
|
|
|
use std::time::Duration;
|
|
|
|
|
use std::time::Instant;
|
feat: initial import of Rust implementation of Codex CLI in codex-rs/ (#629)
As stated in `codex-rs/README.md`:
Today, Codex CLI is written in TypeScript and requires Node.js 22+ to
run it. For a number of users, this runtime requirement inhibits
adoption: they would be better served by a standalone executable. As
maintainers, we want Codex to run efficiently in a wide range of
environments with minimal overhead. We also want to take advantage of
operating system-specific APIs to provide better sandboxing, where
possible.
To that end, we are moving forward with a Rust implementation of Codex
CLI contained in this folder, which has the following benefits:
- The CLI compiles to small, standalone, platform-specific binaries.
- Can make direct, native calls to
[seccomp](https://man7.org/linux/man-pages/man2/seccomp.2.html) and
[landlock](https://man7.org/linux/man-pages/man7/landlock.7.html) in
order to support sandboxing on Linux.
- No runtime garbage collection, resulting in lower memory consumption
and better, more predictable performance.
Currently, the Rust implementation is materially behind the TypeScript
implementation in functionality, so continue to use the TypeScript
implmentation for the time being. We will publish native executables via
GitHub Releases as soon as we feel the Rust version is usable.
2025-04-24 13:31:40 -07:00
|
|
|
|
2025-08-26 11:47:00 -07:00
|
|
|
use crossterm::Command;
|
2025-08-20 13:47:24 -07:00
|
|
|
use crossterm::SynchronizedUpdate;
|
2025-09-19 14:22:58 -07:00
|
|
|
#[cfg(unix)]
|
2025-08-05 22:01:34 -07:00
|
|
|
use crossterm::cursor::MoveTo;
|
2025-05-09 11:33:46 -07:00
|
|
|
use crossterm::event::DisableBracketedPaste;
|
2025-09-15 10:22:02 -07:00
|
|
|
use crossterm::event::DisableFocusChange;
|
2025-05-09 11:33:46 -07:00
|
|
|
use crossterm::event::EnableBracketedPaste;
|
2025-09-15 10:22:02 -07:00
|
|
|
use crossterm::event::EnableFocusChange;
|
2025-08-22 18:05:43 +01:00
|
|
|
use crossterm::event::Event;
|
2025-11-07 10:17:52 -08:00
|
|
|
#[cfg(unix)]
|
|
|
|
|
use crossterm::event::KeyCode;
|
2025-08-20 13:47:24 -07:00
|
|
|
use crossterm::event::KeyEvent;
|
2025-07-31 04:23:56 +01:00
|
|
|
use crossterm::event::KeyboardEnhancementFlags;
|
|
|
|
|
use crossterm::event::PopKeyboardEnhancementFlags;
|
|
|
|
|
use crossterm::event::PushKeyboardEnhancementFlags;
|
2025-08-22 09:41:15 -07:00
|
|
|
use crossterm::terminal::EnterAlternateScreen;
|
|
|
|
|
use crossterm::terminal::LeaveAlternateScreen;
|
2025-09-19 14:38:36 -07:00
|
|
|
use crossterm::terminal::supports_keyboard_enhancement;
|
2025-08-20 13:47:24 -07:00
|
|
|
use ratatui::backend::Backend;
|
feat: initial import of Rust implementation of Codex CLI in codex-rs/ (#629)
As stated in `codex-rs/README.md`:
Today, Codex CLI is written in TypeScript and requires Node.js 22+ to
run it. For a number of users, this runtime requirement inhibits
adoption: they would be better served by a standalone executable. As
maintainers, we want Codex to run efficiently in a wide range of
environments with minimal overhead. We also want to take advantage of
operating system-specific APIs to provide better sandboxing, where
possible.
To that end, we are moving forward with a Rust implementation of Codex
CLI contained in this folder, which has the following benefits:
- The CLI compiles to small, standalone, platform-specific binaries.
- Can make direct, native calls to
[seccomp](https://man7.org/linux/man-pages/man2/seccomp.2.html) and
[landlock](https://man7.org/linux/man-pages/man7/landlock.7.html) in
order to support sandboxing on Linux.
- No runtime garbage collection, resulting in lower memory consumption
and better, more predictable performance.
Currently, the Rust implementation is materially behind the TypeScript
implementation in functionality, so continue to use the TypeScript
implmentation for the time being. We will publish native executables via
GitHub Releases as soon as we feel the Rust version is usable.
2025-04-24 13:31:40 -07:00
|
|
|
use ratatui::backend::CrosstermBackend;
|
|
|
|
|
use ratatui::crossterm::execute;
|
2025-05-07 08:37:48 -07:00
|
|
|
use ratatui::crossterm::terminal::disable_raw_mode;
|
|
|
|
|
use ratatui::crossterm::terminal::enable_raw_mode;
|
2025-08-20 13:47:24 -07:00
|
|
|
use ratatui::layout::Offset;
|
|
|
|
|
use ratatui::text::Line;
|
feat: initial import of Rust implementation of Codex CLI in codex-rs/ (#629)
As stated in `codex-rs/README.md`:
Today, Codex CLI is written in TypeScript and requires Node.js 22+ to
run it. For a number of users, this runtime requirement inhibits
adoption: they would be better served by a standalone executable. As
maintainers, we want Codex to run efficiently in a wide range of
environments with minimal overhead. We also want to take advantage of
operating system-specific APIs to provide better sandboxing, where
possible.
To that end, we are moving forward with a Rust implementation of Codex
CLI contained in this folder, which has the following benefits:
- The CLI compiles to small, standalone, platform-specific binaries.
- Can make direct, native calls to
[seccomp](https://man7.org/linux/man-pages/man2/seccomp.2.html) and
[landlock](https://man7.org/linux/man-pages/man7/landlock.7.html) in
order to support sandboxing on Linux.
- No runtime garbage collection, resulting in lower memory consumption
and better, more predictable performance.
Currently, the Rust implementation is materially behind the TypeScript
implementation in functionality, so continue to use the TypeScript
implmentation for the time being. We will publish native executables via
GitHub Releases as soon as we feel the Rust version is usable.
2025-04-24 13:31:40 -07:00
|
|
|
|
2025-08-20 13:47:24 -07:00
|
|
|
use crate::custom_terminal;
|
|
|
|
|
use crate::custom_terminal::Terminal as CustomTerminal;
|
2025-11-07 10:17:52 -08:00
|
|
|
#[cfg(unix)]
|
|
|
|
|
use crate::key_hint;
|
2025-08-20 13:47:24 -07:00
|
|
|
use tokio::select;
|
|
|
|
|
use tokio_stream::Stream;
|
2025-07-30 17:06:55 -07:00
|
|
|
|
feat: initial import of Rust implementation of Codex CLI in codex-rs/ (#629)
As stated in `codex-rs/README.md`:
Today, Codex CLI is written in TypeScript and requires Node.js 22+ to
run it. For a number of users, this runtime requirement inhibits
adoption: they would be better served by a standalone executable. As
maintainers, we want Codex to run efficiently in a wide range of
environments with minimal overhead. We also want to take advantage of
operating system-specific APIs to provide better sandboxing, where
possible.
To that end, we are moving forward with a Rust implementation of Codex
CLI contained in this folder, which has the following benefits:
- The CLI compiles to small, standalone, platform-specific binaries.
- Can make direct, native calls to
[seccomp](https://man7.org/linux/man-pages/man2/seccomp.2.html) and
[landlock](https://man7.org/linux/man-pages/man7/landlock.7.html) in
order to support sandboxing on Linux.
- No runtime garbage collection, resulting in lower memory consumption
and better, more predictable performance.
Currently, the Rust implementation is materially behind the TypeScript
implementation in functionality, so continue to use the TypeScript
implmentation for the time being. We will publish native executables via
GitHub Releases as soon as we feel the Rust version is usable.
2025-04-24 13:31:40 -07:00
|
|
|
/// A type alias for the terminal type used in this application
|
2025-08-20 13:47:24 -07:00
|
|
|
pub type Terminal = CustomTerminal<CrosstermBackend<Stdout>>;
|
feat: initial import of Rust implementation of Codex CLI in codex-rs/ (#629)
As stated in `codex-rs/README.md`:
Today, Codex CLI is written in TypeScript and requires Node.js 22+ to
run it. For a number of users, this runtime requirement inhibits
adoption: they would be better served by a standalone executable. As
maintainers, we want Codex to run efficiently in a wide range of
environments with minimal overhead. We also want to take advantage of
operating system-specific APIs to provide better sandboxing, where
possible.
To that end, we are moving forward with a Rust implementation of Codex
CLI contained in this folder, which has the following benefits:
- The CLI compiles to small, standalone, platform-specific binaries.
- Can make direct, native calls to
[seccomp](https://man7.org/linux/man-pages/man2/seccomp.2.html) and
[landlock](https://man7.org/linux/man-pages/man7/landlock.7.html) in
order to support sandboxing on Linux.
- No runtime garbage collection, resulting in lower memory consumption
and better, more predictable performance.
Currently, the Rust implementation is materially behind the TypeScript
implementation in functionality, so continue to use the TypeScript
implmentation for the time being. We will publish native executables via
GitHub Releases as soon as we feel the Rust version is usable.
2025-04-24 13:31:40 -07:00
|
|
|
|
2025-11-07 10:17:52 -08:00
|
|
|
#[cfg(unix)]
|
|
|
|
|
const SUSPEND_KEY: key_hint::KeyBinding = key_hint::ctrl(KeyCode::Char('z'));
|
|
|
|
|
|
2025-08-20 13:47:24 -07:00
|
|
|
pub fn set_modes() -> Result<()> {
|
2025-05-09 11:33:46 -07:00
|
|
|
execute!(stdout(), EnableBracketedPaste)?;
|
2025-05-16 16:16:50 -07:00
|
|
|
|
feat: initial import of Rust implementation of Codex CLI in codex-rs/ (#629)
As stated in `codex-rs/README.md`:
Today, Codex CLI is written in TypeScript and requires Node.js 22+ to
run it. For a number of users, this runtime requirement inhibits
adoption: they would be better served by a standalone executable. As
maintainers, we want Codex to run efficiently in a wide range of
environments with minimal overhead. We also want to take advantage of
operating system-specific APIs to provide better sandboxing, where
possible.
To that end, we are moving forward with a Rust implementation of Codex
CLI contained in this folder, which has the following benefits:
- The CLI compiles to small, standalone, platform-specific binaries.
- Can make direct, native calls to
[seccomp](https://man7.org/linux/man-pages/man2/seccomp.2.html) and
[landlock](https://man7.org/linux/man-pages/man7/landlock.7.html) in
order to support sandboxing on Linux.
- No runtime garbage collection, resulting in lower memory consumption
and better, more predictable performance.
Currently, the Rust implementation is materially behind the TypeScript
implementation in functionality, so continue to use the TypeScript
implmentation for the time being. We will publish native executables via
GitHub Releases as soon as we feel the Rust version is usable.
2025-04-24 13:31:40 -07:00
|
|
|
enable_raw_mode()?;
|
2025-07-31 04:23:56 +01:00
|
|
|
// Enable keyboard enhancement flags so modifiers for keys like Enter are disambiguated.
|
|
|
|
|
// chat_composer.rs is using a keyboard event listener to enter for any modified keys
|
|
|
|
|
// to create a new line that require this.
|
2025-08-08 14:57:16 -03:00
|
|
|
// Some terminals (notably legacy Windows consoles) do not support
|
|
|
|
|
// keyboard enhancement flags. Attempt to enable them, but continue
|
|
|
|
|
// gracefully if unsupported.
|
|
|
|
|
let _ = execute!(
|
2025-07-31 04:23:56 +01:00
|
|
|
stdout(),
|
|
|
|
|
PushKeyboardEnhancementFlags(
|
|
|
|
|
KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES
|
|
|
|
|
| KeyboardEnhancementFlags::REPORT_EVENT_TYPES
|
|
|
|
|
| KeyboardEnhancementFlags::REPORT_ALTERNATE_KEYS
|
|
|
|
|
)
|
2025-08-08 14:57:16 -03:00
|
|
|
);
|
2025-09-15 10:22:02 -07:00
|
|
|
|
|
|
|
|
let _ = execute!(stdout(), EnableFocusChange);
|
2025-08-20 13:47:24 -07:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-26 11:47:00 -07:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
|
struct EnableAlternateScroll;
|
|
|
|
|
|
|
|
|
|
impl Command for EnableAlternateScroll {
|
|
|
|
|
fn write_ansi(&self, f: &mut impl std::fmt::Write) -> std::fmt::Result {
|
|
|
|
|
write!(f, "\x1b[?1007h")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
fn execute_winapi(&self) -> std::io::Result<()> {
|
|
|
|
|
Err(std::io::Error::other(
|
|
|
|
|
"tried to execute EnableAlternateScroll using WinAPI; use ANSI instead",
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
fn is_ansi_code_supported(&self) -> bool {
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
|
struct DisableAlternateScroll;
|
|
|
|
|
|
|
|
|
|
impl Command for DisableAlternateScroll {
|
|
|
|
|
fn write_ansi(&self, f: &mut impl std::fmt::Write) -> std::fmt::Result {
|
|
|
|
|
write!(f, "\x1b[?1007l")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
fn execute_winapi(&self) -> std::io::Result<()> {
|
|
|
|
|
Err(std::io::Error::other(
|
|
|
|
|
"tried to execute DisableAlternateScroll using WinAPI; use ANSI instead",
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
fn is_ansi_code_supported(&self) -> bool {
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 13:47:24 -07:00
|
|
|
/// Restore the terminal to its original state.
|
|
|
|
|
/// Inverse of `set_modes`.
|
|
|
|
|
pub fn restore() -> Result<()> {
|
|
|
|
|
// Pop may fail on platforms that didn't support the push; ignore errors.
|
|
|
|
|
let _ = execute!(stdout(), PopKeyboardEnhancementFlags);
|
|
|
|
|
execute!(stdout(), DisableBracketedPaste)?;
|
2025-09-15 10:22:02 -07:00
|
|
|
let _ = execute!(stdout(), DisableFocusChange);
|
2025-08-20 13:47:24 -07:00
|
|
|
disable_raw_mode()?;
|
|
|
|
|
let _ = execute!(stdout(), crossterm::cursor::Show);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Initialize the terminal (inline viewport; history stays in normal scrollback)
|
|
|
|
|
pub fn init() -> Result<Terminal> {
|
2025-09-26 16:35:56 -07:00
|
|
|
if !stdout().is_terminal() {
|
|
|
|
|
return Err(std::io::Error::other("stdout is not a terminal"));
|
|
|
|
|
}
|
2025-08-20 13:47:24 -07:00
|
|
|
set_modes()?;
|
|
|
|
|
|
feat: initial import of Rust implementation of Codex CLI in codex-rs/ (#629)
As stated in `codex-rs/README.md`:
Today, Codex CLI is written in TypeScript and requires Node.js 22+ to
run it. For a number of users, this runtime requirement inhibits
adoption: they would be better served by a standalone executable. As
maintainers, we want Codex to run efficiently in a wide range of
environments with minimal overhead. We also want to take advantage of
operating system-specific APIs to provide better sandboxing, where
possible.
To that end, we are moving forward with a Rust implementation of Codex
CLI contained in this folder, which has the following benefits:
- The CLI compiles to small, standalone, platform-specific binaries.
- Can make direct, native calls to
[seccomp](https://man7.org/linux/man-pages/man2/seccomp.2.html) and
[landlock](https://man7.org/linux/man-pages/man7/landlock.7.html) in
order to support sandboxing on Linux.
- No runtime garbage collection, resulting in lower memory consumption
and better, more predictable performance.
Currently, the Rust implementation is materially behind the TypeScript
implementation in functionality, so continue to use the TypeScript
implmentation for the time being. We will publish native executables via
GitHub Releases as soon as we feel the Rust version is usable.
2025-04-24 13:31:40 -07:00
|
|
|
set_panic_hook();
|
2025-07-25 01:56:40 -07:00
|
|
|
|
|
|
|
|
let backend = CrosstermBackend::new(stdout());
|
2025-08-20 13:47:24 -07:00
|
|
|
let tui = CustomTerminal::with_options(backend)?;
|
2025-07-25 01:56:40 -07:00
|
|
|
Ok(tui)
|
feat: initial import of Rust implementation of Codex CLI in codex-rs/ (#629)
As stated in `codex-rs/README.md`:
Today, Codex CLI is written in TypeScript and requires Node.js 22+ to
run it. For a number of users, this runtime requirement inhibits
adoption: they would be better served by a standalone executable. As
maintainers, we want Codex to run efficiently in a wide range of
environments with minimal overhead. We also want to take advantage of
operating system-specific APIs to provide better sandboxing, where
possible.
To that end, we are moving forward with a Rust implementation of Codex
CLI contained in this folder, which has the following benefits:
- The CLI compiles to small, standalone, platform-specific binaries.
- Can make direct, native calls to
[seccomp](https://man7.org/linux/man-pages/man2/seccomp.2.html) and
[landlock](https://man7.org/linux/man-pages/man7/landlock.7.html) in
order to support sandboxing on Linux.
- No runtime garbage collection, resulting in lower memory consumption
and better, more predictable performance.
Currently, the Rust implementation is materially behind the TypeScript
implementation in functionality, so continue to use the TypeScript
implmentation for the time being. We will publish native executables via
GitHub Releases as soon as we feel the Rust version is usable.
2025-04-24 13:31:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn set_panic_hook() {
|
|
|
|
|
let hook = std::panic::take_hook();
|
|
|
|
|
std::panic::set_hook(Box::new(move |panic_info| {
|
|
|
|
|
let _ = restore(); // ignore any errors as we are already failing
|
|
|
|
|
hook(panic_info);
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 13:47:24 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum TuiEvent {
|
|
|
|
|
Key(KeyEvent),
|
|
|
|
|
Paste(String),
|
|
|
|
|
Draw,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Tui {
|
|
|
|
|
frame_schedule_tx: tokio::sync::mpsc::UnboundedSender<Instant>,
|
|
|
|
|
draw_tx: tokio::sync::broadcast::Sender<()>,
|
|
|
|
|
pub(crate) terminal: Terminal,
|
|
|
|
|
pending_history_lines: Vec<Line<'static>>,
|
2025-08-22 09:41:15 -07:00
|
|
|
alt_saved_viewport: Option<ratatui::layout::Rect>,
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
resume_pending: Arc<AtomicU8>, // Stores a ResumeAction
|
2025-08-27 14:17:10 -07:00
|
|
|
#[cfg(unix)]
|
|
|
|
|
suspend_cursor_y: Arc<AtomicU16>, // Bottom line of inline viewport
|
2025-08-22 09:41:15 -07:00
|
|
|
// True when overlay alt-screen UI is active
|
|
|
|
|
alt_screen_active: Arc<AtomicBool>,
|
2025-09-15 10:22:02 -07:00
|
|
|
// True when terminal/tab is focused; updated internally from crossterm events
|
|
|
|
|
terminal_focused: Arc<AtomicBool>,
|
2025-09-19 14:38:36 -07:00
|
|
|
enhanced_keys_supported: bool,
|
2025-08-22 09:41:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
|
|
|
|
#[repr(u8)]
|
|
|
|
|
enum ResumeAction {
|
|
|
|
|
None = 0,
|
|
|
|
|
RealignInline = 1,
|
|
|
|
|
RestoreAlt = 2,
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-22 16:33:18 -07:00
|
|
|
#[cfg(unix)]
|
|
|
|
|
enum PreparedResumeAction {
|
|
|
|
|
RestoreAltScreen,
|
|
|
|
|
RealignViewport(ratatui::layout::Rect),
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-22 09:41:15 -07:00
|
|
|
#[cfg(unix)]
|
|
|
|
|
fn take_resume_action(pending: &AtomicU8) -> ResumeAction {
|
|
|
|
|
match pending.swap(ResumeAction::None as u8, Ordering::Relaxed) {
|
|
|
|
|
1 => ResumeAction::RealignInline,
|
|
|
|
|
2 => ResumeAction::RestoreAlt,
|
|
|
|
|
_ => ResumeAction::None,
|
|
|
|
|
}
|
2025-08-20 13:47:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub struct FrameRequester {
|
|
|
|
|
frame_schedule_tx: tokio::sync::mpsc::UnboundedSender<Instant>,
|
|
|
|
|
}
|
|
|
|
|
impl FrameRequester {
|
|
|
|
|
pub fn schedule_frame(&self) {
|
|
|
|
|
let _ = self.frame_schedule_tx.send(Instant::now());
|
|
|
|
|
}
|
|
|
|
|
pub fn schedule_frame_in(&self, dur: Duration) {
|
|
|
|
|
let _ = self.frame_schedule_tx.send(Instant::now() + dur);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
impl FrameRequester {
|
|
|
|
|
/// Create a no-op frame requester for tests.
|
|
|
|
|
pub(crate) fn test_dummy() -> Self {
|
|
|
|
|
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
|
|
|
|
|
FrameRequester {
|
|
|
|
|
frame_schedule_tx: tx,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Tui {
|
|
|
|
|
pub fn new(terminal: Terminal) -> Self {
|
|
|
|
|
let (frame_schedule_tx, frame_schedule_rx) = tokio::sync::mpsc::unbounded_channel();
|
|
|
|
|
let (draw_tx, _) = tokio::sync::broadcast::channel(1);
|
|
|
|
|
|
2025-11-07 10:17:52 -08:00
|
|
|
spawn_frame_scheduler(frame_schedule_rx, draw_tx.clone());
|
2025-08-20 13:47:24 -07:00
|
|
|
|
2025-09-19 14:38:36 -07:00
|
|
|
// Detect keyboard enhancement support before any EventStream is created so the
|
|
|
|
|
// crossterm poller can acquire its lock without contention.
|
|
|
|
|
let enhanced_keys_supported = supports_keyboard_enhancement().unwrap_or(false);
|
2025-09-26 16:35:56 -07:00
|
|
|
// Cache this to avoid contention with the event reader.
|
|
|
|
|
supports_color::on_cached(supports_color::Stream::Stdout);
|
|
|
|
|
let _ = crate::terminal_palette::default_colors();
|
2025-09-19 14:38:36 -07:00
|
|
|
|
2025-08-20 13:47:24 -07:00
|
|
|
Self {
|
|
|
|
|
frame_schedule_tx,
|
|
|
|
|
draw_tx,
|
|
|
|
|
terminal,
|
|
|
|
|
pending_history_lines: vec![],
|
2025-08-22 09:41:15 -07:00
|
|
|
alt_saved_viewport: None,
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
resume_pending: Arc::new(AtomicU8::new(0)),
|
2025-08-27 14:17:10 -07:00
|
|
|
#[cfg(unix)]
|
|
|
|
|
suspend_cursor_y: Arc::new(AtomicU16::new(0)),
|
2025-08-22 09:41:15 -07:00
|
|
|
alt_screen_active: Arc::new(AtomicBool::new(false)),
|
2025-09-15 10:22:02 -07:00
|
|
|
terminal_focused: Arc::new(AtomicBool::new(true)),
|
2025-09-19 14:38:36 -07:00
|
|
|
enhanced_keys_supported,
|
2025-08-20 13:47:24 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn frame_requester(&self) -> FrameRequester {
|
|
|
|
|
FrameRequester {
|
|
|
|
|
frame_schedule_tx: self.frame_schedule_tx.clone(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 14:38:36 -07:00
|
|
|
pub fn enhanced_keys_supported(&self) -> bool {
|
|
|
|
|
self.enhanced_keys_supported
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-07 10:17:52 -08:00
|
|
|
/// Emit a desktop notification now if the terminal is unfocused.
|
|
|
|
|
/// Returns true if a notification was posted.
|
|
|
|
|
pub fn notify(&mut self, message: impl AsRef<str>) -> bool {
|
|
|
|
|
if !self.terminal_focused.load(Ordering::Relaxed) {
|
|
|
|
|
let _ = execute!(stdout(), PostNotification(message.as_ref().to_string()));
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 13:47:24 -07:00
|
|
|
pub fn event_stream(&self) -> Pin<Box<dyn Stream<Item = TuiEvent> + Send + 'static>> {
|
|
|
|
|
use tokio_stream::StreamExt;
|
2025-11-07 10:17:52 -08:00
|
|
|
|
2025-08-20 13:47:24 -07:00
|
|
|
let mut crossterm_events = crossterm::event::EventStream::new();
|
|
|
|
|
let mut draw_rx = self.draw_tx.subscribe();
|
2025-11-07 10:17:52 -08:00
|
|
|
|
|
|
|
|
// State for tracking how we should resume from ^Z suspend.
|
2025-08-22 09:41:15 -07:00
|
|
|
#[cfg(unix)]
|
|
|
|
|
let resume_pending = self.resume_pending.clone();
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
let alt_screen_active = self.alt_screen_active.clone();
|
2025-08-27 14:17:10 -07:00
|
|
|
#[cfg(unix)]
|
|
|
|
|
let suspend_cursor_y = self.suspend_cursor_y.clone();
|
2025-11-07 10:17:52 -08:00
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
let suspend = move || {
|
|
|
|
|
if alt_screen_active.load(Ordering::Relaxed) {
|
|
|
|
|
// Disable alternate scroll when suspending from alt-screen
|
|
|
|
|
let _ = execute!(stdout(), DisableAlternateScroll);
|
|
|
|
|
let _ = execute!(stdout(), LeaveAlternateScreen);
|
|
|
|
|
resume_pending.store(ResumeAction::RestoreAlt as u8, Ordering::Relaxed);
|
|
|
|
|
} else {
|
|
|
|
|
resume_pending.store(ResumeAction::RealignInline as u8, Ordering::Relaxed);
|
|
|
|
|
}
|
|
|
|
|
let y = suspend_cursor_y.load(Ordering::Relaxed);
|
|
|
|
|
let _ = execute!(stdout(), MoveTo(0, y), crossterm::cursor::Show);
|
|
|
|
|
let _ = Tui::suspend();
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-15 10:22:02 -07:00
|
|
|
let terminal_focused = self.terminal_focused.clone();
|
2025-08-20 13:47:24 -07:00
|
|
|
let event_stream = async_stream::stream! {
|
|
|
|
|
loop {
|
|
|
|
|
select! {
|
|
|
|
|
Some(Ok(event)) = crossterm_events.next() => {
|
|
|
|
|
match event {
|
2025-08-22 09:41:15 -07:00
|
|
|
crossterm::event::Event::Key(key_event) => {
|
2025-08-20 13:47:24 -07:00
|
|
|
#[cfg(unix)]
|
2025-11-07 10:17:52 -08:00
|
|
|
if SUSPEND_KEY.is_press(key_event) {
|
|
|
|
|
suspend();
|
|
|
|
|
// We continue here after resume.
|
2025-08-20 13:47:24 -07:00
|
|
|
yield TuiEvent::Draw;
|
2025-08-22 09:41:15 -07:00
|
|
|
continue;
|
2025-08-20 13:47:24 -07:00
|
|
|
}
|
|
|
|
|
yield TuiEvent::Key(key_event);
|
|
|
|
|
}
|
2025-08-22 18:05:43 +01:00
|
|
|
Event::Resize(_, _) => {
|
2025-08-20 13:47:24 -07:00
|
|
|
yield TuiEvent::Draw;
|
|
|
|
|
}
|
2025-08-22 18:05:43 +01:00
|
|
|
Event::Paste(pasted) => {
|
2025-08-20 13:47:24 -07:00
|
|
|
yield TuiEvent::Paste(pasted);
|
|
|
|
|
}
|
2025-09-15 10:22:02 -07:00
|
|
|
Event::FocusGained => {
|
|
|
|
|
terminal_focused.store(true, Ordering::Relaxed);
|
2025-10-03 15:43:41 -07:00
|
|
|
crate::terminal_palette::requery_default_colors();
|
|
|
|
|
yield TuiEvent::Draw;
|
2025-09-15 10:22:02 -07:00
|
|
|
}
|
|
|
|
|
Event::FocusLost => {
|
|
|
|
|
terminal_focused.store(false, Ordering::Relaxed);
|
|
|
|
|
}
|
2025-08-20 13:47:24 -07:00
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result = draw_rx.recv() => {
|
|
|
|
|
match result {
|
|
|
|
|
Ok(_) => {
|
|
|
|
|
yield TuiEvent::Draw;
|
|
|
|
|
}
|
|
|
|
|
Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {
|
|
|
|
|
// We dropped one or more draw notifications; coalesce to a single draw.
|
|
|
|
|
yield TuiEvent::Draw;
|
|
|
|
|
}
|
|
|
|
|
Err(tokio::sync::broadcast::error::RecvError::Closed) => {
|
|
|
|
|
// Sender dropped; stop emitting draws from this source.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
Box::pin(event_stream)
|
|
|
|
|
}
|
2025-11-07 10:17:52 -08:00
|
|
|
|
2025-08-20 13:47:24 -07:00
|
|
|
#[cfg(unix)]
|
|
|
|
|
fn suspend() -> Result<()> {
|
|
|
|
|
restore()?;
|
|
|
|
|
unsafe { libc::kill(0, libc::SIGTSTP) };
|
|
|
|
|
set_modes()?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-07 10:17:52 -08:00
|
|
|
/// When resuming from ^Z suspend, we want to put things back the way they were before suspend.
|
|
|
|
|
/// We capture the action in an object so we can pass it into the event stream, since the relevant
|
2025-08-22 09:41:15 -07:00
|
|
|
#[cfg(unix)]
|
2025-08-22 16:33:18 -07:00
|
|
|
fn prepare_resume_action(
|
|
|
|
|
&mut self,
|
|
|
|
|
action: ResumeAction,
|
|
|
|
|
) -> Result<Option<PreparedResumeAction>> {
|
2025-08-22 09:41:15 -07:00
|
|
|
match action {
|
|
|
|
|
ResumeAction::RealignInline => {
|
2025-09-02 14:32:42 -07:00
|
|
|
let cursor_pos = self
|
|
|
|
|
.terminal
|
|
|
|
|
.get_cursor_position()
|
|
|
|
|
.unwrap_or(self.terminal.last_known_cursor_pos);
|
2025-08-22 16:33:18 -07:00
|
|
|
Ok(Some(PreparedResumeAction::RealignViewport(
|
|
|
|
|
ratatui::layout::Rect::new(0, cursor_pos.y, 0, 0),
|
|
|
|
|
)))
|
2025-08-22 09:41:15 -07:00
|
|
|
}
|
|
|
|
|
ResumeAction::RestoreAlt => {
|
2025-08-27 14:17:10 -07:00
|
|
|
if let Ok(ratatui::layout::Position { y, .. }) = self.terminal.get_cursor_position()
|
2025-08-22 09:41:15 -07:00
|
|
|
&& let Some(saved) = self.alt_saved_viewport.as_mut()
|
|
|
|
|
{
|
|
|
|
|
saved.y = y;
|
|
|
|
|
}
|
2025-08-22 16:33:18 -07:00
|
|
|
Ok(Some(PreparedResumeAction::RestoreAltScreen))
|
|
|
|
|
}
|
|
|
|
|
ResumeAction::None => Ok(None),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
fn apply_prepared_resume_action(&mut self, prepared: PreparedResumeAction) -> Result<()> {
|
|
|
|
|
match prepared {
|
|
|
|
|
PreparedResumeAction::RealignViewport(area) => {
|
|
|
|
|
self.terminal.set_viewport_area(area);
|
|
|
|
|
}
|
|
|
|
|
PreparedResumeAction::RestoreAltScreen => {
|
|
|
|
|
execute!(self.terminal.backend_mut(), EnterAlternateScreen)?;
|
2025-08-26 11:47:00 -07:00
|
|
|
// Enable "alternate scroll" so terminals may translate wheel to arrows
|
|
|
|
|
execute!(self.terminal.backend_mut(), EnableAlternateScroll)?;
|
2025-08-22 09:41:15 -07:00
|
|
|
if let Ok(size) = self.terminal.size() {
|
|
|
|
|
self.terminal.set_viewport_area(ratatui::layout::Rect::new(
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
size.width,
|
|
|
|
|
size.height,
|
|
|
|
|
));
|
|
|
|
|
self.terminal.clear()?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Enter alternate screen and expand the viewport to full terminal size, saving the current
|
|
|
|
|
/// inline viewport for restoration when leaving.
|
|
|
|
|
pub fn enter_alt_screen(&mut self) -> Result<()> {
|
|
|
|
|
let _ = execute!(self.terminal.backend_mut(), EnterAlternateScreen);
|
2025-08-26 11:47:00 -07:00
|
|
|
// Enable "alternate scroll" so terminals may translate wheel to arrows
|
|
|
|
|
let _ = execute!(self.terminal.backend_mut(), EnableAlternateScroll);
|
2025-08-22 09:41:15 -07:00
|
|
|
if let Ok(size) = self.terminal.size() {
|
|
|
|
|
self.alt_saved_viewport = Some(self.terminal.viewport_area);
|
|
|
|
|
self.terminal.set_viewport_area(ratatui::layout::Rect::new(
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
size.width,
|
|
|
|
|
size.height,
|
|
|
|
|
));
|
|
|
|
|
let _ = self.terminal.clear();
|
|
|
|
|
}
|
|
|
|
|
self.alt_screen_active.store(true, Ordering::Relaxed);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Leave alternate screen and restore the previously saved inline viewport, if any.
|
|
|
|
|
pub fn leave_alt_screen(&mut self) -> Result<()> {
|
2025-08-26 11:47:00 -07:00
|
|
|
// Disable alternate scroll when leaving alt-screen
|
|
|
|
|
let _ = execute!(self.terminal.backend_mut(), DisableAlternateScroll);
|
2025-08-22 09:41:15 -07:00
|
|
|
let _ = execute!(self.terminal.backend_mut(), LeaveAlternateScreen);
|
|
|
|
|
if let Some(saved) = self.alt_saved_viewport.take() {
|
|
|
|
|
self.terminal.set_viewport_area(saved);
|
|
|
|
|
}
|
|
|
|
|
self.alt_screen_active.store(false, Ordering::Relaxed);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 13:47:24 -07:00
|
|
|
pub fn insert_history_lines(&mut self, lines: Vec<Line<'static>>) {
|
|
|
|
|
self.pending_history_lines.extend(lines);
|
|
|
|
|
self.frame_requester().schedule_frame();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn draw(
|
|
|
|
|
&mut self,
|
|
|
|
|
height: u16,
|
|
|
|
|
draw_fn: impl FnOnce(&mut custom_terminal::Frame),
|
|
|
|
|
) -> Result<()> {
|
2025-11-07 10:17:52 -08:00
|
|
|
// If we are resuming from ^Z, we need to prepare the resume action now so we can apply it
|
|
|
|
|
// in the synchronized update.
|
2025-08-22 16:33:18 -07:00
|
|
|
#[cfg(unix)]
|
|
|
|
|
let mut prepared_resume =
|
|
|
|
|
self.prepare_resume_action(take_resume_action(&self.resume_pending))?;
|
2025-11-07 10:17:52 -08:00
|
|
|
|
|
|
|
|
// Precompute any viewport updates that need a cursor-position query before entering
|
|
|
|
|
// the synchronized update, to avoid racing with the event reader.
|
|
|
|
|
let mut pending_viewport_area: Option<ratatui::layout::Rect> = None;
|
2025-08-22 16:33:18 -07:00
|
|
|
{
|
2025-08-20 13:47:24 -07:00
|
|
|
let terminal = &mut self.terminal;
|
|
|
|
|
let screen_size = terminal.size()?;
|
|
|
|
|
let last_known_screen_size = terminal.last_known_screen_size;
|
2025-09-02 14:32:42 -07:00
|
|
|
if screen_size != last_known_screen_size
|
|
|
|
|
&& let Ok(cursor_pos) = terminal.get_cursor_position()
|
|
|
|
|
{
|
2025-08-20 13:47:24 -07:00
|
|
|
let last_known_cursor_pos = terminal.last_known_cursor_pos;
|
2025-11-07 10:17:52 -08:00
|
|
|
// If we resized AND the cursor moved, we adjust the viewport area to keep the
|
|
|
|
|
// cursor in the same position. This is a heuristic that seems to work well
|
|
|
|
|
// at least in iTerm2.
|
2025-08-20 13:47:24 -07:00
|
|
|
if cursor_pos.y != last_known_cursor_pos.y {
|
|
|
|
|
let cursor_delta = cursor_pos.y as i32 - last_known_cursor_pos.y as i32;
|
|
|
|
|
let new_viewport_area = terminal.viewport_area.offset(Offset {
|
|
|
|
|
x: 0,
|
|
|
|
|
y: cursor_delta,
|
|
|
|
|
});
|
2025-08-22 16:33:18 -07:00
|
|
|
pending_viewport_area = Some(new_viewport_area);
|
2025-08-20 13:47:24 -07:00
|
|
|
}
|
|
|
|
|
}
|
2025-08-22 16:33:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::io::stdout().sync_update(|_| {
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
{
|
|
|
|
|
if let Some(prepared) = prepared_resume.take() {
|
|
|
|
|
self.apply_prepared_resume_action(prepared)?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let terminal = &mut self.terminal;
|
|
|
|
|
if let Some(new_area) = pending_viewport_area.take() {
|
|
|
|
|
terminal.set_viewport_area(new_area);
|
|
|
|
|
terminal.clear()?;
|
|
|
|
|
}
|
2025-08-20 13:47:24 -07:00
|
|
|
|
|
|
|
|
let size = terminal.size()?;
|
|
|
|
|
|
|
|
|
|
let mut area = terminal.viewport_area;
|
|
|
|
|
area.height = height.min(size.height);
|
|
|
|
|
area.width = size.width;
|
2025-11-07 10:17:52 -08:00
|
|
|
// If the viewport has expanded, scroll everything else up to make room.
|
2025-08-20 13:47:24 -07:00
|
|
|
if area.bottom() > size.height {
|
|
|
|
|
terminal
|
|
|
|
|
.backend_mut()
|
|
|
|
|
.scroll_region_up(0..area.top(), area.bottom() - size.height)?;
|
|
|
|
|
area.y = size.height - area.height;
|
|
|
|
|
}
|
|
|
|
|
if area != terminal.viewport_area {
|
2025-11-07 10:17:52 -08:00
|
|
|
// TODO(nornagon): probably this could be collapsed with the clear + set_viewport_area above.
|
2025-08-20 13:47:24 -07:00
|
|
|
terminal.clear()?;
|
|
|
|
|
terminal.set_viewport_area(area);
|
|
|
|
|
}
|
2025-11-07 10:17:52 -08:00
|
|
|
|
2025-08-20 13:47:24 -07:00
|
|
|
if !self.pending_history_lines.is_empty() {
|
|
|
|
|
crate::insert_history::insert_history_lines(
|
|
|
|
|
terminal,
|
|
|
|
|
self.pending_history_lines.clone(),
|
2025-10-30 18:07:51 -07:00
|
|
|
)?;
|
2025-08-20 13:47:24 -07:00
|
|
|
self.pending_history_lines.clear();
|
|
|
|
|
}
|
2025-11-07 10:17:52 -08:00
|
|
|
|
2025-08-27 14:17:10 -07:00
|
|
|
// Update the y position for suspending so Ctrl-Z can place the cursor correctly.
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
{
|
|
|
|
|
let inline_area_bottom = if self.alt_screen_active.load(Ordering::Relaxed) {
|
|
|
|
|
self.alt_saved_viewport
|
|
|
|
|
.map(|r| r.bottom().saturating_sub(1))
|
|
|
|
|
.unwrap_or_else(|| area.bottom().saturating_sub(1))
|
|
|
|
|
} else {
|
|
|
|
|
area.bottom().saturating_sub(1)
|
|
|
|
|
};
|
|
|
|
|
self.suspend_cursor_y
|
|
|
|
|
.store(inline_area_bottom, Ordering::Relaxed);
|
|
|
|
|
}
|
2025-11-07 10:17:52 -08:00
|
|
|
|
2025-08-20 13:47:24 -07:00
|
|
|
terminal.draw(|frame| {
|
|
|
|
|
draw_fn(frame);
|
2025-09-02 10:29:58 -07:00
|
|
|
})
|
2025-08-20 13:47:24 -07:00
|
|
|
})?
|
|
|
|
|
}
|
feat: initial import of Rust implementation of Codex CLI in codex-rs/ (#629)
As stated in `codex-rs/README.md`:
Today, Codex CLI is written in TypeScript and requires Node.js 22+ to
run it. For a number of users, this runtime requirement inhibits
adoption: they would be better served by a standalone executable. As
maintainers, we want Codex to run efficiently in a wide range of
environments with minimal overhead. We also want to take advantage of
operating system-specific APIs to provide better sandboxing, where
possible.
To that end, we are moving forward with a Rust implementation of Codex
CLI contained in this folder, which has the following benefits:
- The CLI compiles to small, standalone, platform-specific binaries.
- Can make direct, native calls to
[seccomp](https://man7.org/linux/man-pages/man2/seccomp.2.html) and
[landlock](https://man7.org/linux/man-pages/man7/landlock.7.html) in
order to support sandboxing on Linux.
- No runtime garbage collection, resulting in lower memory consumption
and better, more predictable performance.
Currently, the Rust implementation is materially behind the TypeScript
implementation in functionality, so continue to use the TypeScript
implmentation for the time being. We will publish native executables via
GitHub Releases as soon as we feel the Rust version is usable.
2025-04-24 13:31:40 -07:00
|
|
|
}
|
2025-09-15 10:22:02 -07:00
|
|
|
|
2025-11-07 10:17:52 -08:00
|
|
|
/// Spawn background scheduler to coalesce frame requests and emit draws at deadlines.
|
|
|
|
|
fn spawn_frame_scheduler(
|
|
|
|
|
frame_schedule_rx: tokio::sync::mpsc::UnboundedReceiver<Instant>,
|
|
|
|
|
draw_tx: tokio::sync::broadcast::Sender<()>,
|
|
|
|
|
) {
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
use tokio::select;
|
|
|
|
|
use tokio::time::Instant as TokioInstant;
|
|
|
|
|
use tokio::time::sleep_until;
|
|
|
|
|
|
|
|
|
|
let mut rx = frame_schedule_rx;
|
|
|
|
|
let mut next_deadline: Option<Instant> = None;
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
let target = next_deadline
|
|
|
|
|
.unwrap_or_else(|| Instant::now() + Duration::from_secs(60 * 60 * 24 * 365));
|
|
|
|
|
let sleep_fut = sleep_until(TokioInstant::from_std(target));
|
|
|
|
|
tokio::pin!(sleep_fut);
|
|
|
|
|
|
|
|
|
|
select! {
|
|
|
|
|
recv = rx.recv() => {
|
|
|
|
|
match recv {
|
|
|
|
|
Some(at) => {
|
|
|
|
|
if next_deadline.is_none_or(|cur| at < cur) {
|
|
|
|
|
next_deadline = Some(at);
|
|
|
|
|
}
|
|
|
|
|
// Do not send a draw immediately here. By continuing the loop,
|
|
|
|
|
// we recompute the sleep target so the draw fires once via the
|
|
|
|
|
// sleep branch, coalescing multiple requests into a single draw.
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
None => break,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ = &mut sleep_fut => {
|
|
|
|
|
if next_deadline.is_some() {
|
|
|
|
|
next_deadline = None;
|
|
|
|
|
let _ = draw_tx.send(());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-15 10:22:02 -07:00
|
|
|
/// Command that emits an OSC 9 desktop notification with a message.
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct PostNotification(pub String);
|
|
|
|
|
|
|
|
|
|
impl Command for PostNotification {
|
|
|
|
|
fn write_ansi(&self, f: &mut impl std::fmt::Write) -> std::fmt::Result {
|
|
|
|
|
write!(f, "\x1b]9;{}\x07", self.0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
fn execute_winapi(&self) -> std::io::Result<()> {
|
|
|
|
|
Err(std::io::Error::other(
|
|
|
|
|
"tried to execute PostNotification using WinAPI; use ANSI instead",
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
fn is_ansi_code_supported(&self) -> bool {
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|