A src/cwd.rs => src/cwd.rs +15 -0
@@ 0,0 1,15 @@
+use std::io::{Error, Write};
+use std::path::Path;
+
+pub 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(())
+}
A src/git.rs => src/git.rs +16 -0
@@ 0,0 1,16 @@
+use std::io::{Error, Write};
+use std::process::Command;
+
+pub 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(()),
+ }
+}
A src/icon.rs => src/icon.rs +5 -0
@@ 0,0 1,5 @@
+use std::io::{Error, Write};
+
+pub fn icon<W: Write>(mut w: W) -> Result<(), Error> {
+ write!(w, "❯ ")
+}
M src/main.rs => src/main.rs +10 -46
@@ 1,25 1,27 @@
#![feature(byte_slice_trim_ascii)]
+mod cwd;
+mod git;
+mod icon;
+mod nix;
+
use std::io::BufWriter;
-use std::io::Error;
-use std::io::Write;
-use std::path::Path;
-use std::process::Command;
+use std::io::{Error, Write};
fn main() -> Result<(), Error> {
let mut stdout = BufWriter::new(std::io::stdout());
faint(&mut stdout)?;
- nix(&mut stdout)?;
+ nix::nix(&mut stdout)?;
reset(&mut stdout)?;
- cwd(&mut stdout)?;
+ cwd::cwd(&mut stdout)?;
faint(&mut stdout)?;
- git(&mut stdout)?;
+ git::git(&mut stdout)?;
reset(&mut stdout)?;
- icon(&mut stdout)?;
+ icon::icon(&mut stdout)?;
stdout.flush()
}
@@ 31,41 33,3 @@ fn faint<W: Write>(mut w: W) -> Result<(), Error> {
fn reset<W: Write>(mut w: W) -> Result<(), Error> {
write!(w, "\x1b[0m")
}
-
-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(())
-}
-
-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(()),
- }
-}
-
-fn nix<W: Write>(mut w: W) -> Result<(), Error> {
- if let Ok(purity) = std::env::var("IN_NIX_SHELL") {
- write!(w, "{purity} ")?;
- }
- Ok(())
-}
A src/nix.rs => src/nix.rs +10 -0
@@ 0,0 1,10 @@
+use std::env::var_os;
+use std::io::{Error, Write};
+use std::os::unix::prelude::OsStrExt;
+
+pub fn nix<W: Write>(mut w: W) -> Result<(), Error> {
+ if let Some(purity) = var_os("IN_NIX_SHELL") {
+ w.write_all(purity.as_bytes())?;
+ }
+ Ok(())
+}