wiki_o/cli/
cmd.rs

1use std::io::{stdin, BufRead, IsTerminal};
2
3use anyhow::Result;
4use clap::{arg, Parser, Subcommand};
5
6#[derive(Parser)]
7#[command(name = "wiki-o", author, version, about, arg_required_else_help = true)]
8/// Create a smart wiki from command line
9pub struct Cli {
10    #[command(subcommand)]
11    pub command: Option<Commands>,
12}
13
14#[derive(Subcommand)]
15pub enum Commands {
16    /// Add note
17    Add {
18        /// The note to write
19        #[arg(value_name = "NOTE")]
20        note: String,
21        /// file name to add
22        #[arg(short, long)]
23        file: Option<String>,
24    },
25    /// Show files with similar name
26    Show {
27        /// file name to show
28        #[arg(short, long)]
29        file: String,
30
31        /// True if showing with file name
32        #[arg(short, long)]
33        complete: Option<bool>,
34    },
35    /// List all notes
36    List {
37        /// list only file names
38        #[arg(short, long)]
39        short: Option<bool>,
40    },
41    /// Delete a note
42    Delete {
43        /// file to delete
44        #[arg(short, long)]
45        file: String,
46    },
47    /// Purge all notes
48    Purge {},
49    /// Piped add note
50    Pa {
51        /// file name to add
52        #[arg(short, long)]
53        file: Option<String>,
54    },
55    /// Show wiki-o configuration
56    Config {},
57}
58
59pub fn pipe_command() -> Result<String> {
60    let mut input = String::new();
61    loop {
62        let mut buffer = String::new();
63        if stdin().is_terminal() {
64            break;
65        }
66        match stdin().lock().read_line(&mut buffer) {
67            Ok(len) => {
68                if len == 0 {
69                    break;
70                } else {
71                    input.push_str(&buffer);
72                }
73            }
74            Err(_) => {
75                break;
76            }
77        }
78    }
79    Ok(input)
80}
81
82// #[cfg(test)]
83// mod tests {
84
85//     use super::cli;
86
87//     #[test]
88//     fn test_config_command() {
89//         let cli = cli();
90
91//         let m: clap::ArgMatches = cli.try_get_matches_from(["wiki-o", "add", "new"]).unwrap();
92
93//         let (_, add_cmd) = m.subcommand().unwrap();
94
95//         assert_eq!(add_cmd.get_one::<String>("NOTE").unwrap(), "new");
96//     }
97// }