Skip to content

Commit fb5e4e3

Browse files
committed
feat: multiple discovery_paths and sane defaults
1 parent 8043e74 commit fb5e4e3

2 files changed

Lines changed: 33 additions & 18 deletions

File tree

src-tauri/src/lib.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,29 +205,39 @@ pub struct Defaults {
205205
pub autostart: bool,
206206
pub autostart_minimized: bool,
207207
pub port: u16,
208-
pub discovery_path: PathBuf,
208+
pub discovery_paths: Vec<PathBuf>,
209209
}
210210

211211
impl Default for Defaults {
212212
fn default() -> Self {
213-
let discovery_path = if cfg!(target_os = "linux") {
214-
UserDirs::new()
215-
.map(|dirs| dirs.home_dir().join("aw-modules"))
216-
.unwrap_or_default()
213+
let mut discovery_paths = Vec::new();
214+
215+
if cfg!(target_os = "linux") {
216+
if let Some(dirs) = UserDirs::new() {
217+
discovery_paths.push(dirs.home_dir().join("aw-modules"));
218+
}
217219
} else if cfg!(windows) {
218-
let username = std::env::var("USERNAME").unwrap_or_default();
219-
PathBuf::from(format!(r"C:\Users\{}\aw-modules", username))
220+
if let Ok(username) = std::env::var("USERNAME") {
221+
discovery_paths.push(PathBuf::from(format!(r"C:\Users\{}\aw-modules", username)));
222+
discovery_paths.push(PathBuf::from(format!(
223+
r"C:\Users\{}\AppData\Local\Programs\ActivityWatch\",
224+
username
225+
)));
226+
}
220227
} else if cfg!(target_os = "macos") {
221-
PathBuf::from("/Applications/ActivityWatch.app/Contents/MacOS")
222-
} else {
223-
PathBuf::new()
224-
};
228+
if let Some(dirs) = UserDirs::new() {
229+
discovery_paths.push(dirs.home_dir().join("aw-modules"));
230+
}
231+
discovery_paths.push(PathBuf::from(
232+
"/Applications/ActivityWatch.app/Contents/MacOS",
233+
));
234+
}
225235

226236
Defaults {
227237
autostart: true,
228238
autostart_minimized: true,
229-
port: 5699, // TODO: update before going stable
230-
discovery_path,
239+
port: 5699,
240+
discovery_paths,
231241
}
232242
}
233243
}

src-tauri/src/manager.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,11 @@ fn discover_modules() -> BTreeMap<String, PathBuf> {
390390
let path = env::var_os("PATH").unwrap_or_default();
391391
let mut paths = env::split_paths(&path).collect::<Vec<_>>();
392392

393-
if !paths.contains(&config.defaults.discovery_path) {
394-
// add to the front of the path list
395-
paths.insert(0, config.defaults.discovery_path.to_owned());
393+
// check each path in discovery_paths and add it to the start of the paths list if it's not already there
394+
for path in config.defaults.discovery_paths.iter() {
395+
if !paths.contains(path) {
396+
paths.insert(0, path.to_owned());
397+
}
396398
}
397399

398400
// Create new PATH-like string
@@ -437,8 +439,11 @@ fn discover_modules() -> BTreeMap<String, PathBuf> {
437439
let path = env::var_os("PATH").unwrap_or_default();
438440
let mut paths = env::split_paths(&path).collect::<Vec<_>>();
439441

440-
if !paths.contains(&config.defaults.discovery_path) {
441-
paths.insert(0, config.defaults.discovery_path.to_owned());
442+
// check each path in discovery_paths and add it to the start of the paths list if it's not already there
443+
for path in config.defaults.discovery_paths.iter() {
444+
if !paths.contains(path) {
445+
paths.insert(0, path.to_owned());
446+
}
442447
}
443448

444449
let new_paths = env::join_paths(paths).unwrap_or_default();

0 commit comments

Comments
 (0)