broot/verb/
write.rs

1use {
2    crate::{
3        app::*,
4        errors::ProgramError,
5    },
6    std::{
7        fs::{
8            File,
9            OpenOptions,
10        },
11        io::Write,
12    },
13};
14
15/// Intended to verbs, this function writes the passed string to the file
16/// provided to broot with `--verb-output`, creating a new line if the
17/// file is not empty.
18pub fn verb_write(
19    con: &AppContext,
20    line: &str,
21) -> Result<CmdResult, ProgramError> {
22    let Some(path) = &con.launch_args.verb_output else {
23        return Ok(CmdResult::error("No --verb-output provided".to_string()));
24    };
25    let mut file = OpenOptions::new().create(true).append(true).open(path)?;
26    if file.metadata().map(|m| m.len() > 0).unwrap_or(false) {
27        writeln!(file)?;
28    }
29    write!(file, "{}", line)?;
30    Ok(CmdResult::Keep)
31}
32
33/// Remove the content of the file provided to broot with `--verb-output`.
34pub fn verb_clear_output(con: &AppContext) -> Result<CmdResult, ProgramError> {
35    let Some(path) = &con.launch_args.verb_output else {
36        return Ok(CmdResult::error("No --verb-output provided".to_string()));
37    };
38    File::create(path)?;
39    Ok(CmdResult::Keep)
40}