-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathgit_repos.rs
200 lines (178 loc) · 6.15 KB
/
git_repos.rs
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
use devicons::FileIcon;
use directories::BaseDirs;
use ignore::overrides::OverrideBuilder;
use lazy_static::lazy_static;
use rustc_hash::{FxBuildHasher, FxHashSet};
use std::collections::HashSet;
use std::path::PathBuf;
use tokio::task::JoinHandle;
use tracing::debug;
use crate::channels::OnAir;
use crate::entry::{Entry, PreviewCommand, PreviewType};
use television_fuzzy::matcher::{config::Config, injector::Injector, Matcher};
use television_utils::files::{walk_builder, DEFAULT_NUM_THREADS};
pub struct Channel {
matcher: Matcher<String>,
icon: FileIcon,
crawl_handle: JoinHandle<()>,
selected_entries: FxHashSet<Entry>,
}
impl Channel {
pub fn new() -> Self {
let matcher = Matcher::new(Config::default().match_paths(true));
let base_dirs = BaseDirs::new().unwrap();
let crawl_handle = tokio::spawn(crawl_for_repos(
base_dirs.home_dir().to_path_buf(),
matcher.injector(),
));
Channel {
matcher,
icon: FileIcon::from("git"),
crawl_handle,
selected_entries: HashSet::with_hasher(FxBuildHasher),
}
}
}
impl Default for Channel {
fn default() -> Self {
Self::new()
}
}
lazy_static! {
static ref PREVIEW_COMMAND: PreviewCommand = PreviewCommand {
command: String::from(
"cd {} && git log -n 200 --pretty=medium --all --graph --color",
),
delimiter: ":".to_string(),
};
}
impl OnAir for Channel {
fn find(&mut self, pattern: &str) {
self.matcher.find(pattern);
}
fn results(&mut self, num_entries: u32, offset: u32) -> Vec<Entry> {
self.matcher.tick();
self.matcher
.results(num_entries, offset)
.into_iter()
.map(|item| {
let path = item.matched_string;
Entry::new(
path.clone(),
PreviewType::Command(PREVIEW_COMMAND.clone()),
)
.with_name_match_ranges(&item.match_indices)
.with_icon(self.icon)
})
.collect()
}
fn get_result(&self, index: u32) -> Option<Entry> {
self.matcher.get_result(index).map(|item| {
let path = item.matched_string;
Entry::new(
path.clone(),
PreviewType::Command(PREVIEW_COMMAND.clone()),
)
.with_icon(self.icon)
})
}
fn selected_entries(&self) -> &FxHashSet<Entry> {
&self.selected_entries
}
fn toggle_selection(&mut self, entry: &Entry) {
if self.selected_entries.contains(entry) {
self.selected_entries.remove(entry);
} else {
self.selected_entries.insert(entry.clone());
}
}
fn result_count(&self) -> u32 {
self.matcher.matched_item_count
}
fn total_count(&self) -> u32 {
self.matcher.total_item_count
}
fn running(&self) -> bool {
self.matcher.status.running
}
fn shutdown(&self) {
debug!("Shutting down git repos channel");
self.crawl_handle.abort();
}
}
fn get_ignored_paths() -> Vec<PathBuf> {
let mut ignored_paths = Vec::new();
if let Some(base_dirs) = BaseDirs::new() {
let home = base_dirs.home_dir();
#[cfg(target_os = "macos")]
{
ignored_paths.push(home.join("Library"));
ignored_paths.push(home.join("Applications"));
ignored_paths.push(home.join("Music"));
ignored_paths.push(home.join("Pictures"));
ignored_paths.push(home.join("Movies"));
ignored_paths.push(home.join("Downloads"));
ignored_paths.push(home.join("Public"));
}
#[cfg(target_os = "linux")]
{
ignored_paths.push(home.join(".cache"));
ignored_paths.push(home.join(".config"));
ignored_paths.push(home.join(".local"));
ignored_paths.push(home.join(".thumbnails"));
ignored_paths.push(home.join("Downloads"));
ignored_paths.push(home.join("Public"));
ignored_paths.push(home.join("snap"));
ignored_paths.push(home.join(".snap"));
}
#[cfg(target_os = "windows")]
{
ignored_paths.push(home.join("AppData"));
ignored_paths.push(home.join("Downloads"));
ignored_paths.push(home.join("Documents"));
ignored_paths.push(home.join("Music"));
ignored_paths.push(home.join("Pictures"));
ignored_paths.push(home.join("Videos"));
}
// Common paths to ignore for all platforms
ignored_paths.push(home.join("node_modules"));
ignored_paths.push(home.join("venv"));
ignored_paths.push(PathBuf::from("/tmp"));
}
ignored_paths
}
#[allow(clippy::unused_async)]
async fn crawl_for_repos(starting_point: PathBuf, injector: Injector<String>) {
let mut walker_overrides_builder = OverrideBuilder::new(&starting_point);
walker_overrides_builder.add(".git").unwrap();
let walker = walk_builder(
&starting_point,
*DEFAULT_NUM_THREADS,
Some(walker_overrides_builder.build().unwrap()),
Some(get_ignored_paths()),
)
.build_parallel();
walker.run(|| {
let injector = injector.clone();
Box::new(move |result| {
if let Ok(entry) = result {
if entry.file_type().unwrap().is_dir() {
// if the entry is a .git directory, add its parent to the list of git repos
if entry.path().ends_with(".git") {
let parent_path =
&entry.path().parent().unwrap().to_string_lossy();
debug!("Found git repo: {:?}", parent_path);
let () = injector.push(
parent_path.to_string(),
|e, cols| {
cols[0] = e.clone().into();
},
);
return ignore::WalkState::Skip;
}
}
}
ignore::WalkState::Continue
})
});
}