sessionizer 0.3.2

Tmux session manager
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
use std::{
    env,
    ffi::OsStr,
    fmt::Debug,
    ops::Range,
    os::unix::ffi::OsStrExt,
    path::{Path, PathBuf},
    process::Command,
    sync::mpsc::SyncSender,
};

use ignore::{DirEntry, WalkBuilder, WalkState};
use onlyerror::Error;

use crate::{debug, info, trace, warn, Entry, Project, Result};

#[derive(Clone, Debug, Default, PartialEq, Eq)]
struct SearchPath {
    path: PathBuf,
    depth: Range<usize>,
}

impl SearchPath {
    fn new(path: impl Into<PathBuf>, depth: Range<usize>) -> Self {
        Self {
            path: path.into(),
            depth,
        }
    }
}

#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Error)]
pub enum ProjectError {
    #[error("{0}")]
    ParsePathError(#[from] ParsePathError),
    #[error("Path `{0:?}` could not be canonicalized: {1}")]
    CanonicalizationFailed(PathBuf, #[source] std::io::Error),
}

#[allow(clippy::module_name_repetitions)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Error)]
pub enum ParsePathError {
    #[error("Invalid path component after `_`, only `_` or `*` are allowed")]
    ComponentAfterUnderscore,
    #[error("Invalid path component after `*`, only `*` is allowed")]
    ComponentAfterStar,
    #[error("The path may not begin with `_` or `*`.")]
    PathBeginning,
    #[error("The path may not end with `_`.")]
    PathEnd,
    #[error("The path must not be empty")]
    Empty,
    #[error("The path must be absolute")]
    PathNotAbsolute,
    #[error("Unknown home directory to do tilde expansion")]
    UnknownHomeDirectory,
}

pub fn find_projects(tx: &SyncSender<Entry>) -> Result<(), ProjectError> {
    let paths = read_sessionizer_path()
        .or_else(read_cdpath)
        .or_else(query_zoxide)
        .or_else(use_last_resort)
        .transpose()?
        .unwrap_or_default();

    if paths.is_empty() {
        info!(concat!(
            "Could not find any paths to search for projects.",
            "This could be due to a missing $HOME.",
            "If configured, $SESSIONIZER_PATH or $CDPATH are also used, in that order."
        ));
        return Ok(());
    };

    find_projects_in(paths, tx);

    Ok(())
}

fn read_sessionizer_path() -> Option<Result<Vec<SearchPath>, ProjectError>> {
    env::var_os("SESSIONIZER_PATH").map(|path| parse_paths(env::split_paths(&path), false))
}

fn read_cdpath() -> Option<Result<Vec<SearchPath>, ProjectError>> {
    env::var_os("CDPATH").map(|path| parse_paths(env::split_paths(&path), true))
}

fn query_zoxide() -> Option<Result<Vec<SearchPath>, ProjectError>> {
    Command::new("zoxide")
        .arg("query")
        .arg("--list")
        .output()
        .ok()
        .map(|o| {
            Ok(o.stdout
                .split(|c| *c == b'\n')
                .filter_map(|p| {
                    let path = OsStr::from_bytes(p);
                    if path.is_empty() {
                        return None;
                    }

                    let path = Path::new(path);
                    let path = path.canonicalize().ok()?;

                    if path.is_relative() {
                        warn!("Found relative path in zoxide output: {p:?}");
                        return None;
                    }

                    Some(path)
                })
                .map(|p| SearchPath::new(p, 0..1))
                .collect())
        })
}

fn use_last_resort() -> Option<Result<Vec<SearchPath>, ProjectError>> {
    warn!("No SESSIONIZER_PATH set, showing only ~/.config as result.");
    let home = home_dir()?;
    Some(Ok(vec![SearchPath::new(home.join(".config"), 1..2)]))
}

#[cfg(not(test))]
fn home_dir() -> Option<&'static Path> {
    use std::sync::OnceLock;

    static HOME: OnceLock<Option<PathBuf>> = OnceLock::new();

    HOME.get_or_init(|| {
        home::home_dir().map_or_else(
            || {
                info!(concat!(
                    "Could not find the home directory.",
                    "This can impact the projects that are found.",
                    "That is, tilde expansion and fallback paths are not available."
                ));
                None
            },
            Some,
        )
    })
    .as_deref()
}

#[cfg(test)]
#[allow(clippy::unnecessary_wraps)]
fn home_dir() -> Option<&'static Path> {
    use home as _;
    Some(Path::new("/home/morpheus"))
}

