Simplify main CLI info message, improve colored highlighting

This commit is contained in:
timvisee 2018-04-18 22:52:27 +02:00
parent 69797a3119
commit ae7d424b0b
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2
6 changed files with 49 additions and 16 deletions

2
Cargo.lock generated
View file

@ -356,7 +356,7 @@ dependencies = [
[[package]]
name = "ffsend-cli"
version = "0.1.0"
version = "0.0.1-dev"
dependencies = [
"clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)",
"clipboard 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -1,5 +1,5 @@
# Release 0.1
- Set MIME type of file when downloaded
- Color usage flag
- Remember all uploaded files, make files listable
- Incognito mode, to not remember files `--incognito`
- Automatically get owner token, from file history when setting password

View file

@ -1,6 +1,7 @@
[package]
name = "ffsend-cli"
version = "0.1.0"
description = "A simple Firefox Send CLI client."
version = "0.0.1-dev"
authors = ["timvisee <timvisee@gmail.com>"]
workspace = ".."

View file

@ -2,7 +2,7 @@
pub const APP_NAME: &'static str = "ffsend";
/// The application version.
pub const APP_VERSION: &'static str = "0.1-dev";
pub const APP_VERSION: &'static str = "0.0.1-dev";
/// The application author.
pub const APP_AUTHOR: &'static str = "Tim Visee <https://timvisee.com/>";

View file

@ -1,3 +1,4 @@
#[macro_use]
extern crate clap;
#[macro_use]
extern crate derive_builder;
@ -24,7 +25,7 @@ use action::password::Password;
use action::upload::Upload;
use cmd::Handler;
use error::Error;
use util::{ErrorHints, quit_error};
use util::{ErrorHints, exe_name, highlight, quit_error};
/// Application entrypoint.
fn main() {
@ -84,10 +85,26 @@ fn invoke_action(handler: &Handler) -> Result<(), Error> {
.map_err(|err| err.into());
}
// No subcommand was selected, show general help
Handler::build()
.print_help()
.expect("failed to print command help");
// Print the main info and return
print_main_info();
Ok(())
}
/// Print the main info, shown when no subcommands were supplied.
pub fn print_main_info() {
// Get the name of the used executable
let exe = exe_name();
// Print the main info
println!("{} {}", crate_name!(), crate_version!());
println!("Usage: {} [FLAGS] <SUBCOMMAND> ...", exe);
println!("");
println!("{}", crate_description!());
println!("");
println!("Missing subcommand. Here are the most used:");
println!(" {}", highlight(&format!("{} upload <FILE> ...", exe)));
println!(" {}", highlight(&format!("{} download <URL> ...", exe)));
println!("");
println!("To show all subcommands, features and other help:");
println!(" {}", highlight(&format!("{} help [SUBCOMMAND]", exe)));
}

View file

@ -3,6 +3,7 @@ extern crate clipboard;
extern crate colored;
extern crate open;
use std::env::current_exe;
#[cfg(feature = "clipboard")]
use std::error::Error as StdError;
use std::fmt::{Debug, Display};
@ -121,19 +122,19 @@ impl ErrorHints {
// Print hints
if self.password {
eprintln!("Use '{}' to specify a password", "--password <PASSWORD>".yellow());
eprintln!("Use '{}' to specify a password", highlight("--password <PASSWORD>"));
}
if self.owner {
eprintln!("Use '{}' to specify an owner token", "--owner <TOKEN>".yellow());
eprintln!("Use '{}' to specify an owner token", highlight("--owner <TOKEN>"));
}
if self.force {
eprintln!("Use '{}' to force", "--force".yellow());
eprintln!("Use '{}' to force", highlight("--force"));
}
if self.verbose {
eprintln!("For detailed errors try '{}'", "--verbose".yellow());
eprintln!("For detailed errors try '{}'", highlight("--verbose"));
}
if self.help {
eprintln!("For more information try '{}'", "--help".yellow());
eprintln!("For more information try '{}'", highlight("--help"));
}
// Flush
@ -153,6 +154,11 @@ impl Default for ErrorHints {
}
}
/// Highlight the given text with a color.
pub fn highlight(msg: &str) -> ColoredString {
msg.yellow()
}
/// Open the given URL in the users default browser.
/// The browsers exit statis is returned.
pub fn open_url(url: Url) -> Result<ExitStatus, IoError> {
@ -394,7 +400,7 @@ pub fn ensure_owner_token(
if token.as_ref().unwrap().is_empty() {
eprintln!(
"Empty owner token given, which is invalid. Use {} to cancel.",
"[CTRL+C]".yellow(),
highlight("[CTRL+C]"),
);
*token = None;
} else {
@ -415,3 +421,12 @@ pub fn format_bytes(bytes: u64) -> String {
_ => format!("{:.*} B", 0, bytes),
}
}
/// Get the name of the executable that was invoked.
pub fn exe_name() -> String {
current_exe()
.ok()
.and_then(|p| p.file_name().map(|n| n.to_owned()))
.and_then(|n| n.into_string().ok())
.unwrap_or(crate_name!().into())
}