Add segment structure and logic

This commit is contained in:
Matan Kushner 2019-04-03 20:14:26 -04:00
parent 41ee54933b
commit e2ba7a1354
No known key found for this signature in database
GPG Key ID: 4B98C3A8949CA8A4
8 changed files with 148 additions and 29 deletions

12
.gitignore vendored
View File

@ -1,2 +1,12 @@
/target # 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 **/*.rs.bk
# VSCode configuration
.vscode/

45
.vscode/launch.json vendored Normal file
View File

@ -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}"
}
]
}

View File

@ -1,4 +1,4 @@
<h3 align="center">Starship 🌠🚀</h3> <h3 align="center">Starship 🚀</h3>
<p align="center">The cross-platform prompt for astronauts.</p> <p align="center">The cross-platform prompt for astronauts.</p>
--- ---

View File

@ -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));
}

View File

@ -1,9 +1,19 @@
#[macro_use] #[macro_use]
extern crate clap; extern crate clap;
use clap::App; extern crate ansi_term;
use std::io;
mod char; mod modules;
mod print;
use ansi_term::Style;
use clap::App;
pub struct Segment {
style: Style,
value: String,
prefix: Option<Box<Segment>>,
suffix: Option<Box<Segment>>,
}
fn main() { fn main() {
App::new("Starship") App::new("Starship")
@ -12,9 +22,8 @@ fn main() {
.version(crate_version!()) .version(crate_version!())
// pull the authors from Cargo.toml // pull the authors from Cargo.toml
.author(crate_authors!()) .author(crate_authors!())
.after_help("https://github.com/matchai/starship")
.get_matches(); .get_matches();
prompt::char(); print::prompt();
// let stdout = io::stdout();
// let mut handle = io::BufWriter::new(stdout);
} }

34
src/modules/char.rs Normal file
View File

@ -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,
}
}

11
src/modules/mod.rs Normal file
View File

@ -0,0 +1,11 @@
mod char;
use crate::Segment;
pub fn handle(module: &str) -> Segment {
match module {
"char" => char::segment(),
_ => panic!("Unknown module: {}", module),
}
}

30
src/print.rs Normal file
View File

@ -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);
}
}