fn parse_paths(
    paths: impl IntoIterator<Item = PathBuf>,
    auto_star: bool,
) -> Result<Vec<SearchPath>, ProjectError> {
    paths
        .into_iter()
        .filter_map(|p| match parse_path(&p, auto_star) {
            Ok(path) => Some(
                path.path
                    .canonicalize()
                    .map(|p| SearchPath { path: p, ..path })
                    .map_err(|e| ProjectError::CanonicalizationFailed(path.path, e)),
            ),
            Err(ParsePathError::PathNotAbsolute) if auto_star => {
                debug!(path =% p.display(), "Skipping path because it us not absolute.");
                None
            }
            Err(e) => Some(Err(ProjectError::ParsePathError(e))),
        })
        .collect::<Result<Vec<_>, _>>()
}

fn parse_path(path: &Path, auto_star: bool) -> Result<SearchPath, ParsePathError> {
    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    enum State {
        Beginning,
        Regular,
        Underscore,
        Star,
    }

    let all_components = tokenize(path);

    let mut path = PathBuf::with_capacity(path.as_os_str().len());
    let mut min_depth = 0;
    let mut search_depth = 1;
    let mut state = State::Beginning;

    for component in all_components {
        match (state, component) {
            (State::Beginning, Token::Underscore | Token::Star) => {
                return Err(ParsePathError::PathBeginning);
            }
            (State::Beginning, Token::End) => {
                return Err(ParsePathError::Empty);
            }
            (State::Beginning, Token::Regular(component)) => {
                if component == "~" {
                    let home = home_dir().ok_or(ParsePathError::UnknownHomeDirectory)?;
                    path.push(home);
                } else {
                    path.push(component);
                }
                state = State::Regular;
            }
            (State::Regular, Token::Regular(component)) => {
                path.push(component);
            }
            (State::Regular, Token::Underscore) => {
                min_depth = 2;
                state = State::Underscore;
            }
            (State::Regular, Token::Star) => {
                min_depth = 1;
                state = State::Star;
            }
            (State::Regular, Token::End) => {
                min_depth = usize::from(auto_star);
            }
            (State::Underscore, Token::Regular(_)) => {
                return Err(ParsePathError::ComponentAfterUnderscore);
            }
            (State::Underscore, Token::Underscore) => {
                min_depth += 1;
            }
            (State::Underscore, Token::Star) => {
                state = State::Star;
            }
            (State::Underscore, Token::End) => {
                if !auto_star {
                    return Err(ParsePathError::PathEnd);
                }
            }
            (State::Star, Token::Underscore | Token::Regular(_)) => {
                return Err(ParsePathError::ComponentAfterStar);
            }
            (State::Star, Token::Star) => {
                search_depth += 1;
            }
            (State::Star, Token::End) => {}
        };
    }

    if path.is_relative() {
        return Err(ParsePathError::PathNotAbsolute);
    }

    Ok(SearchPath {
        path,
        depth: min_depth..(min_depth + search_depth),
    })
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum Token<'a> {
    Regular(&'a OsStr),
    Underscore,
    Star,
    End,
}

fn tokenize(path: &Path) -> impl Iterator<Item = Token<'_>> {
    path.iter()
        .map(|component| {
            if component == "_" {
                Token::Underscore
            } else if component == "*" {
                Token::Star
            } else if component == r"\_" {
                Token::Regular(OsStr::new("_"))
            } else if component == r"\*" {
                Token::Regular(OsStr::new("*"))
            } else {
                Token::Regular(component)
            }
        })
        .chain(std::iter::once(Token::End))
}

fn find_projects_in(mut paths: Vec<SearchPath>, tx: &SyncSender<Entry>) {
    debug!(?paths, "Searching for projects in these locations");

    let mut walker = None::<(WalkBuilder, usize)>;

    paths.retain_mut(|path| {
        if path.depth == (0..1) {
            if let Some(project) = accept_dir(std::mem::take(&mut path.path), 0) {
                let _ = tx.send(Entry::Project(project));
            };
            return false;
        }
        if let Some((walker, max_depth)) = walker.as_mut() {
            let _ = walker.add(&path.path);
            if path.depth.end > *max_depth {
                *max_depth = path.depth.end;
            }
        } else {
            let w = WalkBuilder::new(&path.path);
            walker = Some((w, path.depth.end));
        };
        true
    });

    if let Some((mut walker, max_depth)) = walker {
        let walker = walker
            .max_depth(Some(max_depth))
            .same_file_system(true)
            .follow_links(false)
            .filter_entry(|e| {
                e.file_type()
                    .is_some_and(|ft| ft.is_dir() || ft.is_symlink())
            })
            .build_parallel();

        let handler = |result| process_dir_entry(&paths, tx, result);
        walker.run(move || Box::new(handler));
    }
}

