Skip to content

Commit 73f914f

Browse files
committed
chore: deps update & function cleanup
Cleaned up and rewrote a few functions; fixed a config error bug; updated for latest Rodio dependency changes
1 parent f32a4ce commit 73f914f

7 files changed

Lines changed: 104 additions & 138 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 93 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "shell_command_menu"
3-
version = "0.2.4"
3+
version = "0.2.5"
44
edition = "2024"
55

66
[dependencies]
@@ -12,11 +12,12 @@ prettytable = "0.10.0"
1212
inquire = "0.9.2"
1313
textwrap = "0.16.2"
1414
rodio = "0.21.1"
15-
tokio = { version = "1.49.0", features = ["full"] }
15+
tokio = { version = "1.49.0", features = ["rt"] }
1616
anyhow = "1.0.100"
1717
csv = "1.4.0"
1818

1919

2020
[dev-dependencies]
21+
tokio = { version = "1.49.0", features = ["macros", "rt"] }
2122
serial_test = "3.3.1"
2223
tempfile = "3.24.0"

src/changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
1/27/26 - v0.2.5
4+
Cleaned up and rewrote a few functions; fixed a config error bug; updated for latest Rodio dependency changes
5+
36
7/4/25
47
Updated formatting for Rust 1.88 and Clippy
58

src/config.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,20 @@ pub fn load_config(path: &PathBuf) -> anyhow::Result<Config> {
5858
}
5959

6060
// Save the entire configuration (including commands and other sections) to a file.
61-
pub fn save_config(path: &Path, config: &Config) {
62-
let config_data = serde_json::to_string_pretty(config).expect("❌ Failed to serialize config");
63-
let mut file = File::create(path).expect("❌ Unable to create config file ");
61+
pub fn save_config(path: &Path, config: &Config) -> anyhow::Result<()> {
62+
let config_data = serde_json::to_string_pretty(config).context("failed to serialize config")?;
63+
let mut file = File::create(path)
64+
.with_context(|| format!("unable to create config file at {}", path.display()))?;
6465
// Write the serialized config data to the file
6566
file.write_all(config_data.as_bytes())
66-
.expect("❌ Unable to write to config file");
67+
.context("unable to write to config file")?;
68+
Ok(())
6769
}
6870

6971
// Saves a default config.
7072
fn create_default_config(path: &Path) -> anyhow::Result<Config> {
7173
let default_config = Config::default();
72-
save_config(path, &default_config);
74+
save_config(path, &default_config).context("failed to save default config")?;
7375
println!("✅ Successfully created and saved new default config.");
7476
Ok(default_config)
7577
}
@@ -178,7 +180,7 @@ mod tests {
178180
window_title: Some("My CLI Menu".into()),
179181
};
180182

181-
save_config(&path, &original);
183+
save_config(&path, &original).expect("Should save config");
182184

183185
let loaded = load_config(&path).expect("Should load config");
184186
assert_eq!(original.commands.len(), loaded.commands.len());

src/menu_edit.rs

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ use std::path::PathBuf;
1010
use std::process;
1111
use textwrap::fill;
1212

13+
// Utility function to parse command index from formatted string like "1. Command Name"
14+
fn parse_command_index(index_str: &str) -> Option<usize> {
15+
index_str
16+
.split('.')
17+
.next()
18+
.and_then(|num| num.trim().parse::<usize>().ok())
19+
.and_then(|num| num.checked_sub(1))
20+
}
21+
1322
pub fn edit_menu(config_path: &PathBuf) {
1423
let mut config = crate::config::load_config(config_path).unwrap_or_else(|e| {
1524
eprintln!("Error: {e}");
@@ -62,11 +71,18 @@ pub fn edit_menu(config_path: &PathBuf) {
6271
match save_prompt {
6372
Some("Yes") => {
6473
if validate_json(&config) {
65-
save_config(config_path, &config);
66-
println!(
67-
"✅ Changes Saved. Press any key to return to Main Menu..."
68-
);
69-
pause();
74+
match save_config(config_path, &config) {
75+
Ok(_) => {
76+
println!(
77+
"✅ Changes Saved. Press any key to return to Main Menu..."
78+
);
79+
pause();
80+
}
81+
Err(e) => {
82+
eprintln!("❌ Error saving config: {e}");
83+
pause();
84+
}
85+
}
7086
} else {
7187
println!("❌ Error: Invalid JSON format. Changes not saved.");
7288
}
@@ -133,13 +149,13 @@ pub fn edit_command(config: &mut Config, changes_made: &mut bool) {
133149
None => return,
134150
};
135151

136-
let command_number = command_index
137-
.split('.')
138-
.next()
139-
.unwrap()
140-
.parse::<usize>()
141-
.unwrap()
142-
- 1;
152+
let command_number = match parse_command_index(&command_index) {
153+
Some(num) => num,
154+
None => {
155+
println!("❌ Invalid command selection.");
156+
return;
157+
}
158+
};
143159

144160
let display_name = match prompt_or_return(|| {
145161
inquire::Text::new("Enter the new display name for the command:")
@@ -186,13 +202,13 @@ pub fn reorder_command(config: &mut Config, changes_made: &mut bool) {
186202
None => return,
187203
};
188204

189-
let command_number = command_index
190-
.split('.')
191-
.next()
192-
.unwrap()
193-
.parse::<usize>()
194-
.unwrap()
195-
- 1;
205+
let command_number = match parse_command_index(&command_index) {
206+
Some(num) => num,
207+
None => {
208+
println!("❌ Invalid command selection.");
209+
return;
210+
}
211+
};
196212

197213
let new_position: usize = match prompt_or_return(|| {
198214
inquire::Text::new("Enter the new position for this command:")
@@ -236,13 +252,13 @@ pub fn delete_command(config: &mut Config, changes_made: &mut bool) {
236252
None => return,
237253
};
238254

239-
let command_number = command_index
240-
.split('.')
241-
.next()
242-
.unwrap()
243-
.parse::<usize>()
244-
.unwrap()
245-
- 1;
255+
let command_number = match parse_command_index(&command_index) {
256+
Some(num) => num,
257+
None => {
258+
println!("❌ Invalid command selection.");
259+
return;
260+
}
261+
};
246262

247263
let deleted = config.commands.remove(command_number);
248264
println!(

0 commit comments

Comments
 (0)