diff options
| author | Laurenz <laurmaedje@gmail.com> | 2024-03-14 10:20:31 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-14 09:20:31 +0000 |
| commit | 9e507cd9fd816f960a7f7ab4fe7472dc0f1aa143 (patch) | |
| tree | 6d6b8e830096e791e10113f9ddce550416a4703b /crates/typst-cli/src | |
| parent | c2ca3615d72ab6e7f2c29018f4a1e525b898e11d (diff) | |
Revert use of alternate screen (#3648)
Diffstat (limited to 'crates/typst-cli/src')
| -rw-r--r-- | crates/typst-cli/src/main.rs | 9 | ||||
| -rw-r--r-- | crates/typst-cli/src/terminal.rs | 53 | ||||
| -rw-r--r-- | crates/typst-cli/src/watch.rs | 6 |
3 files changed, 2 insertions, 66 deletions
diff --git a/crates/typst-cli/src/main.rs b/crates/typst-cli/src/main.rs index 3f5ca2aa..da0a57fd 100644 --- a/crates/typst-cli/src/main.rs +++ b/crates/typst-cli/src/main.rs @@ -19,7 +19,6 @@ use std::process::ExitCode; use clap::Parser; use codespan_reporting::term; use codespan_reporting::term::termcolor::WriteColor; -use ecow::eco_format; use once_cell::sync::Lazy; use crate::args::{CliArguments, Command}; @@ -46,13 +45,7 @@ fn main() -> ExitCode { Command::Update(command) => crate::update::update(command), }; - // Leave the alternate screen if it was opened. This operation is done here - // so that it is executed prior to printing the final error. - let res_leave = terminal::out() - .leave_alternate_screen() - .map_err(|err| eco_format!("failed to leave alternate screen ({err})")); - - if let Some(msg) = res.err().or(res_leave.err()) { + if let Err(msg) = res { set_failed(); print_error(&msg).expect("failed to print error"); } diff --git a/crates/typst-cli/src/terminal.rs b/crates/typst-cli/src/terminal.rs index da0eb9d7..cf2906eb 100644 --- a/crates/typst-cli/src/terminal.rs +++ b/crates/typst-cli/src/terminal.rs @@ -1,11 +1,8 @@ use std::io::{self, IsTerminal, Write}; -use std::sync::atomic::{AtomicBool, Ordering}; use codespan_reporting::term::termcolor; -use ecow::eco_format; use once_cell::sync::Lazy; use termcolor::{ColorChoice, WriteColor}; -use typst::diag::StrResult; use crate::ARGS; @@ -18,7 +15,6 @@ pub fn out() -> TermOut { /// The stuff that has to be shared between instances of [`TermOut`]. struct TermOutInner { stream: termcolor::StandardStream, - in_alternate_screen: AtomicBool, } impl TermOutInner { @@ -32,10 +28,7 @@ impl TermOutInner { }; let stream = termcolor::StandardStream::stderr(color_choice); - TermOutInner { - stream, - in_alternate_screen: AtomicBool::new(false), - } + TermOutInner { stream } } } @@ -48,24 +41,6 @@ pub struct TermOut { } impl TermOut { - /// Initialize a handler that listens for Ctrl-C signals. - /// This is used to exit the alternate screen that might have been opened. - pub fn init_exit_handler(&mut self) -> StrResult<()> { - // We can safely ignore the error as the only thing this handler would do - // is leave an alternate screen if none was opened; not very important. - let mut term_out = self.clone(); - ctrlc::set_handler(move || { - let _ = term_out.leave_alternate_screen(); - - // Exit with the exit code standard for Ctrl-C exits[^1]. - // There doesn't seem to be another standard exit code for Windows, - // so we just use the same one there. - // [^1]: https://tldp.org/LDP/abs/html/exitcodes.html - std::process::exit(128 + 2); - }) - .map_err(|err| eco_format!("failed to initialize exit handler ({err})")) - } - /// Clears the entire screen. pub fn clear_screen(&mut self) -> io::Result<()> { // We don't want to clear anything that is not a TTY. @@ -90,32 +65,6 @@ impl TermOut { } Ok(()) } - - /// Enters the alternate screen if none was opened already. - pub fn enter_alternate_screen(&mut self) -> io::Result<()> { - if self.inner.stream.supports_color() - && !self.inner.in_alternate_screen.load(Ordering::Acquire) - { - let mut stream = self.inner.stream.lock(); - write!(stream, "\x1B[?1049h")?; - stream.flush()?; - self.inner.in_alternate_screen.store(true, Ordering::Release); - } - Ok(()) - } - - /// Leaves the alternate screen if it is already open. - pub fn leave_alternate_screen(&mut self) -> io::Result<()> { - if self.inner.stream.supports_color() - && self.inner.in_alternate_screen.load(Ordering::Acquire) - { - let mut stream = self.inner.stream.lock(); - write!(stream, "\x1B[?1049l")?; - stream.flush()?; - self.inner.in_alternate_screen.store(false, Ordering::Release); - } - Ok(()) - } } impl Write for TermOut { diff --git a/crates/typst-cli/src/watch.rs b/crates/typst-cli/src/watch.rs index 14ab2efd..28edd434 100644 --- a/crates/typst-cli/src/watch.rs +++ b/crates/typst-cli/src/watch.rs @@ -20,12 +20,6 @@ use crate::{print_error, terminal}; /// Execute a watching compilation command. pub fn watch(mut timer: Timer, mut command: CompileCommand) -> StrResult<()> { - // Enter the alternate screen and handle Ctrl-C ourselves. - terminal::out().init_exit_handler()?; - terminal::out() - .enter_alternate_screen() - .map_err(|err| eco_format!("failed to enter alternate screen ({err})"))?; - // Create a file system watcher. let mut watcher = Watcher::new(command.output())?; |
