mirror of
https://codeberg.org/icewind/haze.git
synced 2026-06-03 17:14:08 +02:00
command help
This commit is contained in:
parent
e189b2e4f2
commit
f928547ac2
6 changed files with 350 additions and 106 deletions
197
src/help.rs
197
src/help.rs
|
|
@ -1,44 +1,173 @@
|
|||
use crate::args::HazeCommand;
|
||||
use crate::database::DatabaseFamily;
|
||||
use crate::php::PhpVersion;
|
||||
use crate::service::ServiceType;
|
||||
use owo_colors::colors::xterm::Gray;
|
||||
use owo_colors::OwoColorize;
|
||||
use strum::{EnumMessage, IntoEnumIterator};
|
||||
use strum::{EnumMessage, EnumProperty, IntoEnumIterator};
|
||||
|
||||
pub fn help() {
|
||||
println!(
|
||||
"{} {} {}",
|
||||
"Usage:".bright_yellow().bold(),
|
||||
"haze".blue(),
|
||||
"[filter] <COMMAND> [arguments]".green()
|
||||
);
|
||||
println!();
|
||||
println!("{}", "Commands:".yellow().bold());
|
||||
let max_command_len = HazeCommand::iter()
|
||||
.map(|command| <&'static str>::from(command).len())
|
||||
.max()
|
||||
.unwrap();
|
||||
let max_doc_len = HazeCommand::iter()
|
||||
.map(|command| command.get_documentation().unwrap_or_default().len())
|
||||
.max()
|
||||
.unwrap();
|
||||
|
||||
for command in HazeCommand::iter() {
|
||||
let command: HazeCommand = command;
|
||||
let command_str = <&'static str>::from(command);
|
||||
let mut len = command_str.len();
|
||||
if command_str.starts_with("--") {
|
||||
len -= 2;
|
||||
}
|
||||
println!(
|
||||
" {}{} {}{} {}",
|
||||
command.blue(),
|
||||
" ".repeat(max_command_len - len),
|
||||
command.get_documentation().unwrap_or_default(),
|
||||
" ".repeat(max_doc_len - command.get_documentation().unwrap_or_default().len()),
|
||||
pub fn help(command: Option<HazeCommand>) {
|
||||
if let Some(command) = command {
|
||||
println!("{}", command.get_documentation().unwrap_or_default());
|
||||
println!();
|
||||
print!(
|
||||
"{} {}{} {}",
|
||||
"Usage:".bright_yellow().bold(),
|
||||
"haze".blue(),
|
||||
if command.allows_filter() {
|
||||
"- supports filter".fg::<Gray>()
|
||||
" [filter]".green()
|
||||
} else {
|
||||
"".fg::<Gray>()
|
||||
"".green()
|
||||
},
|
||||
command.blue(),
|
||||
);
|
||||
|
||||
let instance_args = command.get_bool("InstanceArgs").unwrap_or_default();
|
||||
if instance_args {
|
||||
print!(" {}", "[php version]".green());
|
||||
print!(" {}", "[database type]".green());
|
||||
print!(" {}", "[services]".green());
|
||||
}
|
||||
|
||||
let args = if let Some(args) = command.get_str("Args") {
|
||||
let args: &str = args;
|
||||
print!(" {}", "[arguments]".green());
|
||||
args.strip_prefix("[")
|
||||
.unwrap_or(args)
|
||||
.split(" [")
|
||||
.filter_map(|arg| arg.split_once("] "))
|
||||
.collect::<Vec<_>>()
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
println!();
|
||||
|
||||
println!();
|
||||
if instance_args {
|
||||
println!("{}", "Php versions:".yellow().bold());
|
||||
for php in PhpVersion::supported_versions() {
|
||||
println!(" {}", php.blue());
|
||||
}
|
||||
println!();
|
||||
|
||||
println!("{}", "Database types:".yellow().bold());
|
||||
let max_db_len = DatabaseFamily::iter()
|
||||
.map(|service| <&'static str>::from(service).len())
|
||||
.max()
|
||||
.unwrap_or_default();
|
||||
|
||||
for db in DatabaseFamily::iter() {
|
||||
let db: DatabaseFamily = db;
|
||||
let db_str: &'static str = db.into();
|
||||
let versions = match db.get_str("Versions") {
|
||||
Some(versions) => {
|
||||
let versions: Vec<_> = versions
|
||||
.split(' ')
|
||||
.map(|version| format!("{}{}{}", db.blue(), ":".blue(), version.blue()))
|
||||
.collect();
|
||||
Some(versions.join(", "))
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
|
||||
print!(" {}{} ", db.blue(), " ".repeat(max_db_len - db_str.len()));
|
||||
if let Some(versions) = versions {
|
||||
println!("supported versions: {versions}");
|
||||
} else {
|
||||
println!();
|
||||
}
|
||||
}
|
||||
println!();
|
||||
|
||||
println!("{}", "Services:".yellow().bold());
|
||||
let max_service_len = ServiceType::iter()
|
||||
.map(|service| <&'static str>::from(service).len())
|
||||
.max()
|
||||
.unwrap_or_default();
|
||||
for service in ServiceType::iter() {
|
||||
let service: ServiceType = service;
|
||||
let service_str: &'static str = service.into();
|
||||
println!(
|
||||
" {}{} {}",
|
||||
service.blue(),
|
||||
" ".repeat(max_service_len - service_str.len()),
|
||||
service.get_documentation().unwrap_or_default(),
|
||||
);
|
||||
}
|
||||
}
|
||||
println!();
|
||||
if !args.is_empty() {
|
||||
let max_arg_len = args
|
||||
.iter()
|
||||
.map(|(arg, _)| arg.len())
|
||||
.max()
|
||||
.unwrap_or_default();
|
||||
println!("{}", "Arguments:".yellow().bold());
|
||||
for (arg, desc) in args {
|
||||
println!(
|
||||
" {}{}{}{} {}",
|
||||
"[".green(),
|
||||
arg.green(),
|
||||
"]".green(),
|
||||
" ".repeat(max_arg_len - arg.len()),
|
||||
desc
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!(
|
||||
"{} {} {}",
|
||||
"Usage:".bright_yellow().bold(),
|
||||
"haze".blue(),
|
||||
"[filter] <COMMAND> [arguments]".green()
|
||||
);
|
||||
println!();
|
||||
println!("{}", "Commands:".yellow().bold());
|
||||
let max_command_len = HazeCommand::iter()
|
||||
.map(|command| <&'static str>::from(command).len())
|
||||
.max()
|
||||
.unwrap();
|
||||
let max_doc_len = HazeCommand::iter()
|
||||
.map(|command| {
|
||||
command
|
||||
.get_documentation()
|
||||
.unwrap_or_default()
|
||||
.split('\n')
|
||||
.next()
|
||||
.unwrap()
|
||||
.len()
|
||||
})
|
||||
.max()
|
||||
.unwrap();
|
||||
|
||||
for command in HazeCommand::iter() {
|
||||
let command: HazeCommand = command;
|
||||
let command_str = <&'static str>::from(command);
|
||||
let mut len = command_str.len();
|
||||
if command_str.starts_with("--") {
|
||||
len -= 2;
|
||||
}
|
||||
let doc: &str = command.get_documentation().unwrap_or_default();
|
||||
let doc = doc.split('\n').next().unwrap();
|
||||
println!(
|
||||
" {}{} {}{} {}",
|
||||
command.blue(),
|
||||
" ".repeat(max_command_len - len),
|
||||
doc,
|
||||
" ".repeat(max_doc_len - doc.len()),
|
||||
if command.allows_filter() {
|
||||
"- supports filter".fg::<Gray>()
|
||||
} else {
|
||||
"".fg::<Gray>()
|
||||
},
|
||||
);
|
||||
}
|
||||
println!();
|
||||
println!(
|
||||
"See {} {} for more information about a {}",
|
||||
"haze help".blue(),
|
||||
"<COMMAND>".green(),
|
||||
"<COMMAND>".green()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue