aboutsummaryrefslogtreecommitdiffstats
path: root/crates
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2026-05-26 18:19:19 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2026-05-26 18:19:19 +0200
commitb11ec67b71cd848092d874429e0d1c04c206bab1 (patch)
tree0c3763ff395a40953c059b5b3aa7eab549b2ce7a /crates
parentbuild(Cargo.toml): Update pins (diff)
downloadyt-b11ec67b71cd848092d874429e0d1c04c206bab1.zip
refactor(yt/ansi_escape_codes): Force specifications of stream
Otherwise, silent errors might sneak into the code, when the colon move is printed to stdout, but the delete code to stderr (for example).
Diffstat (limited to 'crates')
-rw-r--r--crates/yt/src/ansi_escape_codes.rs18
-rw-r--r--crates/yt/src/commands/playlist/implm.rs6
-rw-r--r--crates/yt/src/commands/select/implm/standalone/mod.rs4
-rw-r--r--crates/yt/src/commands/update/implm/updater.rs11
4 files changed, 22 insertions, 17 deletions
diff --git a/crates/yt/src/ansi_escape_codes.rs b/crates/yt/src/ansi_escape_codes.rs
index 28a8370..0348d89 100644
--- a/crates/yt/src/ansi_escape_codes.rs
+++ b/crates/yt/src/ansi_escape_codes.rs
@@ -8,22 +8,24 @@
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
+use std::io::Write;
+
// see: https://en.wikipedia.org/wiki/ANSI_escape_code#Control_Sequence_Introducer_commands
const CSI: &str = "\x1b[";
-pub(crate) fn erase_from_cursor_to_bottom() {
- print!("{CSI}0J");
+pub(crate) fn erase_from_cursor_to_bottom<W: Write>(mut stream: W) {
+ write!(stream, "{CSI}0J").expect("the stream to accept writes");
}
-pub(crate) fn cursor_up(number: usize) {
+pub(crate) fn cursor_up<W: Write>(mut stream: W, number: usize) {
// HACK(@bpeetz): The default is `1` and running this command with a
// number of `0` results in it using the default (i.e., `1`) <2025-03-25>
if number != 0 {
- print!("{CSI}{number}A");
+ write!(stream, "{CSI}{number}A").expect("the stream to accept writes");
}
}
-pub(crate) fn clear_whole_line() {
- eprint!("{CSI}2K");
+pub(crate) fn clear_whole_line<W: Write>(mut stream: W) {
+ write!(stream, "{CSI}2K").expect("the stream to accept writes");
}
-pub(crate) fn move_to_col(x: usize) {
- eprint!("{CSI}{x}G");
+pub(crate) fn move_to_col<W: Write>(mut stream: W, x: usize) {
+ write!(stream, "{CSI}{x}G").expect("the stream to accept writes");
}
diff --git a/crates/yt/src/commands/playlist/implm.rs b/crates/yt/src/commands/playlist/implm.rs
index 603184b..adb75da 100644
--- a/crates/yt/src/commands/playlist/implm.rs
+++ b/crates/yt/src/commands/playlist/implm.rs
@@ -8,7 +8,7 @@
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
-use std::{fmt::Write, path::Path};
+use std::{fmt::Write, io::stdout, path::Path};
use crate::{
ansi_escape_codes,
@@ -78,8 +78,8 @@ impl PlaylistCommand {
.await?;
// Delete the previous output
- ansi_escape_codes::cursor_up(previous_output_length);
- ansi_escape_codes::erase_from_cursor_to_bottom();
+ ansi_escape_codes::cursor_up(stdout(), previous_output_length);
+ ansi_escape_codes::erase_from_cursor_to_bottom(stdout());
previous_output_length = output.chars().filter(|ch| *ch == '\n').count();
diff --git a/crates/yt/src/commands/select/implm/standalone/mod.rs b/crates/yt/src/commands/select/implm/standalone/mod.rs
index 9512e32..dfabc2b 100644
--- a/crates/yt/src/commands/select/implm/standalone/mod.rs
+++ b/crates/yt/src/commands/select/implm/standalone/mod.rs
@@ -108,8 +108,8 @@ async fn handle_status_change(
}
if !is_single {
- ansi_escape_codes::clear_whole_line();
- ansi_escape_codes::move_to_col(1);
+ ansi_escape_codes::clear_whole_line(stderr());
+ ansi_escape_codes::move_to_col(stderr(), 1);
}
eprint!("{}", &video.to_line_display(app, None).await?);
diff --git a/crates/yt/src/commands/update/implm/updater.rs b/crates/yt/src/commands/update/implm/updater.rs
index 2b96bf2..d6fcbd0 100644
--- a/crates/yt/src/commands/update/implm/updater.rs
+++ b/crates/yt/src/commands/update/implm/updater.rs
@@ -8,7 +8,10 @@
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
-use std::sync::atomic::{AtomicUsize, Ordering};
+use std::{
+ io,
+ sync::atomic::{AtomicUsize, Ordering},
+};
use anyhow::{Context, Result};
use futures::{StreamExt, future::join_all, stream};
@@ -87,14 +90,14 @@ impl Updater {
.spawn_pinned(move || {
async move {
if !log_enabled!(Level::Debug) {
- ansi_escape_codes::clear_whole_line();
- ansi_escape_codes::move_to_col(1);
+ ansi_escape_codes::clear_whole_line(io::stderr());
+ ansi_escape_codes::move_to_col(io::stderr(), 1);
eprint!(
"({}/{total_number}) Checking playlist {}...",
REACHED_NUMBER.fetch_add(1, Ordering::Relaxed),
sub.name
);
- ansi_escape_codes::move_to_col(1);
+ ansi_escape_codes::move_to_col(io::stderr(), 1);
stderr().flush().await?;
}