Skip to content

Commit

Permalink
Revert use of alternate screen
Browse files Browse the repository at this point in the history
The alternate screen doesn't support scrolling by default. A more comprehensive alternate screen solution could support it, but adding something so complex immediately before the release is a bit risky.

Fixes #3613
  • Loading branch information
laurmaedje committed Mar 13, 2024
1 parent 48820fe commit 9552eb8
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 91 deletions.
22 changes: 0 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/typst-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ chrono = { workspace = true }
clap = { workspace = true }
codespan-reporting = { workspace = true }
comemo = { workspace = true }
ctrlc = { workspace = true }
dirs = { workspace = true }
ecow = { workspace = true }
env_proxy = { workspace = true }
Expand Down
9 changes: 1 addition & 8 deletions crates/typst-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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");
}
Expand Down
53 changes: 1 addition & 52 deletions crates/typst-cli/src/terminal.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 {
Expand All @@ -32,10 +28,7 @@ impl TermOutInner {
};

let stream = termcolor::StandardStream::stderr(color_choice);
TermOutInner {
stream,
in_alternate_screen: AtomicBool::new(false),
}
TermOutInner { stream }
}
}

Expand All @@ -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.
Expand All @@ -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 {
Expand Down
6 changes: 0 additions & 6 deletions crates/typst-cli/src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?;

Expand Down
2 changes: 0 additions & 2 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,6 @@ description: |
- Command line interface
- Added support for passing [inputs]($category/foundations/sys) via a CLI flag
- When passing the filename `-`, Typst will now read input from stdin
- The watch mode now uses the alternate screen so that the original state of
the terminal is restored when exiting
- Now uses the system-native TLS implementation for network fetching which
should be generally more robust
- Watch mode will now properly detect when a previously missing file is
Expand Down

0 comments on commit 9552eb8

Please sign in to comment.