From 59eb7fa29694c7dd63c71c6906885b7229be7354 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sat, 14 Dec 2024 12:32:03 +0100 Subject: refactor(yt/comments): Move the display code to a separate function --- yt/src/comments/mod.rs | 32 ++++---------------------------- yt/src/comments/output.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 28 deletions(-) create mode 100644 yt/src/comments/output.rs 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 . -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 { @@ -153,29 +151,7 @@ pub async fn get(app: &App) -> Result { 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 +// 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 . + +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(()) +} -- cgit 1.4.1