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
use std::path::PathBuf;
use prettytable::{cell, row, Cell, Row, Table};
use semver::VersionReq;
use crate::{
constants::EXECUTABLE_NAME,
toolchain::{
find_installed_toolchains, installed::InstalledToolchain, is_a_custom_install,
SelectedToolchain, ToolchainFile,
},
utils::directory::PycorsPathsProviderFromEnv,
Result,
};
pub fn run() -> Result<()> {
let paths_provider = PycorsPathsProviderFromEnv::new();
let installed_toolchains: Vec<InstalledToolchain> = find_installed_toolchains(&paths_provider)?;
let mut toolchains_table = ToolChainTable::new(&installed_toolchains);
if let Some(toolchain_file) = ToolchainFile::load()? {
let selected_toolchain =
SelectedToolchain::from_toolchain_file(&toolchain_file, &installed_toolchains);
toolchains_table.append(&selected_toolchain, true);
}
toolchains_table.printstd();
Ok(())
}
struct ToolChainTableLine {
active: bool,
version: Option<VersionReq>,
custom_install: bool,
location: Option<PathBuf>,
installed: bool,
}
struct ToolChainTable(Vec<ToolChainTableLine>);
impl ToolChainTable {
fn new(installed_toolchains: &[InstalledToolchain]) -> ToolChainTable {
let list: Vec<ToolChainTableLine> = installed_toolchains
.iter()
.map(|t| ToolChainTableLine {
active: false,
version: Some(VersionReq::exact(&t.version)),
custom_install: t.is_custom_install(),
location: Some(t.location.clone()),
installed: true,
})
.collect();
ToolChainTable(list)
}
fn append(&mut self, toolchain: &SelectedToolchain, active: bool) {
match self.0.iter_mut().find(|t| match (&t.version, &t.location) {
(None, _) => false,
(_, None) => false,
(Some(version), Some(location)) => {
toolchain.same_location(&location) && toolchain.same_version(&version)
}
}) {
Some(installed_toolchain_line) => {
installed_toolchain_line.active = active;
}
None => {
let line: ToolChainTableLine = match toolchain {
SelectedToolchain::InstalledToolchain(t) => ToolChainTableLine {
active,
version: Some(VersionReq::exact(&t.version)),
custom_install: is_a_custom_install(&t.location),
location: Some(t.location.clone()),
installed: true,
},
SelectedToolchain::NotInstalledToolchain(t) => ToolChainTableLine {
active,
version: t.version.clone(),
custom_install: t
.location
.as_ref()
.map(|p| is_a_custom_install(&p))
.unwrap_or(false),
location: t.location.clone(),
installed: false,
},
};
self.0.insert(0, line);
}
}
}
}
impl ToolChainTable {
fn printstd(&self) {
let mut table = Table::new();
table.add_row(row![
"Active",
"Version",
&format!("Installed by {}", EXECUTABLE_NAME),
"Location"
]);
let green = prettytable::Attr::ForegroundColor(prettytable::color::GREEN);
let red = prettytable::Attr::ForegroundColor(prettytable::color::RED);
let bold = prettytable::Attr::Bold;
self.0.iter().for_each(|t: &ToolChainTableLine| {
let (active_char, line_color, line_style) = match (t.active, t.installed) {
(true, true) => ("โ", Some(green), Some(bold)),
(true, false) => ("โ", Some(red), Some(bold)),
(false, _) => ("", None, None),
};
let custom_char = if t.custom_install { "โ" } else { "" };
let mut col_1 = Cell::new_align(active_char, prettytable::format::Alignment::CENTER);
let mut col_2 = Cell::new_align(
&t.version
.as_ref()
.map(|t| format!("{}", t).replace("= ", ""))
.unwrap_or_default(),
prettytable::format::Alignment::CENTER,
);
let mut col_3 = Cell::new_align(&custom_char, prettytable::format::Alignment::CENTER);
let mut col_4 = Cell::new_align(
&t.location
.as_ref()
.map(|t| format!("{}", t.display()))
.unwrap_or_default(),
prettytable::format::Alignment::LEFT,
);
if let Some(c) = line_color {
col_1.style(c);
col_2.style(c);
col_3.style(c);
col_4.style(c);
}
if let Some(c) = line_style {
col_1.style(c);
col_2.style(c);
col_3.style(c);
col_4.style(c);
}
table.add_row(Row::new(vec![col_1, col_2, col_3, col_4]));
});
table.printstd();
}
}