const SPINNER_NERD: [&str; 10] = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
const SPINNER_ASCII: [&str; 4] = ["-", "\\", "|", "/"];
pub struct Glyphs {
nerd: bool,
}
impl Glyphs {
pub fn new(nerd: bool) -> Glyphs {
Glyphs { nerd }
}
pub fn spinner(&self) -> &'static str {
"…"
}
pub fn spinner_frame(&self, tick: usize) -> &'static str {
let set: &[&str] = if self.nerd {
&SPINNER_NERD
} else {
&SPINNER_ASCII
};
set[tick % set.len()]
}
pub fn spinner_frame_count(&self) -> usize {
if self.nerd {
SPINNER_NERD.len()
} else {
SPINNER_ASCII.len()
}
}
pub fn absent(&self) -> &'static str {
"–"
}
pub fn current(&self) -> &'static str {
if self.nerd { "▸" } else { "*" }
}
pub fn missing(&self) -> &'static str {
if self.nerd { "✘" } else { "!" }
}
pub fn detached(&self) -> &'static str {
if self.nerd { "⚓" } else { "~" }
}
pub fn branchless(&self) -> &'static str {
if self.nerd { "" } else { "○" }
}
pub fn dirty(&self) -> &'static str {
if self.nerd { "●" } else { "M" }
}
pub fn untracked(&self) -> &'static str {
"?"
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ascii_fallbacks_match_list_markers() {
let g = Glyphs::new(false);
assert_eq!(g.current(), "*");
assert_eq!(g.missing(), "!");
assert_eq!(g.detached(), "~");
assert_eq!(g.branchless(), "○");
assert_eq!(g.dirty(), "M");
assert_eq!(g.untracked(), "?");
assert_eq!(g.spinner(), "…");
assert_eq!(g.absent(), "–");
}
#[test]
fn nerd_fonts_differ_from_ascii() {
let ascii = Glyphs::new(false);
let nerd = Glyphs::new(true);
assert_ne!(ascii.current(), nerd.current());
assert_ne!(ascii.missing(), nerd.missing());
assert_ne!(ascii.branchless(), nerd.branchless());
assert_eq!(ascii.spinner(), nerd.spinner());
}
#[test]
fn spinner_frame_wraps_and_advances() {
for nerd in [false, true] {
let g = Glyphs::new(nerd);
let count = g.spinner_frame_count();
assert_eq!(g.spinner_frame(0), g.spinner_frame(count));
assert_ne!(g.spinner_frame(0), g.spinner_frame(1));
}
assert_eq!(Glyphs::new(true).spinner_frame_count(), 10);
assert_eq!(Glyphs::new(false).spinner_frame_count(), 4);
}
#[test]
fn spinner_frames_differ_nerd_vs_ascii() {
assert_ne!(
Glyphs::new(true).spinner_frame(2),
Glyphs::new(false).spinner_frame(2)
);
}
}