Skip to content

Commit

Permalink
feat: improve display prompt (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandangel authored Mar 16, 2019
1 parent ee62bc2 commit 3a0c37c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 51 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 19 additions & 11 deletions src/precmd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ansi_term::Colour::{Blue, Cyan, Green, Purple, Red};
use ansi_term::{ANSIGenericString, ANSIStrings};
use ansi_term::ANSIStrings;
use ansi_term::Colour::{Blue, Cyan, Fixed, Green, Purple, Red, Yellow};
use clap::{App, Arg, ArgMatches, SubCommand};
use git2::{self, Repository, StatusOptions};
use regex::Regex;
Expand Down Expand Up @@ -27,7 +27,7 @@ fn repo_status(r: &Repository, detailed: bool) -> Option<String> {
if !detailed {
if let Some((index_change, wt_change, conflicted, untracked)) = count_files_statuses(r) {
if index_change != 0 || wt_change != 0 || conflicted != 0 || untracked != 0 {
out.push(Red.bold().paint(" *"));
out.push(Red.bold().paint("*"));
}
}
} else {
Expand All @@ -48,19 +48,19 @@ fn repo_status(r: &Repository, detailed: bool) -> Option<String> {
out.push(Green.paint(format!(" ♦ {}", index_change)));
}
if conflicted > 0 {
out.push(Red.paint(format!(" {}", conflicted)));
out.push(Red.paint(format!(" x {}", conflicted)));
}
if wt_change > 0 {
out.push(ANSIGenericString::from(format!(" {}", wt_change)));
out.push(Yellow.paint(format!(" + {}", wt_change)));
}
if untracked > 0 {
out.push(ANSIGenericString::from(" …"));
out.push(Fixed(245).paint(" *"));
}
}
}

if let Some(action) = get_action(r) {
out.push(Purple.paint(format!(" {}", action)));
out.push(Purple.paint(format!("{}", action)));
}
}

Expand Down Expand Up @@ -192,17 +192,25 @@ fn get_action(r: &Repository) -> Option<String> {
}

pub fn display(sub_matches: &ArgMatches) {
let my_path = env::current_dir().unwrap();
let display_path = Blue.paint(shorten_path(my_path.to_str().unwrap()));
let cur_path = env::current_dir().unwrap();
let cur_user = env::var("USER").unwrap();

let branch = match Repository::discover(my_path) {
let display_path = Blue.paint(shorten_path(cur_path.to_str().unwrap()));

let branch = match Repository::discover(cur_path) {
Ok(repo) => repo_status(&repo, sub_matches.is_present("git-detailed")),
Err(_e) => None,
};
let display_branch = Cyan.paint(branch.unwrap_or_default());

println!("");
println!("{} {}", display_path, display_branch);
println!(
"{} {} {} {}",
Purple.paint("λ"),
Green.paint(cur_user),
display_path,
display_branch
);
}

pub fn cli_arguments<'a>() -> App<'a, 'a> {
Expand Down
54 changes: 25 additions & 29 deletions src/prompt.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
use clap::{ArgMatches, App, SubCommand, Arg};
use clap::{App, Arg, ArgMatches, SubCommand};

const INSERT_SYMBOL:&str = "";
const COMMAND_SYMBOL:&str = "⬢";
const COMMAND_KEYMAP:&str = "vicmd";
const NO_ERROR:&str = "0";
const INSERT_SYMBOL: &str = "";
const COMMAND_SYMBOL: &str = "⬢";
const COMMAND_KEYMAP: &str = "vicmd";
const NO_ERROR: &str = "0";

pub fn display(sub_matches: &ArgMatches) {
let last_return_code = sub_matches.value_of("last_return_code").unwrap_or("0");
let keymap = sub_matches.value_of("keymap").unwrap_or("US");

let symbol = match keymap {
COMMAND_KEYMAP => COMMAND_SYMBOL,
_ => INSERT_SYMBOL,
};
let last_return_code = sub_matches.value_of("last_return_code").unwrap_or("0");
let keymap = sub_matches.value_of("keymap").unwrap_or("US");

let shell_color = match (symbol, last_return_code) {
(COMMAND_SYMBOL, _) => 3,
(_, NO_ERROR) => 5,
_ => 9,
};
let symbol = match keymap {
COMMAND_KEYMAP => COMMAND_SYMBOL,
_ => INSERT_SYMBOL,
};

print!("%F{{{}}}{}%f ", shell_color, symbol);
let shell_color = match (symbol, last_return_code) {
(COMMAND_SYMBOL, _) => 3,
(_, NO_ERROR) => 5,
_ => 9,
};

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

pub fn cli_arguments<'a>() -> App<'a, 'a> {
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)
)
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))
}

0 comments on commit 3a0c37c

Please sign in to comment.