summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSaurav Sharma <appdroiddeveloper@gmail.com>2019-08-20 10:27:25 +0545
committerMatan Kushner <hello@matchai.me>2019-08-20 00:42:25 -0400
commit075a76897cc28a6775c42b1d01a9c344c0f75ef0 (patch)
tree4f348709752d552694bda25e7f599359933be397 /src
parent0e82c19f37e80e258ad7f0e6136a2bcf37c9648a (diff)
fix: Change panicking on unknown module to error print and support module listing flag (#197)
Diffstat (limited to 'src')
-rw-r--r--src/main.rs22
-rw-r--r--src/module.rs19
-rw-r--r--src/modules/mod.rs6
-rw-r--r--src/print.rs20
4 files changed, 44 insertions, 23 deletions
diff --git a/src/main.rs b/src/main.rs
index 7143a59bf..16d0e3025 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -10,6 +10,7 @@ mod print;
mod segment;
mod utils;
+use crate::module::ALL_MODULES;
use clap::{App, AppSettings, Arg, SubCommand};
fn main() {
@@ -91,7 +92,14 @@ fn main() {
.arg(
Arg::with_name("name")
.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(&path_arg)
@@ -112,8 +120,16 @@ fn main() {
}
("prompt", Some(sub_m)) => print::prompt(sub_m.clone()),
("module", Some(sub_m)) => {
- let module_name = sub_m.value_of("name").expect("Module name missing.");
- print::module(module_name, sub_m.clone());
+ if sub_m.is_present("list") {
+ 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());
+ }
}
_ => {}
}
diff --git a/src/module.rs b/src/module.rs
index 98f586cb3..fcb1ea361 100644
--- a/src/module.rs
+++ b/src/module.rs
@@ -4,6 +4,25 @@ use ansi_term::Style;
use ansi_term::{ANSIString, ANSIStrings};
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
/// (e.g. The git module shows the current git branch and status)
pub struct Module<'a> {
diff --git a/src/modules/mod.rs b/src/modules/mod.rs
index 3ab7d8b3f..4aa67108e 100644
--- a/src/modules/mod.rs
+++ b/src/modules/mod.rs
@@ -1,3 +1,4 @@
+// While adding out new module add out module to src/module.rs ALL_MODULES const array also.
mod battery;
mod character;
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),
"jobs" => jobs::module(context),
- _ => panic!("Unknown module: {}", module),
+ _ => {
+ eprintln!("Error: Unknown module {}. Use starship module --list to list out all supported modules.", module);
+ None
+ }
}
}
diff --git a/src/print.rs b/src/print.rs
index 0cf495f72..69c56027a 100644
--- a/src/print.rs
+++ b/src/print.rs
@@ -5,27 +5,9 @@ use std::io::{self, Write};
use crate::config::Config;
use crate::context::Context;
use crate::module::Module;
+use crate::module::ALL_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
// 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.