Skip to content

Commit 7403169

Browse files
committed
Small refactors
1 parent 78a7771 commit 7403169

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

src/cli/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ pub enum Commands {
3232
/// Manage projects in the context of moxide and tmux
3333
///
3434
/// This command provides functionalities to interact with tmux sessions based on projects
35-
#[command(alias = "proj", alias = "projects")]
35+
#[command(alias = "pr", alias = "proj", alias = "projects")]
3636
Project(project::ProjectCli),
3737
/// List all moxide directories, templates and projecets
3838
#[command(alias = "ls")]
3939
List(list::ListCli),
4040
/// Save the current session into a new template
41-
#[command(alias = "save")]
41+
#[command(alias = "fre", alias = "save")]
4242
Freeze {
4343
/// The name of the saved session, if none is provided the most used directory is used
4444
#[arg(short = 'n', long)]
4545
name: Option<String>,
4646
/// The name of the new file
47-
#[arg(long, group = "file")]
47+
#[arg(alias = "file", long, group = "file")]
4848
file_name: Option<String>,
4949
/// Force overwrite existing files
5050
#[arg(short, long, default_value_t = false, group = "file")]

src/templates.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
helpers::{get_config_dir, Exit},
2+
helpers::{apply_if_some, get_config_dir, Exit},
33
widgets::table::Table,
44
};
55
use serde::{Deserialize, Serialize};
@@ -32,15 +32,17 @@ pub fn parse_template_config() -> Vec<Template> {
3232
let templates_content =
3333
fs::read_dir(get_config_dir().join("templates/")).exit(1, "Can't read template config");
3434

35-
let templates_raw: Vec<_> = templates_content
36-
.filter_map(|x| x.ok())
37-
.filter(|x| x.path().is_file())
38-
.filter_map(|x| fs::read_to_string(x.path()).ok())
39-
.collect();
35+
templates_content
36+
.filter_map(|entry| {
37+
let entry = entry.ok()?;
38+
let path = entry.path();
39+
if !path.is_file() {
40+
return None;
41+
}
4042

41-
templates_raw
42-
.iter()
43-
.filter_map(|x| serde_yaml::from_str::<Template>(x).ok())
43+
let content = fs::read_to_string(path).ok()?;
44+
serde_yaml::from_str::<Template>(&content).ok()
45+
})
4446
.collect()
4547
}
4648

@@ -53,10 +55,15 @@ pub fn apply_windows<'a>(
5355
enumerated.fold(tmux, |tmux, (window_idx, window)| {
5456
let cmd = build_tmux_command(window_idx, window, dir);
5557

56-
let layout_cmd: TmuxCommand = window.layout.as_ref().map_or_else(|| TmuxCommand::select_layout().into(), |layout| TmuxCommand::select_layout().layout_name(layout).into());
58+
let layout = window.layout.as_ref();
59+
let layout_cmd = layout.map(|layout| TmuxCommand::select_layout().layout_name(layout));
5760

5861
let tmux = tmux.add_command(cmd);
59-
add_panes_to_tmux(tmux, &window.panes, dir).add_command(layout_cmd)
62+
apply_if_some(
63+
add_panes_to_tmux(tmux, &window.panes, dir),
64+
layout_cmd,
65+
|tmux, cmd| tmux.add_command(cmd),
66+
)
6067
})
6168
}
6269

@@ -87,12 +94,10 @@ fn build_tmux_command<'a>(
8794
TmuxCommand::rename_window().new_name(name).into()
8895
})
8996
} else {
90-
let new_win = window
91-
.name
92-
.as_ref()
93-
.map_or_else(TmuxCommand::new_window, |name| {
94-
TmuxCommand::new_window().window_name(name)
95-
});
97+
let name = window.name.as_ref();
98+
let new_win = name.map_or_else(TmuxCommand::new_window, |name| {
99+
TmuxCommand::new_window().window_name(name)
100+
});
96101
match dir {
97102
Some(d) => new_win.start_directory(d.to_string_lossy()).into(),
98103
None => new_win.into(),

0 commit comments

Comments
 (0)