Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 28 additions & 34 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ struct TermInner {
prompt_guard: Mutex<()>,
}

impl TermInner {
fn new(target: TermTarget) -> Self {
Self::with_buffer(target, None)
}

fn new_buffered(target: TermTarget) -> Self {
Self::with_buffer(target, Some(vec![]))
}

fn with_buffer(target: TermTarget, buffer: Option<Vec<u8>>) -> Self {
Self {
target,
buffer: buffer.map(Mutex::new),
prompt: RwLock::new(String::new()),
prompt_guard: Mutex::new(()),
}
}
}

/// The family of the terminal.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum TermFamily {
Expand Down Expand Up @@ -153,43 +172,23 @@ impl Term {
/// Return a new unbuffered terminal.
#[inline]
pub fn stdout() -> Term {
Term::with_inner(TermInner {
target: TermTarget::Stdout,
buffer: None,
prompt: RwLock::new(String::new()),
prompt_guard: Mutex::new(()),
})
Term::with_inner(TermInner::new(TermTarget::Stdout))
}

/// Return a new unbuffered terminal to stderr.
#[inline]
pub fn stderr() -> Term {
Term::with_inner(TermInner {
target: TermTarget::Stderr,
buffer: None,
prompt: RwLock::new(String::new()),
prompt_guard: Mutex::new(()),
})
Term::with_inner(TermInner::new(TermTarget::Stderr))
}

/// Return a new buffered terminal.
pub fn buffered_stdout() -> Term {
Term::with_inner(TermInner {
target: TermTarget::Stdout,
buffer: Some(Mutex::new(vec![])),
prompt: RwLock::new(String::new()),
prompt_guard: Mutex::new(()),
})
Term::with_inner(TermInner::new_buffered(TermTarget::Stdout))
}

/// Return a new buffered terminal to stderr.
pub fn buffered_stderr() -> Term {
Term::with_inner(TermInner {
target: TermTarget::Stderr,
buffer: Some(Mutex::new(vec![])),
prompt: RwLock::new(String::new()),
prompt_guard: Mutex::new(()),
})
Term::with_inner(TermInner::new_buffered(TermTarget::Stderr))
}

/// Return a terminal for the given Read/Write pair styled like stderr.
Expand All @@ -209,16 +208,11 @@ impl Term {
R: Read + Debug + AsRawFd + Send + 'static,
W: Write + Debug + AsRawFd + Send + 'static,
{
Term::with_inner(TermInner {
target: TermTarget::ReadWritePair(ReadWritePair {
read: Arc::new(Mutex::new(read)),
write: Arc::new(Mutex::new(write)),
style,
}),
buffer: None,
prompt: RwLock::new(String::new()),
prompt_guard: Mutex::new(()),
})
Term::with_inner(TermInner::new(TermTarget::ReadWritePair(ReadWritePair {
read: Arc::new(Mutex::new(read)),
write: Arc::new(Mutex::new(write)),
style,
})))
}

/// Return the style for this terminal.
Expand Down
27 changes: 11 additions & 16 deletions src/windows_term/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ pub(crate) use self::colors::*;

pub(crate) const DEFAULT_WIDTH: u16 = 79;

pub(crate) fn as_handle(term: &Term) -> HANDLE {
// convert between windows_sys::Win32::Foundation::HANDLE and std::os::windows::raw::HANDLE
term.as_raw_handle() as HANDLE
}

pub(crate) fn is_a_terminal(out: &Term) -> bool {
let (fd, others) = match out.target() {
TermTarget::Stdout => (STD_OUTPUT_HANDLE, [STD_INPUT_HANDLE, STD_ERROR_HANDLE]),
Expand Down Expand Up @@ -196,7 +191,7 @@ pub(crate) fn move_cursor_to(out: &Term, x: usize, y: usize) -> io::Result<()> {
if out.is_msys_tty {
return common_term::move_cursor_to(out, x, y);
}
if let Some((hand, _)) = get_console_screen_buffer_info(as_handle(out)) {
if let Some((hand, _)) = get_console_screen_buffer_info(out.as_raw_handle()) {
unsafe {
SetConsoleCursorPosition(
hand,
Expand All @@ -215,7 +210,7 @@ pub(crate) fn move_cursor_up(out: &Term, n: usize) -> io::Result<()> {
return common_term::move_cursor_up(out, n);
}

if let Some((_, csbi)) = get_console_screen_buffer_info(as_handle(out)) {
if let Some((_, csbi)) = get_console_screen_buffer_info(out.as_raw_handle()) {
move_cursor_to(out, 0, csbi.dwCursorPosition.Y as usize - n)?;
}
Ok(())
Expand All @@ -226,7 +221,7 @@ pub(crate) fn move_cursor_down(out: &Term, n: usize) -> io::Result<()> {
return common_term::move_cursor_down(out, n);
}

if let Some((_, csbi)) = get_console_screen_buffer_info(as_handle(out)) {
if let Some((_, csbi)) = get_console_screen_buffer_info(out.as_raw_handle()) {
move_cursor_to(out, 0, csbi.dwCursorPosition.Y as usize + n)?;
}
Ok(())
Expand All @@ -237,7 +232,7 @@ pub(crate) fn move_cursor_left(out: &Term, n: usize) -> io::Result<()> {
return common_term::move_cursor_left(out, n);
}

if let Some((_, csbi)) = get_console_screen_buffer_info(as_handle(out)) {
if let Some((_, csbi)) = get_console_screen_buffer_info(out.as_raw_handle()) {
move_cursor_to(
out,
csbi.dwCursorPosition.X as usize - n,
Expand All @@ -252,7 +247,7 @@ pub(crate) fn move_cursor_right(out: &Term, n: usize) -> io::Result<()> {
return common_term::move_cursor_right(out, n);
}

if let Some((_, csbi)) = get_console_screen_buffer_info(as_handle(out)) {
if let Some((_, csbi)) = get_console_screen_buffer_info(out.as_raw_handle()) {
move_cursor_to(
out,
csbi.dwCursorPosition.X as usize + n,
Expand All @@ -266,7 +261,7 @@ pub(crate) fn clear_line(out: &Term) -> io::Result<()> {
if out.is_msys_tty {
return common_term::clear_line(out);
}
if let Some((hand, csbi)) = get_console_screen_buffer_info(as_handle(out)) {
if let Some((hand, csbi)) = get_console_screen_buffer_info(out.as_raw_handle()) {
unsafe {
let width = csbi.srWindow.Right - csbi.srWindow.Left;
let pos = COORD {
Expand All @@ -286,7 +281,7 @@ pub(crate) fn clear_chars(out: &Term, n: usize) -> io::Result<()> {
if out.is_msys_tty {
return common_term::clear_chars(out, n);
}
if let Some((hand, csbi)) = get_console_screen_buffer_info(as_handle(out)) {
if let Some((hand, csbi)) = get_console_screen_buffer_info(out.as_raw_handle()) {
unsafe {
let width = cmp::min(csbi.dwCursorPosition.X, n as i16);
let pos = COORD {
Expand All @@ -306,7 +301,7 @@ pub(crate) fn clear_screen(out: &Term) -> io::Result<()> {
if out.is_msys_tty {
return common_term::clear_screen(out);
}
if let Some((hand, csbi)) = get_console_screen_buffer_info(as_handle(out)) {
if let Some((hand, csbi)) = get_console_screen_buffer_info(out.as_raw_handle()) {
unsafe {
let cells = csbi.dwSize.X as u32 * csbi.dwSize.Y as u32; // as u32, or else this causes stack overflows.
let pos = COORD { X: 0, Y: 0 };
Expand All @@ -323,7 +318,7 @@ pub(crate) fn clear_to_end_of_screen(out: &Term) -> io::Result<()> {
if out.is_msys_tty {
return common_term::clear_to_end_of_screen(out);
}
if let Some((hand, csbi)) = get_console_screen_buffer_info(as_handle(out)) {
if let Some((hand, csbi)) = get_console_screen_buffer_info(out.as_raw_handle()) {
unsafe {
let bottom = csbi.srWindow.Right as u32 * csbi.srWindow.Bottom as u32;
let cells = bottom - (csbi.dwCursorPosition.X as u32 * csbi.dwCursorPosition.Y as u32); // as u32, or else this causes stack overflows.
Expand All @@ -344,7 +339,7 @@ pub(crate) fn show_cursor(out: &Term) -> io::Result<()> {
if out.is_msys_tty {
return common_term::show_cursor(out);
}
if let Some((hand, mut cci)) = get_console_cursor_info(as_handle(out)) {
if let Some((hand, mut cci)) = get_console_cursor_info(out.as_raw_handle()) {
unsafe {
cci.bVisible = 1;
SetConsoleCursorInfo(hand, &cci);
Expand All @@ -357,7 +352,7 @@ pub(crate) fn hide_cursor(out: &Term) -> io::Result<()> {
if out.is_msys_tty {
return common_term::hide_cursor(out);
}
if let Some((hand, mut cci)) = get_console_cursor_info(as_handle(out)) {
if let Some((hand, mut cci)) = get_console_cursor_info(out.as_raw_handle()) {
unsafe {
cci.bVisible = 0;
SetConsoleCursorInfo(hand, &cci);
Expand Down
Loading