diff options
-rw-r--r-- | yt/src/comments/mod.rs | 32 | ||||
-rw-r--r-- | yt/src/comments/output.rs | 46 |
2 files changed, 50 insertions, 28 deletions
diff --git a/yt/src/comments/mod.rs b/yt/src/comments/mod.rs index fd9f9da..afc90de 100644 --- a/yt/src/comments/mod.rs +++ b/yt/src/comments/mod.rs @@ -8,14 +8,11 @@ // 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, - mem, - process::{Command, Stdio}, -}; +use std::mem; use anyhow::{bail, Context, Result}; use comment::{CommentExt, Comments}; +use output::display_fmt_and_less; use regex::Regex; use yt_dlp::wrapper::info_json::{Comment, InfoJson, Parent}; @@ -30,6 +27,7 @@ use crate::{ mod comment; mod display; +pub mod output; #[allow(clippy::too_many_lines)] pub async fn get(app: &App) -> Result<Comments> { @@ -153,29 +151,7 @@ pub async fn get(app: &App) -> Result<Comments> { pub async fn comments(app: &App) -> Result<()> { let comments = get(app).await?; - let mut less = Command::new("less") - .args(["--raw-control-chars"]) - .stdin(Stdio::piped()) - .stderr(Stdio::inherit()) - .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")?; - std::thread::spawn(move || { - stdin - .write_all(comments.render(true).as_bytes()) - .unreachable("Should be able to write to the stdin of less"); - }); - - let _ = less.wait().context("Failed to await less")?; + display_fmt_and_less(comments.render(true)).await?; Ok(()) } diff --git a/yt/src/comments/output.rs b/yt/src/comments/output.rs new file mode 100644 index 0000000..626db2a --- /dev/null +++ b/yt/src/comments/output.rs @@ -0,0 +1,46 @@ +// yt - A fully featured command line YouTube client +// +// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de> +// SPDX-License-Identifier: GPL-3.0-or-later +// +// This file is part of Yt. +// +// 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, + process::{Command, Stdio}, +}; + +use anyhow::{Context, Result}; + +use crate::unreachable::Unreachable; + +pub async fn display_fmt_and_less(input: String) -> Result<()> { + let mut less = Command::new("less") + .args(["--raw-control-chars"]) + .stdin(Stdio::piped()) + .stderr(Stdio::inherit()) + .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")?; + std::thread::spawn(move || { + stdin + .write_all(input.as_bytes()) + .unreachable("Should be able to write to the stdin of less"); + }); + + let _ = less.wait().context("Failed to await less")?; + + Ok(()) +} |