Compare commits

...

2 Commits

Author SHA1 Message Date
Araozu 09c267139d CLI help command 2023-12-12 21:24:12 -05:00
Araozu 2583c35f21 Use colored for CLI output 2023-12-12 20:58:09 -05:00
6 changed files with 124 additions and 29 deletions

85
Cargo.lock generated
View File

@ -2,6 +2,91 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "colored"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8"
dependencies = [
"lazy_static",
"windows-sys",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "thp" name = "thp"
version = "0.0.9" version = "0.0.9"
dependencies = [
"colored",
]
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"

View File

@ -7,3 +7,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
colored = "2.1.0"

0
src/cli/empty.rs Normal file
View File

View File

@ -1,7 +1,10 @@
use crate::cli::HELP_TEXT; use crate::cli::get_help_text;
use colored::*;
pub fn help_command(options: &Vec<String>) { pub fn help_command(options: &Vec<String>) {
println!("{}", HELP_TEXT); println!("{}", get_help_text());
}
if options.len() > 0 {
println!("{}: {}", "warning".yellow(), "The help command doesn't take any options.");
}
}

View File

@ -1,30 +1,38 @@
mod help; mod help;
mod types; mod types;
mod empty;
use types::{Command, CommandType}; use types::{Command, CommandType};
pub const HELP_TEXT: &str = r#" use colored::*;
pub fn get_help_text() -> String {
format!(
r#"
Usage: `thp [command] [options]` Usage: `thp [command] [options]`
Commands Commands
c _file_ Compiles `file` in-place c {0} Compiles {0} in-place
f _file_ Formats `file` f {0} Formats {0}
r Starts the REPL r Starts the REPL
init Initializes a new project in the current directory init Initializes a new project in the current directory
build Builds the project build Builds the project
fmt Formats all files in the project fmt Formats all files in the project
watch, w Starts compilation of the project in watch mode watch, w Starts compilation of the project in watch mode
help, h Print this message & exit help, h Print this message & exit
General options General options
-h, --help Print command-specific usage -h, --help Print command-specific usage
"#; "#,
"_file_".green()
)
}
fn get_copyright() -> String { fn get_version() -> String {
let crate_version = env!("CARGO_PKG_VERSION"); let crate_version = env!("CARGO_PKG_VERSION");
format!("The THP compiler, linter & formatter, v{}", crate_version) format!("The THP compiler, linter & formatter, v{}", crate_version)
} }
@ -33,10 +41,10 @@ pub fn run_cli() {
let command = match parse_args() { let command = match parse_args() {
Ok(c) => c, Ok(c) => c,
Err(reason) => { Err(reason) => {
println!("{}", HELP_TEXT); println!("{}", get_help_text());
println!("Error: {}", reason); println!("{}: {}", "error".on_red(), reason);
return; return;
}, }
}; };
command.run(); command.run();
@ -58,10 +66,7 @@ fn parse_args() -> Result<Command, String> {
if arg.starts_with('-') { if arg.starts_with('-') {
options.push(arg); options.push(arg);
} else { } else {
return Err(format!( return Err(format!("Unexpected command `{}` after the options", arg));
"Unexpected command `{}` after the options",
arg
));
} }
} }
@ -75,12 +80,10 @@ fn parse_args() -> Result<Command, String> {
"fmt" => CommandType::Fmt, "fmt" => CommandType::Fmt,
"watch" | "w" => CommandType::Watch, "watch" | "w" => CommandType::Watch,
"help" | "h" => CommandType::Help, "help" | "h" => CommandType::Help,
_ => return Err(format!("Unknown command: {}", command)), _ => return Err(format!("Unknown command `{}`", command)),
}, },
None => CommandType::None, None => CommandType::None,
}; };
Ok(Command { command, options }) Ok(Command { command, options })
} }

View File

@ -17,16 +17,19 @@ pub enum CommandType {
None, None,
} }
impl Command { impl Command {
pub fn run(&self) { pub fn run(&self) {
println!("Running command! {:?}", self);
self.command.run(&self.options); self.command.run(&self.options);
} }
} }
impl CommandType { impl CommandType {
pub fn run(&self, options: &Vec<String>) { pub fn run(&self, options: &Vec<String>) {
println!("Running command! {:?}", self) match self {
CommandType::Help => super::help::help_command(options),
_ => {
println!("Not implemented yet! {:?} {:?}", self, options);
}
}
} }
} }