fn process_dir_entry(
    paths: &[SearchPath],
    tx: &SyncSender<Entry>,
    entry: Result<DirEntry, ignore::Error>,
) -> WalkState {
    match entry {
        Ok(entry) => match process_dir(paths, entry.depth(), entry.into_path()) {
            DirResult::Ignored(state) => state,
            DirResult::Handle(entry, state) => {
                if tx.send(entry).is_err() {
                    WalkState::Quit
                } else {
                    state
                }
            }
        },
        Err(e) => {
            warn!("failed to walk entry: {}", e);
            WalkState::Skip
        }
    }
}

enum DirResult {
    Ignored(WalkState),
    Handle(Entry, WalkState),
}

fn process_dir(paths: &[SearchPath], depth: usize, path: PathBuf) -> DirResult {
    trace!(path =% path.display(), depth, "Processing directory");

    if depth == 0 {
        return DirResult::Ignored(WalkState::Continue);
    }

    let search_depth = paths
        .iter()
        .find_map(|p| path.starts_with(&p.path).then_some(&p.depth));

    let at_end = if let Some(search_depth) = search_depth {
        if depth < search_depth.start {
            trace!(path =% path.display(), depth, concat!(
                "Continue traversing but not searching the path because ",
                "it's min depth has not been reached yet."
            ));
            return DirResult::Ignored(WalkState::Continue);
        }
        if depth >= search_depth.end {
            debug!(path =% path.display(), depth, concat!(
                "Stopping recursing the path because ",
                "it's max depth has been exceeded (this should not happen)."
            ));
            return DirResult::Ignored(WalkState::Skip);
        }
        depth + 1 == search_depth.end
    } else {
        false
    };

    let git_path = path.join(".git");
    let state = if git_path.exists() {
        debug!(
            path =% path.display(),
            depth,
            "Stopping recursing the path because it is a git repository."
        );
        WalkState::Skip
    } else if at_end {
        trace!(
            path =% path.display(),
            depth,
            "Stopping recursing the path because it's max depth has been reached."
        );
        WalkState::Skip
    } else {
        WalkState::Continue
    };

    let Some(project) = accept_dir(path, depth) else {
        return DirResult::Ignored(WalkState::Continue);
    };

    let project = Entry::Project(project);
    DirResult::Handle(project, state)
}

