Revert use of alternate screen (#3648)

This commit is contained in:
Laurenz 2024-03-14 10:20:31 +01:00 committed by GitHub
parent c2ca3615d7
commit 9e507cd9fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 2 additions and 91 deletions

22
Cargo.lock generated
View File

@ -501,16 +501,6 @@ dependencies = [
"memchr",
]
[[package]]
name = "ctrlc"
version = "3.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b467862cc8610ca6fc9a1532d7777cee0804e678ab45410897b9396495994a0b"
dependencies = [
"nix",
"windows-sys 0.52.0",
]
[[package]]
name = "data-url"
version = "0.3.1"
@ -1406,17 +1396,6 @@ dependencies = [
"tempfile",
]
[[package]]
name = "nix"
version = "0.27.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053"
dependencies = [
"bitflags 2.4.2",
"cfg-if",
"libc",
]
[[package]]
name = "notify"
version = "6.1.1"
@ -2605,7 +2584,6 @@ dependencies = [
"clap_mangen",
"codespan-reporting",
"comemo",
"ctrlc",
"dirs",
"ecow",
"env_proxy",

View File

@ -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 }

View File

@ -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");
}

View File

@ -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 {

View File

@ -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())?;

View File

@ -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