Skip to content

Commit

Permalink
Flush I/O after writing to it
Browse files Browse the repository at this point in the history
May help in some cases where line buffering is preventing the
application of the clearing sequences. See #1.
  • Loading branch information
passcod committed May 22, 2021
1 parent 4e92381 commit 4c28490
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

## Next (YYYY-MM-DD)

- Fix [#1](https://github.com/watchexec/clearscreen/issues/1): need to flush after writing sequences.

## v1.0.3 (2021-05-08)

- Drop unused `log` dependency.
Expand Down
17 changes: 15 additions & 2 deletions src/lib.rs
Expand Up @@ -441,6 +441,7 @@ impl ClearScreen {

if let Some(seq) = info.get::<capability::ClearScreen>() {
seq.expand().with(&mut ctx).to(&mut w)?;
w.flush()?;
} else {
return Err(Error::TerminfoCap("clear"));
}
Expand All @@ -452,15 +453,17 @@ impl ClearScreen {
Self::TerminfoScreen => {
let info = Database::from_env()?;
if let Some(seq) = info.get::<capability::ClearScreen>() {
seq.expand().to(w)?;
seq.expand().to(&mut w)?;
w.flush()?;
} else {
return Err(Error::TerminfoCap("clear"));
}
}
Self::TerminfoScrollback => {
let info = Database::from_env()?;
if let Some(seq) = info.get::<ResetScrollback>() {
seq.expand().to(w)?;
seq.expand().to(&mut w)?;
w.flush()?;
} else {
return Err(Error::TerminfoCap("E3"));
}
Expand All @@ -487,6 +490,8 @@ impl ClearScreen {
seq.expand().with(&mut ctx).to(&mut w)?;
}

w.flush()?;

if reset {
return Ok(());
}
Expand All @@ -508,6 +513,8 @@ impl ClearScreen {
seq.expand().with(&mut ctx).to(&mut w)?;
}

w.flush()?;

if !reset {
return Err(Error::TerminfoCap("reset"));
}
Expand All @@ -525,6 +532,8 @@ impl ClearScreen {

w.write_all(CSI)?;
w.write_all(ERASE_SCROLLBACK)?;

w.flush()?;
}
Self::XtermReset => {
const STR: &[u8] = b"!p";
Expand All @@ -550,6 +559,8 @@ impl ClearScreen {

w.write_all(CSI)?;
w.write_all(RESET_MARGINS)?;

w.flush()?;
}
Self::TputClear => {
let status = Command::new("tput").arg("clear").status()?;
Expand Down Expand Up @@ -583,11 +594,13 @@ impl ClearScreen {
Self::VtRis => {
w.write_all(ESC)?;
w.write_all(RIS)?;
w.flush()?;
}
Self::VtLeaveAlt => {
const LEAVE_ALT: &[u8] = b"?1049l";
w.write_all(CSI)?;
w.write_all(LEAVE_ALT)?;
w.flush()?;
}
Self::VtCooked => unix::vt_cooked()?,
Self::VtWellDone => unix::vt_well_done()?,
Expand Down

0 comments on commit 4c28490

Please sign in to comment.