mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 18:05:04 +02:00
Merge pull request #59 from jonathandturner/gitbranch
Show the current git branch in the prompt
This commit is contained in:
@ -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
21
src/git.rs
Normal 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
|
||||
}
|
||||
}
|
@ -11,6 +11,7 @@ mod env;
|
||||
mod errors;
|
||||
mod evaluate;
|
||||
mod format;
|
||||
mod git;
|
||||
mod object;
|
||||
mod parser;
|
||||
mod prelude;
|
||||
|
Reference in New Issue
Block a user