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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use std::path::PathBuf;
use log::{debug, error, info, warn, LevelFilter};
use crate::cli::Cli;
use crate::config::{TomlConfigFile, VimVariant, ENVIRONMENT, SUPPORTED_VIM_VARIATIONS};
use crate::error::VsmRuntimeFault;
use crate::logger::StdoutLog;
use crate::ui::UserPromptRenderer;
use crate::utils::{CommandExecutor, FilesystemManager};
#[derive(Debug)]
pub struct VimSessionManager {
cli: Cli,
config_file_struct: TomlConfigFile,
fs: FilesystemManager,
shell: CommandExecutor,
prompt: UserPromptRenderer,
first_run: bool,
}
impl Default for VimSessionManager {
fn default() -> Self {
Self::new()
}
}
impl VimSessionManager {
#[must_use]
pub fn new() -> Self {
let cli = Cli::new();
let level_filter: LevelFilter = if *cli.debug_mode() {
LevelFilter::Debug
} else {
LevelFilter::Info
};
StdoutLog::init(level_filter);
Self {
cli,
config_file_struct: TomlConfigFile::default(),
fs: FilesystemManager::new(
ENVIRONMENT.path().vsm_config_dir(),
ENVIRONMENT.path().vsm_config_file(),
ENVIRONMENT.var().vim_sessions(),
),
shell: CommandExecutor::new(),
prompt: UserPromptRenderer::new(),
first_run: true,
}
}
pub fn run(&mut self) -> Result<(), VsmRuntimeFault> {
self.setup()?;
self.subcommand_dispatcher()?;
Ok(())
}
fn setup(&mut self) -> Result<(), VsmRuntimeFault> {
if self.fs.config_file_exists() {
self.first_run = false;
self.config_file_struct = self.fs.read_config()?;
} else {
warn!("No config file detected");
self.select_vim_variation()?
}
Ok(())
}
fn select_vim_variation(&mut self) -> Result<(), VsmRuntimeFault> {
let mut installed_variations: Vec<String> = vec![];
let mut variants_not_installed_error_msg = String::new();
for variant in SUPPORTED_VIM_VARIATIONS.keys() {
if self.shell.is_installed(variant) {
installed_variations.push((*variant).to_owned());
} else {
variants_not_installed_error_msg.push_str(format!("{}, ", variant).as_str());
}
}
if installed_variations.is_empty() {
return Err(VsmRuntimeFault::NoSupportedVimVariantFound {
msg: variants_not_installed_error_msg,
});
}
if !self.first_run {
info!(
"Current active variant is => {}",
self.config_file_struct.vim_variant().active_variant()
);
}
match self.prompt.vim_variant(installed_variations) {
Ok(choice) => {
if self.first_run
|| *self.config_file_struct.vim_variant().active_variant() != choice
{
let shell_command = SUPPORTED_VIM_VARIATIONS
.get(choice.as_str())
.expect("Failed to retrieve shell_command value from Lazy loaded hashmap");
self.config_file_struct =
TomlConfigFile::new(VimVariant::new(choice, String::from(*shell_command)));
self.fs.write_config(&self.config_file_struct)?;
}
Ok(())
}
Err(e) => Err(e),
}
}
fn subcommand_dispatcher(&mut self) -> Result<(), VsmRuntimeFault> {
if !self.cli.variant() {
match self.fs.load_vim_session_files() {
Ok(maybe_empty_sessions) => maybe_empty_sessions.map_or_else(
|| warn!("No session files found"),
|sessions| {
if self.cli.list() {
self.list(&sessions);
}
if self.cli.open() {
if let Err(e) = self.open(&sessions) {
error!("{}", e);
}
}
if self.cli.remove() {
if let Err(e) = self.remove(&sessions) {
error!("{}", e);
}
}
},
),
Err(e) => error!("{}", e.to_string()),
}
} else if !self.first_run {
self.variant()?;
}
Ok(())
}
fn list(&mut self, sessions: &Vec<PathBuf>) {
debug!("Listing all sessions");
for session in sessions {
if let Some(file) = session.file_stem() {
info!("{}", file.to_string_lossy());
}
}
}
fn open(&mut self, sessions: &Vec<PathBuf>) -> Result<(), VsmRuntimeFault> {
debug!("Opening a session");
match self.prompt.session_open(sessions) {
Ok(choice) => {
for session in sessions {
if let Some(file) = session.file_stem() {
if choice == file.to_string_lossy() {
self.shell.open_editor_with_session(
self.config_file_struct.vim_variant().active_variant(),
self.config_file_struct.vim_variant().shell_command(),
&session.to_string_lossy().to_string(),
)?;
}
}
}
}
Err(e) => return Err(e),
}
Ok(())
}
fn remove(&mut self, sessions: &Vec<PathBuf>) -> Result<(), VsmRuntimeFault> {
debug!("Removing stale sessions");
match self.prompt.session_remove(sessions) {
Ok(selected_sessions) => {
for session in sessions {
for selected in &selected_sessions {
if let Some(s) = session.file_stem() {
if s.to_string_lossy() == *selected {
info!("Removing => {}", s.to_string_lossy());
self.fs.remove_file(session)?
}
}
}
}
}
Err(e) => return Err(e),
}
Ok(())
}
fn variant(&mut self) -> Result<(), VsmRuntimeFault> {
debug!("Updating users vim variant selection");
self.select_vim_variation()?;
info!("Succesfully updated the active vim variant");
Ok(())
}
}