diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-02-16 18:15:49 +0100 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-02-16 18:16:27 +0100 |
commit | 6da5602d083bf26312071e68bfb1eb130da98934 (patch) | |
tree | 5eca723d6e31a45b318153459f284e5d5d4f0a4a | |
parent | build(rustfmt.toml): Add (diff) | |
download | yt-6da5602d083bf26312071e68bfb1eb130da98934.zip |
refactor(yt/): Use the new `termsize` and `uu_fmt` crates
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | yt/Cargo.toml | 2 | ||||
-rw-r--r-- | yt/src/comments/output.rs | 24 | ||||
-rw-r--r-- | yt/src/videos/display/format_video.rs | 9 |
4 files changed, 24 insertions, 12 deletions
diff --git a/Cargo.lock b/Cargo.lock index 0d0d1b6..06a7253 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2698,6 +2698,7 @@ dependencies = [ "sqlx", "stderrlog", "tempfile", + "termsize", "tokio", "toml", "trinitry", diff --git a/yt/Cargo.toml b/yt/Cargo.toml index f02f393..ae3d229 100644 --- a/yt/Cargo.toml +++ b/yt/Cargo.toml @@ -46,6 +46,8 @@ serde_json.workspace = true tokio.workspace = true url.workspace = true yt_dlp.workspace = true +termsize.workspace = true +uu_fmt.workspace = true [[bin]] name = "yt" diff --git a/yt/src/comments/output.rs b/yt/src/comments/output.rs index 626db2a..c279e9e 100644 --- a/yt/src/comments/output.rs +++ b/yt/src/comments/output.rs @@ -14,6 +14,7 @@ use std::{ }; use anyhow::{Context, Result}; +use uu_fmt::{process_text, FmtOptions}; use crate::unreachable::Unreachable; @@ -25,15 +26,8 @@ pub async fn display_fmt_and_less(input: String) -> Result<()> { .spawn() .context("Failed to run less")?; - let mut child = Command::new("fmt") - .args(["--uniform-spacing", "--split-only", "--width=90"]) - .stdin(Stdio::piped()) - .stderr(Stdio::inherit()) - .stdout(less.stdin.take().unreachable("Should be open")) - .spawn() - .context("Failed to run fmt")?; - - let mut stdin = child.stdin.take().context("Failed to open stdin")?; + let input = format_text(&input); + let mut stdin = less.stdin.take().context("Failed to open stdin")?; std::thread::spawn(move || { stdin .write_all(input.as_bytes()) @@ -44,3 +38,15 @@ pub async fn display_fmt_and_less(input: String) -> Result<()> { Ok(()) } + +#[must_use] +pub fn format_text(input: &str) -> String { + let width = termsize::get().map_or(90, |size| size.cols); + let fmt_opts = FmtOptions { + uniform: true, + split_only: true, + ..FmtOptions::new(Some(width as usize), None, Some(4)) + }; + + process_text(input, &fmt_opts) +} diff --git a/yt/src/videos/display/format_video.rs b/yt/src/videos/display/format_video.rs index 18c2e15..5c0320a 100644 --- a/yt/src/videos/display/format_video.rs +++ b/yt/src/videos/display/format_video.rs @@ -10,6 +10,8 @@ use std::fmt::Display; +use crate::comments::output::format_text; + pub trait FormatVideo { type Output; @@ -30,7 +32,7 @@ pub trait FormatVideo { #[allow(clippy::type_complexity)] fn to_parts( - &self, + &self ) -> ( Self::Output, Self::Output, @@ -122,8 +124,9 @@ pub trait FormatVideo { | -> {thumbnail_url} | -> {url} | -> options: {} -{description}\n", - video_options.to_string().trim() +{}\n", + video_options.to_string().trim(), + format_text(description.to_string().as_str()) ); string } |