From e2ba7a13542c0fcefee3e6e85d8b0bf3ec6c8265 Mon Sep 17 00:00:00 2001 From: Matan Kushner Date: Wed, 3 Apr 2019 20:14:26 -0400 Subject: [PATCH] Add segment structure and logic --- .gitignore | 14 ++++++++++++-- .vscode/launch.json | 45 +++++++++++++++++++++++++++++++++++++++++++++ README.md | 2 +- src/char.rs | 20 -------------------- src/main.rs | 21 +++++++++++++++------ src/modules/char.rs | 34 ++++++++++++++++++++++++++++++++++ src/modules/mod.rs | 11 +++++++++++ src/print.rs | 30 ++++++++++++++++++++++++++++++ 8 files changed, 148 insertions(+), 29 deletions(-) create mode 100644 .vscode/launch.json delete mode 100644 src/char.rs create mode 100644 src/modules/char.rs create mode 100644 src/modules/mod.rs create mode 100644 src/print.rs diff --git a/.gitignore b/.gitignore index f0e3bcacb..a81d3520e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,12 @@ -/target -**/*.rs.bk \ No newline at end of file +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# VSCode configuration +.vscode/ diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..acec0046b --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,45 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'starship'", + "cargo": { + "args": [ + "build", + "--bin=starship", + "--package=starship" + ], + "filter": { + "name": "starship", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'starship'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=starship", + "--package=starship" + ], + "filter": { + "name": "starship", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index e22bf4685..4dba8a84f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -

Starship 🌠🚀

+

Starship ✨🚀

The cross-platform prompt for astronauts.

--- diff --git a/src/char.rs b/src/char.rs deleted file mode 100644 index 7d0998b4a..000000000 --- a/src/char.rs +++ /dev/null @@ -1,20 +0,0 @@ -use std::env; -use ansi_term::Color; - -pub fn display() { - let PROMPT_CHAR = "➜ "; - let COLOR_SUCCESS = Color::Green; - let COLOR_FAILURE = Color::Red; - - let color = match env::var_os("status") { - None | "0" => COLOR_SUCCESS, - _ => COLOR_FAILURE - }; - - // let color = match env::var("status") { - // Ok("0") | _ => COLOR_SUCCESS, - // Ok("1") => COLOR_FAILURE - // }; - - print!("{}", color.paint(PROMPT_CHAR)); -} diff --git a/src/main.rs b/src/main.rs index 8a6cd5fd7..ab7e04e56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,19 @@ #[macro_use] extern crate clap; -use clap::App; -use std::io; +extern crate ansi_term; -mod char; +mod modules; +mod print; + +use ansi_term::Style; +use clap::App; + +pub struct Segment { + style: Style, + value: String, + prefix: Option>, + suffix: Option>, +} fn main() { App::new("Starship") @@ -12,9 +22,8 @@ fn main() { .version(crate_version!()) // pull the authors from Cargo.toml .author(crate_authors!()) + .after_help("https://github.com/matchai/starship") .get_matches(); - prompt::char(); - // let stdout = io::stdout(); - // let mut handle = io::BufWriter::new(stdout); + print::prompt(); } diff --git a/src/modules/char.rs b/src/modules/char.rs new file mode 100644 index 000000000..96387c740 --- /dev/null +++ b/src/modules/char.rs @@ -0,0 +1,34 @@ +use crate::Segment; +use ansi_term::{Color, Style}; +use std::env; + +pub fn segment() -> Segment { + const PROMPT_CHAR: &str = "➜ "; + const COLOR_SUCCESS: Color = Color::Green; + const COLOR_FAILURE: Color = Color::Red; + + let default_prefix = Segment { + value: String::from("testPrefix"), + style: Style::default(), + prefix: None, + suffix: None, + }; + + let color; + if let Ok(status) = env::var("status") { + if status == "0" { + color = COLOR_SUCCESS; + } else { + color = COLOR_FAILURE; + } + } else { + panic!("No status environment variable provided"); + } + + Segment { + prefix: Some(Box::new(default_prefix)), + value: String::from(PROMPT_CHAR), + style: Style::new().fg(color), + suffix: None, + } +} diff --git a/src/modules/mod.rs b/src/modules/mod.rs new file mode 100644 index 000000000..8fe6d63a4 --- /dev/null +++ b/src/modules/mod.rs @@ -0,0 +1,11 @@ +mod char; + +use crate::Segment; + +pub fn handle(module: &str) -> Segment { + match module { + "char" => char::segment(), + + _ => panic!("Unknown module: {}", module), + } +} diff --git a/src/print.rs b/src/print.rs new file mode 100644 index 000000000..2f7ca0999 --- /dev/null +++ b/src/print.rs @@ -0,0 +1,30 @@ +use crate::modules; +use crate::Segment; + +pub fn prompt() { + let default_prompt = vec!["char"]; + + for module in default_prompt { + let segment = modules::handle(module); + print_segment(segment); + } +} + +fn print_segment(segment: Segment) { + let Segment { + prefix, + value, + style, + suffix, + } = segment; + + if let Some(prefix) = prefix { + print_segment(*prefix); + } + + print!("{}", style.paint(value)); + + if let Some(suffix) = suffix { + print_segment(*suffix); + } +}