mirror of
https://github.com/openai/codex.git
synced 2026-04-24 06:35:50 +00:00
add kpp check
This commit is contained in:
1
codex-rs/Cargo.lock
generated
1
codex-rs/Cargo.lock
generated
@@ -853,6 +853,7 @@ dependencies = [
|
||||
"image",
|
||||
"insta",
|
||||
"lazy_static",
|
||||
"libc",
|
||||
"mcp-types",
|
||||
"path-clean",
|
||||
"pretty_assertions",
|
||||
|
||||
@@ -32,6 +32,7 @@ color-eyre = "0.6.3"
|
||||
crossterm = { version = "0.28.1", features = ["bracketed-paste"] }
|
||||
image = { version = "^0.25.6", default-features = false, features = ["jpeg"] }
|
||||
lazy_static = "1"
|
||||
libc = "0.2"
|
||||
mcp-types = { path = "../mcp-types" }
|
||||
path-clean = "1.0.1"
|
||||
ratatui = { version = "0.29.0", features = [
|
||||
|
||||
@@ -712,11 +712,12 @@ impl WidgetRef for &ChatComposer<'_> {
|
||||
Span::from(" to quit"),
|
||||
]
|
||||
} else {
|
||||
let newline_hint = if crate::tui::is_kkp_enabled() { "Shift+⏎" } else { "Ctrl+J" };
|
||||
vec![
|
||||
Span::from(" "),
|
||||
"⏎".set_style(key_hint_style),
|
||||
Span::from(" send "),
|
||||
"Shift+⏎".set_style(key_hint_style),
|
||||
newline_hint.set_style(key_hint_style),
|
||||
Span::from(" newline "),
|
||||
"Ctrl+C".set_style(key_hint_style),
|
||||
Span::from(" quit"),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::io::Result;
|
||||
use std::io::Stdout;
|
||||
use std::io::stdout;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
use codex_core::config::Config;
|
||||
use crossterm::event::DisableBracketedPaste;
|
||||
@@ -18,6 +19,53 @@ use crate::custom_terminal::Terminal;
|
||||
/// A type alias for the terminal type used in this application
|
||||
pub type Tui = Terminal<CrosstermBackend<Stdout>>;
|
||||
|
||||
// Global flag indicating whether Kitty Keyboard Protocol (KKP) appears enabled.
|
||||
static KKP_ENABLED: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
/// Return whether KKP (alternate key reporting) appears enabled.
|
||||
pub(crate) fn is_kkp_enabled() -> bool {
|
||||
KKP_ENABLED.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Try to detect Kitty Keyboard Protocol support by issuing a progressive
|
||||
/// enhancement query and waiting briefly for a response.
|
||||
#[cfg(unix)]
|
||||
fn detect_kitty_protocol() -> std::io::Result<bool> {
|
||||
use std::io::{self, Read, Write};
|
||||
use std::os::unix::io::AsRawFd;
|
||||
|
||||
let mut stdout = io::stdout();
|
||||
let mut stdin = io::stdin();
|
||||
|
||||
// Send query for progressive enhancement + DA1
|
||||
write!(stdout, "\x1b[?u\x1b[c")?;
|
||||
stdout.flush()?;
|
||||
|
||||
// Wait up to ~200ms for a response
|
||||
let fd = stdin.as_raw_fd();
|
||||
let mut pfd = libc::pollfd {
|
||||
fd,
|
||||
events: libc::POLLIN,
|
||||
revents: 0,
|
||||
};
|
||||
let rc = unsafe { libc::poll(&mut pfd as *mut libc::pollfd, 1, 200) };
|
||||
if rc > 0 && (pfd.revents & libc::POLLIN) != 0 {
|
||||
let mut buf = [0u8; 256];
|
||||
if let Ok(n) = stdin.read(&mut buf) {
|
||||
let response = String::from_utf8_lossy(&buf[..n]);
|
||||
if response.contains("[?") && response.contains('u') {
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn detect_kitty_protocol() -> std::io::Result<bool> {
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
/// Initialize the terminal (inline viewport; history stays in normal scrollback)
|
||||
pub fn init(_config: &Config) -> Result<Tui> {
|
||||
execute!(stdout(), EnableBracketedPaste)?;
|
||||
@@ -34,6 +82,11 @@ pub fn init(_config: &Config) -> Result<Tui> {
|
||||
| KeyboardEnhancementFlags::REPORT_ALTERNATE_KEYS
|
||||
)
|
||||
)?;
|
||||
|
||||
// Detect KKP availability; used to adjust UI hints in the composer.
|
||||
let kkp = detect_kitty_protocol().unwrap_or(false);
|
||||
KKP_ENABLED.store(kkp, Ordering::Relaxed);
|
||||
|
||||
set_panic_hook();
|
||||
|
||||
let backend = CrosstermBackend::new(stdout());
|
||||
|
||||
Reference in New Issue
Block a user