-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathalias.rs
177 lines (151 loc) · 4.92 KB
/
alias.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
use std::collections::HashSet;
use crate::channels::OnAir;
use crate::entry::Entry;
use crate::entry::PreviewType;
use devicons::FileIcon;
use rustc_hash::FxBuildHasher;
use rustc_hash::FxHashSet;
use television_fuzzy::matcher::{config::Config, injector::Injector, Matcher};
use television_utils::indices::sep_name_and_value_indices;
use tracing::debug;
#[derive(Debug, Clone)]
struct Alias {
name: String,
value: String,
}
impl Alias {
fn new(name: String, value: String) -> Self {
Self { name, value }
}
}
pub struct Channel {
matcher: Matcher<Alias>,
file_icon: FileIcon,
selected_entries: FxHashSet<Entry>,
}
const NUM_THREADS: usize = 1;
const FILE_ICON_STR: &str = "nu";
const SHELL_ENV_VAR: &str = "SHELL";
fn get_current_shell() -> Option<String> {
std::env::var(SHELL_ENV_VAR).ok()
}
fn get_raw_aliases(shell: &str) -> Vec<String> {
let output = std::process::Command::new(shell)
.arg("-i")
.arg("-c")
.arg("alias")
.output()
.expect("failed to execute process");
let aliases = String::from_utf8_lossy(&output.stdout);
aliases.lines().map(ToString::to_string).collect()
}
impl Channel {
pub fn new() -> Self {
let matcher = Matcher::new(Config::default().n_threads(NUM_THREADS));
let injector = matcher.injector();
tokio::spawn(load_aliases(injector));
Self {
matcher,
file_icon: FileIcon::from(FILE_ICON_STR),
selected_entries: HashSet::with_hasher(FxBuildHasher),
}
}
}
impl Default for Channel {
fn default() -> Self {
Self::new()
}
}
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 (
name_indices,
value_indices,
should_add_name_indices,
should_add_value_indices,
) = sep_name_and_value_indices(
&mut item.match_indices.iter().map(|i| i.0).collect(),
u32::try_from(item.inner.name.len()).unwrap(),
);
let mut entry =
Entry::new(item.inner.name.clone(), PreviewType::EnvVar)
.with_value(item.inner.value)
.with_icon(self.file_icon);
if should_add_name_indices {
let name_indices: Vec<(u32, u32)> =
name_indices.into_iter().map(|i| (i, i + 1)).collect();
entry = entry.with_name_match_ranges(&name_indices);
}
if should_add_value_indices {
let value_indices: Vec<(u32, u32)> = value_indices
.into_iter()
.map(|i| (i, i + 1))
.collect();
entry = entry.with_value_match_ranges(&value_indices);
}
entry
})
.collect()
}
fn get_result(&self, index: u32) -> Option<Entry> {
self.matcher.get_result(index).map(|item| {
Entry::new(item.inner.name.clone(), PreviewType::EnvVar)
.with_value(item.inner.value)
.with_icon(self.file_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) {}
}
#[allow(clippy::unused_async)]
async fn load_aliases(injector: Injector<Alias>) {
let raw_shell = get_current_shell().unwrap_or("bash".to_string());
let shell = raw_shell.split('/').last().unwrap();
debug!("Current shell: {}", shell);
let raw_aliases = get_raw_aliases(shell);
raw_aliases
.iter()
.filter_map(|alias| {
let mut parts = alias.split('=');
if let Some(name) = parts.next() {
if let Some(value) = parts.next() {
return Some(Alias::new(
name.to_string(),
value.to_string(),
));
}
}
None
})
.for_each(|alias| {
let () = injector.push(alias.clone(), |_, cols| {
cols[0] = (alias.name.clone() + &alias.value).into();
});
});
}