Merge pull request #59 from jonathandturner/gitbranch

Show the current git branch in the prompt
This commit is contained in:
Jonathan Turner
2019-06-02 11:39:58 +12:00
committed by GitHub
5 changed files with 179 additions and 2 deletions

View File

@ -11,6 +11,7 @@ crate use crate::format::{EntriesListView, GenericView};
use crate::object::Value;
use crate::parser::{ParsedCommand, Pipeline};
use crate::stream::empty_stream;
use crate::git::current_branch;
use log::debug;
use rustyline::error::ReadlineError;
@ -83,8 +84,12 @@ pub async fn cli() -> Result<(), Box<Error>> {
loop {
let readline = rl.readline(&format!(
"{}> ",
context.env.lock().unwrap().cwd().display().to_string()
"{}{}> ",
context.env.lock().unwrap().cwd().display().to_string(),
match current_branch() {
Some(s) => format!("({})", s),
None => "".to_string()
}
));
match process_line(readline, &mut context).await {

21
src/git.rs Normal file
View File

@ -0,0 +1,21 @@
use git2::{Repository, RepositoryOpenFlags};
use std::ffi::OsString;
pub fn current_branch() -> Option<String> {
let v: Vec<OsString> = vec![];
match Repository::open_ext(".", RepositoryOpenFlags::empty(), v) {
Ok(repo) => {
let r = repo.head();
match r {
Ok(r) => {
match r.shorthand() {
Some(s) => Some(s.to_string()),
None => None,
}
},
_ => None
}
},
_ => None
}
}

View File

@ -11,6 +11,7 @@ mod env;
mod errors;
mod evaluate;
mod format;
mod git;
mod object;
mod parser;
mod prelude;