-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support for synchronized output #195
Draft
Funami580
wants to merge
1
commit into
console-rs:main
Choose a base branch
from
Funami580:sync-output
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ use std::io; | |
use std::io::{BufRead, BufReader}; | ||
use std::mem; | ||
use std::os::unix::io::AsRawFd; | ||
use std::ptr; | ||
use std::os::unix::io::FromRawFd; | ||
use std::os::unix::io::IntoRawFd; | ||
use std::str; | ||
|
||
use crate::kb::Key; | ||
|
@@ -360,3 +361,289 @@ pub fn wants_emoji() -> bool { | |
pub fn set_title<T: Display>(title: T) { | ||
print!("\x1b]0;{}\x07", title); | ||
} | ||
|
||
fn with_raw_terminal<R>(f: impl FnOnce(&mut fs::File) -> R) -> io::Result<R> { | ||
// We need a custom drop implementation for File, | ||
// so that the fd for stdin does not get closed | ||
enum CustomDropFile { | ||
CloseFd(Option<fs::File>), | ||
NotCloseFd(Option<fs::File>), | ||
} | ||
|
||
impl Drop for CustomDropFile { | ||
fn drop(&mut self) { | ||
match self { | ||
CustomDropFile::CloseFd(_) => {} | ||
CustomDropFile::NotCloseFd(inner) => { | ||
if let Some(file) = inner.take() { | ||
file.into_raw_fd(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
let (mut tty_handle, tty_fd) = if unsafe { libc::isatty(libc::STDIN_FILENO) } == 1 { | ||
( | ||
CustomDropFile::NotCloseFd(Some(unsafe { fs::File::from_raw_fd(libc::STDIN_FILENO) })), | ||
libc::STDIN_FILENO, | ||
) | ||
} else { | ||
let handle = fs::OpenOptions::new() | ||
.read(true) | ||
.write(true) | ||
.open("/dev/tty")?; | ||
let fd = handle.as_raw_fd(); | ||
(CustomDropFile::CloseFd(Some(handle)), fd) | ||
}; | ||
|
||
// Get current mode | ||
let mut termios = mem::MaybeUninit::uninit(); | ||
c_result(|| unsafe { libc::tcgetattr(tty_fd, termios.as_mut_ptr()) })?; | ||
|
||
let mut termios = unsafe { termios.assume_init() }; | ||
let old_iflag = termios.c_iflag; | ||
let old_oflag = termios.c_oflag; | ||
let old_cflag = termios.c_cflag; | ||
let old_lflag = termios.c_lflag; | ||
|
||
// Go into raw mode | ||
unsafe { libc::cfmakeraw(&mut termios) }; | ||
if old_lflag & libc::ISIG != 0 { | ||
// Re-enable INTR, QUIT, SUSP, DSUSP, if it was activated before | ||
termios.c_lflag |= libc::ISIG; | ||
} | ||
c_result(|| unsafe { libc::tcsetattr(tty_fd, libc::TCSADRAIN, &termios) })?; | ||
|
||
let result = match &mut tty_handle { | ||
CustomDropFile::CloseFd(Some(handle)) => f(handle), | ||
CustomDropFile::NotCloseFd(Some(handle)) => f(handle), | ||
_ => unreachable!(), | ||
}; | ||
|
||
// Reset to previous mode | ||
termios.c_iflag = old_iflag; | ||
termios.c_oflag = old_oflag; | ||
termios.c_cflag = old_cflag; | ||
termios.c_lflag = old_lflag; | ||
c_result(|| unsafe { libc::tcsetattr(tty_fd, libc::TCSADRAIN, &termios) })?; | ||
|
||
Ok(result) | ||
} | ||
|
||
pub fn supports_synchronized_output() -> bool { | ||
*sync_output::SUPPORTS_SYNCHRONIZED_OUTPUT | ||
} | ||
|
||
/// Specification: https://gist.github.com/christianparpart/d8a62cc1ab659194337d73e399004036 | ||
mod sync_output { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think Rust style would suggest not abbreviating |
||
use std::convert::TryInto as _; | ||
use std::io::Read as _; | ||
use std::io::Write as _; | ||
use std::os::unix::io::AsRawFd as _; | ||
use std::time; | ||
|
||
use lazy_static::lazy_static; | ||
|
||
use super::select_or_poll_term_fd; | ||
use super::with_raw_terminal; | ||
|
||
const RESPONSE_TIMEOUT: time::Duration = time::Duration::from_millis(10); | ||
|
||
lazy_static! { | ||
pub(crate) static ref SUPPORTS_SYNCHRONIZED_OUTPUT: bool = | ||
supports_synchronized_output_uncached(); | ||
} | ||
|
||
struct ResponseParser { | ||
state: ResponseParserState, | ||
response: u8, | ||
} | ||
|
||
#[derive(PartialEq)] | ||
enum ResponseParserState { | ||
None, | ||
CsiOne, | ||
CsiTwo, | ||
QuestionMark, | ||
ModeDigit1, | ||
ModeDigit2, | ||
ModeDigit3, | ||
ModeDigit4, | ||
Semicolon, | ||
Response, | ||
DollarSign, | ||
Ypsilon, | ||
} | ||
|
||
impl ResponseParser { | ||
const fn new() -> Self { | ||
Self { | ||
state: ResponseParserState::None, | ||
response: u8::MAX, | ||
} | ||
} | ||
|
||
fn process_byte(&mut self, byte: u8) { | ||
match byte { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggest writing this as such: self.state = match (self.state, byte) {
..
}; |
||
b'\x1b' => { | ||
self.state = ResponseParserState::CsiOne; | ||
} | ||
b'[' => { | ||
self.state = if self.state == ResponseParserState::CsiOne { | ||
ResponseParserState::CsiTwo | ||
} else { | ||
ResponseParserState::None | ||
}; | ||
} | ||
b'?' => { | ||
self.state = if self.state == ResponseParserState::CsiTwo { | ||
ResponseParserState::QuestionMark | ||
} else { | ||
ResponseParserState::None | ||
}; | ||
} | ||
byte @ b'0' => { | ||
self.state = if self.state == ResponseParserState::Semicolon { | ||
self.response = byte; | ||
ResponseParserState::Response | ||
} else if self.state == ResponseParserState::ModeDigit1 { | ||
ResponseParserState::ModeDigit2 | ||
} else { | ||
ResponseParserState::None | ||
}; | ||
} | ||
byte @ b'2' => { | ||
self.state = if self.state == ResponseParserState::Semicolon { | ||
self.response = byte; | ||
ResponseParserState::Response | ||
} else if self.state == ResponseParserState::QuestionMark { | ||
ResponseParserState::ModeDigit1 | ||
} else if self.state == ResponseParserState::ModeDigit2 { | ||
ResponseParserState::ModeDigit3 | ||
} else { | ||
ResponseParserState::None | ||
}; | ||
} | ||
byte @ b'1' | byte @ b'3' | byte @ b'4' => { | ||
self.state = if self.state == ResponseParserState::Semicolon { | ||
self.response = byte; | ||
ResponseParserState::Response | ||
} else { | ||
ResponseParserState::None | ||
}; | ||
} | ||
b'6' => { | ||
self.state = if self.state == ResponseParserState::ModeDigit3 { | ||
ResponseParserState::ModeDigit4 | ||
} else { | ||
ResponseParserState::None | ||
}; | ||
} | ||
b';' => { | ||
self.state = if self.state == ResponseParserState::ModeDigit4 { | ||
ResponseParserState::Semicolon | ||
} else { | ||
ResponseParserState::None | ||
}; | ||
} | ||
b'$' => { | ||
self.state = if self.state == ResponseParserState::Response { | ||
ResponseParserState::DollarSign | ||
} else { | ||
ResponseParserState::None | ||
}; | ||
} | ||
b'y' => { | ||
self.state = if self.state == ResponseParserState::DollarSign { | ||
ResponseParserState::Ypsilon | ||
} else { | ||
ResponseParserState::None | ||
}; | ||
} | ||
_ => { | ||
self.state = ResponseParserState::None; | ||
} | ||
} | ||
} | ||
|
||
fn get_response(&self) -> Option<u8> { | ||
if self.state == ResponseParserState::Ypsilon { | ||
Some(self.response - b'0') | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
||
fn supports_synchronized_output_uncached() -> bool { | ||
with_raw_terminal(|term_handle| { | ||
// Query the state of the (DEC) mode 2026 (Synchronized Output) | ||
write!(term_handle, "\x1b[?2026$p").ok()?; | ||
term_handle.flush().ok()?; | ||
|
||
// Wait for response or timeout | ||
let term_fd = term_handle.as_raw_fd(); | ||
let mut parser = ResponseParser::new(); | ||
let mut buf = [0u8; 256]; | ||
let deadline = time::Instant::now() + RESPONSE_TIMEOUT; | ||
|
||
loop { | ||
let remaining_time = deadline | ||
.saturating_duration_since(time::Instant::now()) | ||
.as_millis() | ||
.try_into() | ||
.ok()?; | ||
|
||
if remaining_time == 0 { | ||
// Timeout | ||
return Some(false); | ||
} | ||
|
||
match select_or_poll_term_fd(term_fd, remaining_time) { | ||
Ok(false) => { | ||
// Timeout | ||
return Some(false); | ||
} | ||
Ok(true) => { | ||
'read: loop { | ||
match term_handle.read(&mut buf) { | ||
Ok(0) => { | ||
// Reached EOF | ||
return Some(false); | ||
} | ||
Ok(size) => { | ||
for byte in &buf[..size] { | ||
parser.process_byte(*byte); | ||
|
||
match parser.get_response() { | ||
Some(1) | Some(2) => return Some(true), | ||
Some(_) => return Some(false), | ||
None => {} | ||
} | ||
} | ||
|
||
break 'read; | ||
} | ||
Err(err) if err.kind() == std::io::ErrorKind::Interrupted => { | ||
// Got interrupted, retry read | ||
continue 'read; | ||
} | ||
Err(_) => { | ||
return Some(false); | ||
} | ||
} | ||
} | ||
} | ||
Err(_) => { | ||
// Error | ||
return Some(false); | ||
} | ||
} | ||
} | ||
}) | ||
.ok() | ||
.flatten() | ||
.unwrap_or(false) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is only used within
sync_output
, I would move it inside the module.