~kmaasrud/prompt

5c65ae6abe96066d0db36bfd0262c09d4982ace9 — kmaasrud 1 year, 6 months ago 7ebbe50
simple prompt with cwd
1 files changed, 31 insertions(+), 2 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +31 -2
@@ 1,3 1,32 @@
fn main() {
    println!("Hello, world!");
#![feature(byte_slice_trim_ascii)]

use std::io::BufWriter;
use std::io::Error;
use std::io::Write;
use std::path::Path;

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

    cwd(&mut stdout)?;
    icon(&mut stdout)?;

    stdout.flush()
}

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

fn cwd<W: Write>(mut w: W) -> Result<(), Error> {
    if let Ok(mut cwd) = std::env::current_dir() {
        cwd = cwd.canonicalize()?;
        if let Some(home) = std::env::var_os("HOME") {
            if let Ok(stripped) = cwd.strip_prefix(home) {
                cwd = Path::new("~").join(stripped);
            }
        }
        write!(w, "{} ", cwd.display())?;
    }
    Ok(())
}