Skip to content

Commit

Permalink
Rewrite the prompt to accept arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
xcambar committed Sep 18, 2017
1 parent 6fe3dda commit 259533d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
39 changes: 27 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,37 @@ extern crate clap;
extern crate git2;
extern crate regex;

use clap::{App, Arg};
use clap::{App, AppSettings, Arg, SubCommand};

mod prompt;
mod precmd;

fn main() {
let matches = App::new("Pure prompt")
.arg(Arg::with_name("part")
.index(1)
.possible_values(&["precmd", "prompt"])
.required(true))
.get_matches();

match matches.value_of("part").unwrap() {
"precmd" => precmd::display(),
"prompt" => prompt::display(),
_ => (),
.setting(AppSettings::SubcommandRequired)
.subcommand(SubCommand::with_name("precmd"))
.subcommand(
SubCommand::with_name("prompt")
.arg(
Arg::with_name("last_return_code")
.short("r")
.takes_value(true)
)
.arg(
Arg::with_name("keymap")
.short("k")
.takes_value(true)
),
)
.get_matches();

match matches.subcommand() {
("precmd", _) => precmd::display(),
("prompt", Some(sub_matches)) => {
let last_return_code = sub_matches.value_of("last_return_code").unwrap();
let keymap = sub_matches.value_of("keymap").unwrap();
prompt::display(last_return_code, keymap);
}
_ => (),
}
}
}
21 changes: 10 additions & 11 deletions src/prompt.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use std::env;
const INSERT_SYMBOL:&str = "❯";
const COMMAND_SYMBOL:&str = "⬢";

const SYMBOL:&str = "❯";

pub fn display() {
let var = env::var("_LAST_RETURN_STATUS");
let last_command = match var {
Ok(val) => val.parse::<u8>().unwrap(),
Err(_e) => 1
pub fn display(return_code: &str, keymap: &str) {
let symbol = match keymap {
"vicmd" => COMMAND_SYMBOL,
_ => INSERT_SYMBOL,
};

let shell_color = match last_command {
0 => 5,
let shell_color = match (symbol, return_code) {
(COMMAND_SYMBOL, _) => 3,
(_, "0") => 5,
_ => 9,
};

print!("%F{{{}}}{}%f", shell_color, SYMBOL);
print!("%F{{{}}}{}%f ", shell_color, symbol);
}

0 comments on commit 259533d

Please sign in to comment.