summaryrefslogtreecommitdiffstats
path: root/src/project.rs
blob: 7d9357b53420f539e841b65e483846bcd4c227f1 (plain)
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
use std::{
    collections::{hash_map::Entry, HashMap},
    fmt::Display,
    io::{BufWriter, Write},
    ops::{Deref, DerefMut},
    path::PathBuf,
    time::{Duration, SystemTime},
};

use tracing::trace;

use crate::{parser::FilterMap, path::PathMatcher, tmux::Tmux};

#[derive(Default)]
pub struct Projects {
    inner: HashMap<PathBuf, Duration>,
    filters: Vec<Box<dyn FilterMap>>,
    excludes: Vec<PathBuf>,
    mtime: bool,
}

impl Projects {
    pub fn new(mtime: bool, excludes: Vec<PathBuf>) -> Self {
        Self {
            mtime,
            excludes,
            ..Default::default()
        }
    }

    pub fn add_filter<T: FilterMap + 'static>(&mut self, filter: T) {
        self.filters.push(Box::new(filter))
    }

    pub fn insert(&mut self, item: Project) {
        let span = tracing::trace_span!("Entry", ?item);
        let _guard = span.enter();

        if self.excludes.iter().any(|p| &item.path_buf == p) {
            return;
        }

        match self.inner.entry(item.path_buf) {
            Entry::Occupied(mut occupied) if &item.timestamp > occupied.get() => {
                trace!(?occupied, new_value=?item.timestamp, "New entry is more recent, replacing");
                occupied.insert(item.timestamp);
            }
            Entry::Occupied(occupied) => {
                trace!(?occupied, new_value=?item.timestamp, "Previous entry is more recent, skipping");
            }
            Entry::Vacant(v) => {
                trace!(?item.timestamp, "No previous entry exists, inserting");
                v.insert(item.timestamp);
            }
        }
    }

    pub fn write<W: Write>(&self, writer: W) -> Result<(), std::io::Error> {
        let mut writer = BufWriter::new(writer);
        let mut projects: Vec<Project> = self.inner.iter().map(Project::from).collect();

        projects.sort();

        projects
            .into_iter()
            .try_for_each(|project| writeln!(writer, "{project}"))
    }
}

impl Deref for Projects {
    type Target = HashMap<PathBuf, Duration>;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl DerefMut for Projects {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl Extend<PathBuf> for Projects {
    fn extend<T>(&mut self, iter: T)
    where
        T: IntoIterator<Item = PathBuf>,
    {
        for path_buf in iter {
            if let Some(project) = self.filters.filter_map(path_buf.to_owned()) {
                self.insert(project)
            } else if self.mtime {
                if let Ok(project) = Project::try_from(path_buf) {
                    self.insert(project)
                }
            }
        }
    }
}

impl Extend<Project> for Projects {
    fn extend<T>(&mut self, iter: T)
    where
        T: IntoIterator<Item = Project>,
    {
        for project in iter.into_iter() {
            self.insert(project)
        }
    }
}

impl From<crate::config::Projects> for Projects {
    fn from(mut value: crate::config::Projects) -> Self {
        let mut filters: Vec<Box<dyn FilterMap>> = Vec::new();

        if let Some(pattern) = &value.pattern {
            filters.push(Box::new(PathMatcher(pattern.to_owned())));
        }

        if value.tmux {
            filters.push(Box::new(Tmux));
        }

        #[cfg(feature = "git")]
        if value.git {
            filters.push(Box::new(crate::git::Git));
        }

        if value.exclude_cwd {
            if let Ok(path) = std::env::current_dir() {
                value.excludes.push(path)
            }
        }

        Self {
            filters,
            excludes: value.excludes,
            mtime: value.mtime,
            ..Default::default()
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Project {
    pub timestamp: Duration,
    pub path_buf: PathBuf,
}

impl Display for Project {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.path_buf.to_string_lossy())
    }
}

impl From<(PathBuf, Duration)> for Project {
    fn from((path_buf, timestamp): (PathBuf, Duration)) -> Self {
        Self {
            timestamp,
            path_buf,
        }
    }
}

impl From<(&PathBuf, &Duration)> for Project {
    fn from((path_buf, &timestamp): (&PathBuf, &Duration)) -> Self {
        Self {
            timestamp,
            path_buf: path_buf.to_owned(),
        }
    }
}

impl TryFrom<PathBuf> for Project {
    type Error = std::io::Error;

    fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
        let timestamp = value
            .metadata()?
            .modified()?
            .duration_since(SystemTime::UNIX_EPOCH)
            .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err.to_string()))?;

        Ok(Self {
            path_buf: value,
            timestamp,
        })
    }
}