|
| 1 | +pub mod env { |
| 2 | + pub const BAT_GITSIGNS: &str = "BAT_GITSIGNS"; |
| 3 | +} |
| 4 | + |
| 5 | +#[cfg(feature = "git")] |
| 6 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 7 | +pub struct Gitsigns { |
| 8 | + pub modified: String, |
| 9 | + pub added: String, |
| 10 | + pub removed_above: String, |
| 11 | + pub removed_below: String, |
| 12 | +} |
| 13 | + |
| 14 | +#[cfg(feature = "git")] |
| 15 | +impl Default for Gitsigns { |
| 16 | + fn default() -> Self { |
| 17 | + Gitsigns::classic() |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +#[cfg(feature = "git")] |
| 22 | +impl Gitsigns { |
| 23 | + pub fn classic() -> Self { |
| 24 | + Self { |
| 25 | + modified: "~".into(), |
| 26 | + added: "+".into(), |
| 27 | + removed_above: "‾".into(), |
| 28 | + removed_below: "_".into(), |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + pub fn modern() -> Self { |
| 33 | + Self { |
| 34 | + modified: "▎".to_string(), |
| 35 | + added: "▎".to_string(), |
| 36 | + removed_above: "‾".to_string(), |
| 37 | + removed_below: "_".to_string(), |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + pub fn parse(s: &str) -> Result<Self, String> { |
| 42 | + let parts: Vec<&str> = s |
| 43 | + .split(',') |
| 44 | + .map(|p| { |
| 45 | + // allow single space char as gitsign |
| 46 | + if p == " " { |
| 47 | + return p; |
| 48 | + } |
| 49 | + |
| 50 | + p.trim() |
| 51 | + }) |
| 52 | + .collect(); |
| 53 | + |
| 54 | + if parts.len() != 4 { |
| 55 | + return Err("Expected 4 comma-separated signs: `modified,added,removed-above,removed-below`, e.g. `~,+,‾,_`".into()); |
| 56 | + } |
| 57 | + |
| 58 | + for (i, part) in parts.iter().enumerate() { |
| 59 | + if part.chars().count() != 1 { |
| 60 | + return Err(format!( |
| 61 | + "Invalid sign at position {}: `{}`. Each sign must be a single character.", |
| 62 | + i + 1, |
| 63 | + part |
| 64 | + )); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + Ok(Self { |
| 69 | + modified: parts[0].to_string(), |
| 70 | + added: parts[1].to_string(), |
| 71 | + removed_above: parts[2].to_string(), |
| 72 | + removed_below: parts[3].to_string(), |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
0 commit comments