dodot_lib/commands/
mod.rs1pub mod addignore;
9pub mod adopt;
10pub mod down;
11pub mod fill;
12pub mod init;
13pub mod list;
14pub mod status;
15pub mod up;
16
17#[cfg(test)]
18mod tests;
19
20use serde::Serialize;
21
22pub fn handler_symbol(handler: &str) -> &'static str {
26 match handler {
27 "symlink" => "➞",
28 "shell" => "⚙",
29 "path" => "+",
30 "homebrew" => "⚙",
31 "install" => "×",
32 _ => "?",
33 }
34}
35
36pub fn status_style(deployed: bool) -> &'static str {
38 if deployed {
39 "deployed"
40 } else {
41 "pending"
42 }
43}
44
45pub fn handler_description(handler: &str, rel_path: &str, user_target: Option<&str>) -> String {
47 match handler {
48 "symlink" => {
49 if let Some(target) = user_target {
50 target.to_string()
51 } else {
52 let display_path = if !rel_path.contains('/') && rel_path.starts_with("dot.") {
54 format!(".{}", &rel_path[4..])
55 } else {
56 format!(".{rel_path}")
57 };
58 format!("~/{display_path}")
59 }
60 }
61 "shell" => "shell profile".into(),
62 "path" => format!("$PATH/{rel_path}"),
63 "install" => "run script".into(),
64 "homebrew" => "brew install".into(),
65 _ => String::new(),
66 }
67}
68
69#[derive(Debug, Clone, Serialize)]
71pub struct DisplayFile {
72 pub name: String,
73 pub symbol: String,
74 pub description: String,
75 pub status: String,
76 pub status_label: String,
77 pub handler: String,
78}
79
80#[derive(Debug, Clone, Serialize)]
82pub struct DisplayPack {
83 pub name: String,
84 pub files: Vec<DisplayFile>,
85}
86
87#[derive(Debug, Clone, Serialize)]
89pub struct DisplayClaimant {
90 pub pack: String,
92 pub source: String,
94}
95
96#[derive(Debug, Clone, Serialize)]
98pub struct DisplayConflict {
99 pub kind: String,
102 pub target: String,
104 pub claimants: Vec<DisplayClaimant>,
105}
106
107impl DisplayConflict {
108 pub fn from_conflict(c: &crate::conflicts::Conflict, home: &std::path::Path) -> Self {
111 let kind = match c.kind {
112 crate::conflicts::ConflictKind::SymlinkTarget => "symlink",
113 crate::conflicts::ConflictKind::PathExecutable => "path",
114 };
115 let target = match c.kind {
116 crate::conflicts::ConflictKind::SymlinkTarget => shorten_path(&c.target, home),
117 crate::conflicts::ConflictKind::PathExecutable => c
118 .target
119 .file_name()
120 .map(|n| n.to_string_lossy().into_owned())
121 .unwrap_or_else(|| c.target.display().to_string()),
122 };
123 let claimants = c
124 .claimants
125 .iter()
126 .map(|cl| DisplayClaimant {
127 pack: cl.pack.clone(),
128 source: pack_relative_source(&cl.source, &cl.pack),
129 })
130 .collect();
131 DisplayConflict {
132 kind: kind.into(),
133 target,
134 claimants,
135 }
136 }
137}
138
139fn shorten_path(p: &std::path::Path, home: &std::path::Path) -> String {
140 if let Ok(rel) = p.strip_prefix(home) {
141 format!("~/{}", rel.display())
142 } else {
143 p.display().to_string()
144 }
145}
146
147fn pack_relative_source(source: &std::path::Path, pack: &str) -> String {
150 let s = source.to_string_lossy();
151 let marker = format!("/{pack}/");
152 if let Some(idx) = s.rfind(&marker) {
153 let rel = &s[idx + 1..];
154 return rel.to_string();
155 }
156 let fname = source
158 .file_name()
159 .map(|n| n.to_string_lossy().into_owned())
160 .unwrap_or_default();
161 format!("{pack}/{fname}")
162}
163
164#[derive(Debug, Clone, Serialize)]
167pub struct PackStatusResult {
168 #[serde(skip_serializing_if = "Option::is_none")]
169 pub message: Option<String>,
170 pub dry_run: bool,
171 pub packs: Vec<DisplayPack>,
172 #[serde(skip_serializing_if = "Vec::is_empty")]
173 pub warnings: Vec<String>,
174 #[serde(skip_serializing_if = "Vec::is_empty")]
176 pub conflicts: Vec<DisplayConflict>,
177 #[serde(skip_serializing_if = "Vec::is_empty")]
181 pub ignored_packs: Vec<String>,
182}