wisp/session/
workspace_status.rs1use std::path::{Path, PathBuf};
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub struct WorkspaceStatus {
5 pub display_dir: String,
6 pub git_ref: Option<String>,
7}
8
9impl WorkspaceStatus {
10 pub fn new(display_dir: impl Into<String>, git_ref: Option<String>) -> Self {
11 Self { display_dir: display_dir.into(), git_ref }
12 }
13
14 pub fn initial(cwd: &Path) -> Self {
18 Self::new(home_relative_path(cwd), None)
19 }
20}
21
22pub fn home_relative_path(path: &Path) -> String {
23 home_dir().map_or_else(|| path.display().to_string(), |home| home_relative_path_with_home(path, &home))
24}
25
26fn home_dir() -> Option<PathBuf> {
27 std::env::var_os("HOME").or_else(|| std::env::var_os("USERPROFILE")).map(PathBuf::from)
28}
29
30fn home_relative_path_with_home(path: &Path, home: &Path) -> String {
31 if path == home {
32 return "~".to_string();
33 }
34 path.strip_prefix(home)
35 .ok()
36 .filter(|relative| !relative.as_os_str().is_empty())
37 .map_or_else(|| path.display().to_string(), |relative| format!("~/{}", relative.display()))
38}