fn accept_dir(path: PathBuf, depth: usize) -> Option<Project> {
    trace!(path =% path.display(), depth, "accepting directory");

    let name = path
        .file_name()
        .and_then(|name| name.to_str())
        .map(|name| name.replace('.', "_"));

    let Some(name) = name else {
        warn!(debug_path =? path, display_path =% path.display() , concat!("Could not ",
        "get a path from the ",
        "directory to sugges",
        "t as a project"));
        return None;
    };

    let depth = depth.max(1);

    let search_path = {
        let mut path = path
            .iter()
            .rev()
            .take(depth)
            .filter_map(|p| p.to_str())
            .collect::<Vec<_>>();
        path.reverse();
        path.join("/")
    };

    let project = Project {
        root: path,
        name,
        search_path,
        depth,
    };

    Some(project)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_path_regular() {
        test_parse_path("/dir", SearchPath::new("/dir", 0..1));
    }

    #[test]
    fn parse_path_star() {
        test_parse_path("/dir/*", SearchPath::new("/dir", 1..2));
    }

    #[test]
    fn parse_path_underscore() {
        let result = parse_path(Path::new("/dir/_"), false);
        assert_eq!(result.unwrap_err(), ParsePathError::PathEnd);
    }

    #[test]
    fn parse_path_underscore_star() {
        test_parse_path("/dir/_/*", SearchPath::new("/dir", 2..3));
    }

    #[test]
    fn parse_path_underscore_underscore_star() {
        test_parse_path("/dir/_/_/*", SearchPath::new("/dir", 3..4));
    }

    #[test]
    fn parse_path_star_star() {
        test_parse_path("/dir/*/*", SearchPath::new("/dir", 1..3));
    }

    #[test]
    fn parse_path_escaped_star() {
        test_parse_path("/dir/\\*", SearchPath::new("/dir/*", 0..1));
    }

    #[test]
    fn parse_path_escaped_underscore() {
        test_parse_path("/dir/\\_", SearchPath::new("/dir/_", 0..1));
    }

    #[test]
    fn parse_path_tilde_expansion() {
        test_parse_path("~/dir", SearchPath::new("/home/morpheus/dir", 0..1));
    }

    #[test]
    fn parse_path_no_tilde_expansion() {
        test_parse_path("/dir/~", SearchPath::new("/dir/~", 0..1));
    }

    #[test]
    fn parse_path_auto_star_regular() {
        test_parse_path_auto_star("/dir", SearchPath::new("/dir", 1..2));
    }

    #[test]
    fn parse_path_auto_star_star() {
        test_parse_path_auto_star("/dir/*", SearchPath::new("/dir", 1..2));
    }

    #[test]
    fn parse_path_auto_star_underscore() {
        test_parse_path_auto_star("/dir/_", SearchPath::new("/dir", 2..3));
    }

    #[test]
    fn parse_path_auto_star_underscore_underscore() {
        test_parse_path_auto_star("/dir/_/_", SearchPath::new("/dir", 3..4));
    }

    #[test]
    fn parse_path_auto_star_underscore_star() {
        test_parse_path_auto_star("/dir/_/*", SearchPath::new("/dir", 2..3));
    }

    #[test]
    fn parse_path_empty() {
        test_parse_path_error("", ParsePathError::Empty);
    }

    #[test]
    fn parse_path_regular_after_underscore() {
        test_parse_path_error("/dir/_/dir", ParsePathError::ComponentAfterUnderscore);
    }

    #[test]
    fn parse_path_regular_after_star() {
        test_parse_path_error("/dir/*/dir", ParsePathError::ComponentAfterStar);
    }

    #[test]
    fn parse_path_underscore_after_star() {
        test_parse_path_error("/dir/*/_", ParsePathError::ComponentAfterStar);
    }

    #[test]
    fn parse_path_relative_path() {
        test_parse_path_error("dir", ParsePathError::PathNotAbsolute);
        test_parse_path_error(".", ParsePathError::PathNotAbsolute);
    }

    #[allow(clippy::needless_pass_by_value)]
    fn test_parse_path(path: &str, expected: SearchPath) {
        let result = parse_path(Path::new(path), false).unwrap();
        assert_eq!(result, expected);
    }

    #[allow(clippy::needless_pass_by_value)]
    fn test_parse_path_auto_star(path: &str, expected: SearchPath) {
        let result = parse_path(Path::new(path), true).unwrap();
        assert_eq!(result, expected);
    }

    fn test_parse_path_error(path: &str, expected: ParsePathError) {
        let result = parse_path(Path::new(path), false);
        assert_eq!(result.unwrap_err(), expected);
        let result = parse_path(Path::new(path), true);
        assert_eq!(result.unwrap_err(), expected);
    }

    #[test]
    fn ignore_root_entries() {
        let result = process_dir(&[], 0, PathBuf::from("/"));
        assert!(matches!(result, DirResult::Ignored(WalkState::Continue)));
    }

    #[test]
    fn replace_invalid_chars_in_name() {
        let result = process_dir(&[], 1, PathBuf::from("/home/user/dev/project.name"));
        let DirResult::Handle(Entry::Project(project), _) = result else {
            panic!("expected project entry");
        };
        assert_eq!(project.name, "project_name");
    }

    #[test]
    fn search_path_is_the_last_depth_path_elements() {
        let result = process_dir(&[], 3, PathBuf::from("/home/user/dev/project-name/src"));
        let DirResult::Handle(Entry::Project(project), _) = result else {
            panic!("expected project entry");
        };
        assert_eq!(project.search_path, "dev/project-name/src");
    }

    #[test]
    fn skip_path_becuse_of_min_depth() {
        let result = process_dir(
            &[SearchPath::new("/home/user/dev/project-name", 2..3)],
            1,
            PathBuf::from("/home/user/dev/project-name/src"),
        );
        let DirResult::Ignored(WalkState::Continue) = result else {
            panic!("expected ignored+continue");
        };
    }

    #[test]
    fn skip_path_becuse_of_max_depth() {
        let result = process_dir(
            &[SearchPath::new("/home/user/dev/project-name", 1..2)],
            2,
            PathBuf::from("/home/user/dev/project-name/src/dir"),
        );
        let DirResult::Ignored(WalkState::Skip) = result else {
            panic!("expected ignored+skip");
        };
    }
}