fix: Change panicking on unknown module to error print and support module listing flag (#197)

This commit is contained in:
Saurav Sharma 2019-08-20 10:27:25 +05:45 committed by Matan Kushner
parent 0e82c19f37
commit 075a76897c
6 changed files with 76 additions and 23 deletions

View File

@ -10,6 +10,7 @@ mod print;
mod segment; mod segment;
mod utils; mod utils;
use crate::module::ALL_MODULES;
use clap::{App, AppSettings, Arg, SubCommand}; use clap::{App, AppSettings, Arg, SubCommand};
fn main() { fn main() {
@ -91,7 +92,14 @@ fn main() {
.arg( .arg(
Arg::with_name("name") Arg::with_name("name")
.help("The name of the module to be printed") .help("The name of the module to be printed")
.required(true), .required(true)
.required_unless("list"),
)
.arg(
Arg::with_name("list")
.short("l")
.long("list")
.help("List out all supported modules"),
) )
.arg(&status_code_arg) .arg(&status_code_arg)
.arg(&path_arg) .arg(&path_arg)
@ -112,8 +120,16 @@ fn main() {
} }
("prompt", Some(sub_m)) => print::prompt(sub_m.clone()), ("prompt", Some(sub_m)) => print::prompt(sub_m.clone()),
("module", Some(sub_m)) => { ("module", Some(sub_m)) => {
let module_name = sub_m.value_of("name").expect("Module name missing."); if sub_m.is_present("list") {
print::module(module_name, sub_m.clone()); println!("Supported modules list");
println!("----------------------");
for modules in ALL_MODULES {
println!("{}", modules);
}
}
if let Some(module_name) = sub_m.value_of("name") {
print::module(module_name, sub_m.clone());
}
} }
_ => {} _ => {}
} }

View File

@ -4,6 +4,25 @@ use ansi_term::Style;
use ansi_term::{ANSIString, ANSIStrings}; use ansi_term::{ANSIString, ANSIStrings};
use std::fmt; use std::fmt;
// List of all modules
pub const ALL_MODULES: &[&str] = &[
"battery",
"character",
"cmd_duration",
"directory",
"git_branch",
"git_status",
"golang",
"jobs",
"line_break",
"nodejs",
"package",
"python",
"ruby",
"rust",
"username",
];
/// A module is a collection of segments showing data for a single integration /// A module is a collection of segments showing data for a single integration
/// (e.g. The git module shows the current git branch and status) /// (e.g. The git module shows the current git branch and status)
pub struct Module<'a> { pub struct Module<'a> {

View File

@ -1,3 +1,4 @@
// While adding out new module add out module to src/module.rs ALL_MODULES const array also.
mod battery; mod battery;
mod character; mod character;
mod cmd_duration; mod cmd_duration;
@ -35,6 +36,9 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"cmd_duration" => cmd_duration::module(context), "cmd_duration" => cmd_duration::module(context),
"jobs" => jobs::module(context), "jobs" => jobs::module(context),
_ => panic!("Unknown module: {}", module), _ => {
eprintln!("Error: Unknown module {}. Use starship module --list to list out all supported modules.", module);
None
}
} }
} }

View File

@ -5,27 +5,9 @@ use std::io::{self, Write};
use crate::config::Config; use crate::config::Config;
use crate::context::Context; use crate::context::Context;
use crate::module::Module; use crate::module::Module;
use crate::module::ALL_MODULES;
use crate::modules; use crate::modules;
// List of all modules
const ALL_MODULES: &[&str] = &[
"battery",
"character",
"cmd_duration",
"directory",
"git_branch",
"git_status",
"golang",
"jobs",
"line_break",
"nodejs",
"package",
"python",
"ruby",
"rust",
"username",
];
// List of default prompt order // List of default prompt order
// NOTE: If this const value is changed then Default prompt order subheading inside // NOTE: If this const value is changed then Default prompt order subheading inside
// prompt heading of config docs needs to be updated according to changes made here. // prompt heading of config docs needs to be updated according to changes made here.

View File

@ -6,6 +6,7 @@ mod directory;
mod golang; mod golang;
mod jobs; mod jobs;
mod line_break; mod line_break;
mod modules;
mod nodejs; mod nodejs;
mod python; mod python;
mod ruby; mod ruby;

View File

@ -0,0 +1,31 @@
use std::io;
use crate::common;
#[test]
fn unknown_module_name() -> io::Result<()> {
let unknown_module_name = "some_random_name";
let output = common::render_module(unknown_module_name).output()?;
let actual_stdout = String::from_utf8(output.stdout).unwrap();
let actual_stderr = String::from_utf8(output.stderr).unwrap();
let expected_stdout = "";
let expected_stderr = format!(
"Error: Unknown module {}. Use starship module --list to list out all supported modules.\n",
unknown_module_name
);
assert_eq!(expected_stdout, actual_stdout);
assert_eq!(expected_stderr, actual_stderr);
Ok(())
}
#[test]
fn known_module_name() -> io::Result<()> {
let output = common::render_module("line_break").output()?;
let actual_stdout = String::from_utf8(output.stdout).unwrap();
let actual_stderr = String::from_utf8(output.stderr).unwrap();
let expected_stdout = "\n";
let expected_stderr = "";
assert_eq!(expected_stdout, actual_stdout);
assert_eq!(expected_stderr, actual_stderr);
Ok(())
}