Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
))
})
}
Expand Down
9 changes: 3 additions & 6 deletions src/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::format::CodeStr;
use crate::format::CodePath;
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
Expand Down Expand Up @@ -57,7 +57,7 @@ pub fn load() -> io::Result<State> {
// 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.
Expand All @@ -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();
Expand Down
Loading