~kmaasrud/prompt

06a969ac787fe6bf34464a7706e4c7b58062d07c — kmaasrud 1 year, 6 months ago 5c65ae6
add git info
1 files changed, 28 insertions(+), 0 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +28 -0
@@ 4,16 4,30 @@ use std::io::BufWriter;
use std::io::Error;
use std::io::Write;
use std::path::Path;
use std::process::Command;

fn main() -> Result<(), Error> {
    let mut stdout = BufWriter::new(std::io::stdout());

    cwd(&mut stdout)?;

    faint(&mut stdout)?;
    git(&mut stdout)?;
    reset(&mut stdout)?;

    icon(&mut stdout)?;

    stdout.flush()
}

fn faint<W: Write>(mut w: W) -> Result<(), Error> {
    write!(w, "\x1b[2m")
}

fn reset<W: Write>(mut w: W) -> Result<(), Error> {
    write!(w, "\x1b[0m")
}

fn icon<W: Write>(mut w: W) -> Result<(), Error> {
    write!(w, "❯ ")
}


@@ 30,3 44,17 @@ fn cwd<W: Write>(mut w: W) -> Result<(), Error> {
    }
    Ok(())
}

fn git<W: Write>(mut w: W) -> Result<(), Error> {
    match Command::new("git")
        .args(["rev-parse", "--abbrev-ref", "HEAD"])
        .output()
    {
        Ok(abbrev_ref) if abbrev_ref.status.success() => {
            write!(w, "(")?;
            w.write_all(abbrev_ref.stdout.trim_ascii())?;
            write!(w, ") ")
        }
        _ => Ok(()),
    }
}