diff --git a/src/format.rs b/src/format.rs index c0f37d0..ed780f7 100644 --- a/src/format.rs +++ b/src/format.rs @@ -1,4 +1,16 @@ use colored::{ColoredString, Colorize, control::SHOULD_COLORIZE}; +use std::path::Path; + +// This trait formats a filesystem path for human-facing diagnostic output. +pub trait CodePath { + fn code_path(&self) -> ColoredString; +} + +impl CodePath for Path { + fn code_path(&self) -> ColoredString { + self.to_string_lossy().code_str() + } +} // This trait has a function for formatting "code-like" text, such as a file path. The reason it's // implemented as a trait and not just a function is so we can use it with method syntax, as in diff --git a/src/run.rs b/src/run.rs index ab47bd4..5cf17ce 100644 --- a/src/run.rs +++ b/src/run.rs @@ -3,6 +3,9 @@ use crate::{ format::CodeStr, state::{self, State}, }; + +#[cfg(target_os = "linux")] +use crate::format::CodePath; use byte_unit::{Byte, UnitType}; use chrono::{DateTime, Utc}; use regex::RegexSet; @@ -366,7 +369,7 @@ fn get_disk_by_file<'a>(disks: &'a [Disk], path: &Path) -> io::Result<&'a Disk> .ok_or_else(|| { io::Error::other(format!( "Unable to find disk for path {}.", - path.to_string_lossy().code_str(), + path.code_path(), )) }) } diff --git a/src/state.rs b/src/state.rs index ef30d9e..8e61857 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,4 +1,4 @@ -use crate::format::CodeStr; +use crate::format::CodePath; use serde::{Deserialize, Serialize}; use std::{ collections::HashMap, @@ -57,7 +57,7 @@ pub fn load() -> io::Result { // Log what we are trying to do in case an error occurs. trace!( "Attempting to load the state from {}\u{2026}", - path.to_string_lossy().code_str(), + path.code_path(), ); // Read the YAML from disk. @@ -76,10 +76,7 @@ pub fn save(state: &State) -> io::Result<()> { // Check if we have a path. if let Some(path) = path() { // Log what we're trying to do in case an error occurs. - trace!( - "Persisting the state to {}\u{2026}", - path.to_string_lossy().code_str(), - ); + trace!("Persisting the state to {}\u{2026}", path.code_path()); // The `unwrap` is safe due to [ref:state_path_has_parent]. let parent = path.parent().unwrap().to_owned();