forked from reujab/silver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
138 lines (128 loc) · 4.26 KB
/
main.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
mod cli;
mod config;
mod icons;
mod modules;
mod print;
mod sh;
use cli::*;
use once_cell::sync::{Lazy, OnceCell};
use std::path::{Path, PathBuf};
use sysinfo::{get_current_pid, ProcessExt, System, SystemExt};
static CONFIG_PATH: OnceCell<PathBuf> = OnceCell::new();
static CONFIG: Lazy<config::Config> = Lazy::new(|| {
match CONFIG_PATH.get() {
Some(path) => confy::load_path(path),
None => confy::load("silver", None),
}
.expect("Problem loading or serializing default config")
});
#[derive(Clone, Debug)]
pub struct Segment {
background: String,
foreground: String,
value: String,
}
impl Default for Segment {
fn default() -> Self {
Self {
background: "none".to_owned(),
foreground: "none".to_owned(),
value: String::new(),
}
}
}
fn main() {
let sys = System::new_all();
let process = sys.get_process(get_current_pid().unwrap()).unwrap();
let parent = sys.get_process(process.parent().unwrap()).unwrap();
let shell = std::env::var("SILVER_SHELL")
.map(|s| s)
.unwrap_or_else(|_| {
let shell = parent.name().trim();
let shell = shell.strip_suffix(".exe").unwrap_or(shell);
shell.strip_prefix("-").unwrap_or(shell).to_owned()
});
let opt = cli::Silver::parse();
if let Some(path) = opt.config {
let path = Path::new(path.as_str()).canonicalize().unwrap();
CONFIG_PATH.set(path).unwrap()
}
match opt.cmd {
Command::Init => {
print!(
"{}",
match shell.as_str() {
"bash" => include_str!("init.bash"),
"powershell" | "pwsh" => include_str!("init.ps1"),
"ion" => include_str!("init.ion"),
_ =>
panic!(
"unknown shell: \"{}\". Supported shells: bash, ion, powershell",
shell
),
}
.replace(
"silver",
format!(
"silver{}",
if let Some(path) = CONFIG_PATH.get() {
format!(" --config {}", path.display())
} else {
String::new()
}
)
.as_str()
)
)
}
Command::Lprint => {
print::prompt(&shell, &CONFIG.left, |_, (_, c, n)| {
vec![
(
c.background.to_owned(),
c.foreground.to_owned(),
format!(" {} ", c.value),
),
if n.background == c.background {
(
c.background.to_owned(),
c.foreground.to_owned(),
CONFIG.separator.left.thin.to_owned(),
)
} else {
(
n.background.to_owned(),
c.background.to_owned(),
CONFIG.separator.left.thick.to_owned(),
)
},
]
});
print!(" ");
}
Command::Rprint => {
print::prompt(&shell, &CONFIG.right, |_, (p, c, _)| {
vec![
if p.background == c.background {
(
c.background.to_owned(),
c.foreground.to_owned(),
CONFIG.separator.right.thin.to_owned(),
)
} else {
(
p.background.to_owned(),
c.background.to_owned(),
CONFIG.separator.right.thick.to_owned(),
)
},
(
c.background.to_owned(),
c.foreground.to_owned(),
format!(" {} ", c.value),
),
]
})
}
}
}