Skip to main content

warp_directory/
lib.rs

1use clap::{Command, arg, builder::styling};
2
3use crate::commands::{
4    add_warp_point, delete_warp_point, edit_warp_point, list_warp_points, warp_to_point,
5};
6use crate::shell_wrapper::print_init;
7
8mod commands;
9mod shell_wrapper;
10mod storage;
11mod util;
12
13/// Set up the command line interface clap styles
14fn build_clap_styles() -> styling::Styles {
15    styling::Styles::styled()
16        .header(styling::AnsiColor::Green.on_default() | styling::Effects::BOLD)
17        .usage(styling::AnsiColor::Green.on_default() | styling::Effects::BOLD)
18        .literal(styling::AnsiColor::Blue.on_default() | styling::Effects::BOLD)
19        .placeholder(styling::AnsiColor::Cyan.on_default())
20}
21
22/// Construct the CLI command using clap
23fn construct_command() -> Command {
24    Command::new("warp-directory")
25        .version("0.1.0")
26        .author("Simon Barth <data5tream@proton.me>")
27        .about("Set and warp to directories")
28        .arg(arg!([warp_point] "The warp point to warp to"))
29        .subcommand(
30            Command::new("add")
31                .short_flag('a')
32                .about("Add a directory warp point")
33                .arg(arg!([name] "Name of the warp point").required(true))
34                .arg(arg!([path] "Target path of the warp point").required(false))
35                .arg(arg!(-f --force "Force overwrite an existing warp point").required(false))
36                .arg(
37                    arg!(--description <description> "Description of the warp point")
38                        .required(false),
39                ),
40        )
41        .subcommand(
42            Command::new("add-directory")
43                .short_flag('A')
44                .alias("add-dir")
45                .about("Add all subdirectories of the given directory")
46                .long_about("Add a directory warp point. All direct subdirectories will be added as warp points.")
47                .arg(arg!([path] "Target path of the warp point").required(false))
48                .arg(arg!(-f --force "Force overwrite an existing warp point").required(false))
49                .arg(
50                    arg!(--description <description> "Description of the warp point")
51                        .required(false),
52                )
53                .arg(
54                    arg!(--"strip-prefix" <prefix> "Prefix to strip from the directory names")
55                        .required(false),
56                ),
57        )
58        .subcommand(
59            Command::new("list")
60                .short_flag('l')
61                .about("List all directory warp points"),
62        )
63        .subcommand(
64            Command::new("delete")
65                .short_flag('d')
66                .about("Delete a directory warp point")
67                .arg(arg!([name] "Name of the warp point").required(true)),
68        )
69        .subcommand(
70            Command::new("edit")
71                .short_flag('e')
72                .about("Edit a warp point")
73                .arg(arg!([name] "Name of the warp point").required(true))
74                .arg(arg!(-p --path [path] "Updated target path").required(false))
75                .arg(arg!(-d --description [description] "Updated description").required(false))
76        )
77        .subcommand(
78            Command::new("init")
79                .short_flag('i')
80                .hide(true)
81                .about("Initialize the warp directory")
82                .arg(arg!([shell] "Name of shell").required(true)),
83        )
84        .subcommand(
85            Command::new("warp-point-file")
86                .about("Print the warp point file path")
87                .hide(true)
88        )
89        .arg_required_else_help(false)
90        .styles(build_clap_styles())
91}
92
93/// Main function to run the CLI application
94///
95/// Currently, supports the following subcommands:
96/// - `add`: Add a new warp point
97/// - `add-directory`: Add a directory warp point
98/// - `list`: List all warp points
99/// - `delete`: Delete a warp point
100/// - `init`: Prints the shell function to get the directory changing to work (hidden)
101///
102/// Subcommands also have short flags for easier access, they can be consulted using the help flag.
103pub fn app() {
104    let matches = construct_command().get_matches();
105
106    match matches.subcommand() {
107        None => {
108            if matches.args_present() {
109                if let Some(warp_point) = matches.get_one::<String>("warp_point") {
110                    warp_to_point(warp_point);
111                }
112            } else {
113                list_warp_points();
114            }
115        }
116        Some(("add", submatches)) => add_warp_point(submatches),
117        Some(("add-directory", submatches)) => {
118            commands::add_warp_directory(submatches);
119        }
120        Some(("list", _)) => list_warp_points(),
121        Some(("delete", submatches)) => delete_warp_point(submatches),
122        Some(("edit", submatches)) => edit_warp_point(submatches),
123        Some(("init", submatches)) => print_init(submatches),
124        Some(("warp-point-file", _)) => {
125            let path = storage::get_storage_file();
126            println!("{}", path.display());
127        }
128        _ => {}
129    }
130}