Skip to content

Commit 518ca23

Browse files
committed
cleanup and add 'list' command
1 parent 4d74f99 commit 518ca23

File tree

6 files changed

+133
-54
lines changed

6 files changed

+133
-54
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ edition = "2021"
55

66
[dependencies]
77
clap = { version = "4.0.15", features = ["derive"] }
8+
dirs = "4.0.0"
89
serde = { version = "1.0.152", features = ["derive"] }
910
serde_json = "1.0.91"

src/cli.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub enum Action {
1818
Launch { category: String },
1919
Set { category: String, choice: String },
2020
Get { category: String },
21+
List { category: Option<String> },
2122
Install { category: String, choice: String },
2223
Uninstall { category: String, choice: String },
2324
Test { category: String, choice: String },

src/launcher.sh

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/main.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::{fs, path::PathBuf};
2+
13
use clap::Parser;
24
use cli::{Action, Args};
35

@@ -27,7 +29,7 @@ fn main() {
2729
.choices
2830
.shell_prompt_bash
2931
.unwrap_or(String::from("starship"));
30-
util::launch("shell-prompt-bash", choice);
32+
util::run("shell-prompt-bash", choice.as_str(), "launch");
3133
}
3234
"shell-prompt-zsh" => {
3335
let _args = vec!["starship", "init", "zsh", "--print-full-init"];
@@ -63,6 +65,27 @@ fn main() {
6365
util::save_data(&data);
6466
}
6567
Action::Get { category: _ } => {}
68+
Action::List { category } => {
69+
let list = |dir: PathBuf| {
70+
for entry in fs::read_dir(&dir)
71+
.expect(format!("Directory does not exist: {}", dir.to_str().unwrap()).as_str())
72+
{
73+
let path = entry.unwrap().path();
74+
let basename = path.file_name().unwrap();
75+
76+
println!("{}", basename.to_str().unwrap());
77+
}
78+
};
79+
80+
let choose_dir = util::get_choose_dir();
81+
if let Some(category) = category {
82+
let dir = choose_dir.join("choices").join(category);
83+
list(dir);
84+
} else {
85+
let dir = choose_dir.join("choices");
86+
list(dir);
87+
}
88+
}
6689
Action::Install { category, choice } => {
6790
util::run(category.as_str(), choice.as_str(), "install")
6891
}

src/util.rs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use std::{env, fs, path::PathBuf, process::Command};
44

55
use serde::{Deserialize, Serialize};
66

7-
pub struct Config {}
8-
97
#[derive(Serialize, Deserialize)]
108
pub struct Choices {
119
pub shell_prompt_bash: Option<String>,
@@ -16,32 +14,15 @@ pub struct Data {
1614
pub choices: Choices,
1715
}
1816

19-
pub fn launch(category: &str, choice: String) {
20-
let entrypoint =
21-
"/storage/ur/storage_home/Docs/Programming/Repositories/default/choose/src/launcher.sh";
22-
23-
let choice_file = PathBuf::from(format!("{}", env::var("HOME").unwrap()))
24-
.join(".dotfiles/os/*nix/user/.config/choose/choices")
25-
.join(category)
26-
.join(format!("{}.sh", choice.as_str()));
27-
28-
Command::new(entrypoint)
29-
.arg(choice_file)
30-
.arg("launch")
31-
.spawn()
32-
.unwrap();
33-
}
34-
3517
pub fn run(category: &str, choice: &str, action: &str) {
36-
let entrypoint =
37-
"/storage/ur/storage_home/Docs/Programming/Repositories/default/choose/src/launcher.sh";
18+
let choose_dir = get_choose_dir();
3819

39-
let choice_file = PathBuf::from(format!("{}", env::var("HOME").unwrap()))
40-
.join(".dotfiles/os/*nix/user/.config/choose/choices")
20+
let choice_file = choose_dir
21+
.join("choices")
4122
.join(category)
4223
.join(format!("{}.sh", choice));
4324

44-
let exit_status = Command::new(entrypoint)
25+
let exit_status = Command::new(choose_dir.join("launcher.sh"))
4526
.arg(choice_file)
4627
.arg(action)
4728
.spawn()
@@ -54,17 +35,32 @@ pub fn run(category: &str, choice: &str, action: &str) {
5435
}
5536

5637
pub fn get_data() -> Data {
57-
let path = PathBuf::from(format!("{}", env::var("HOME").unwrap()))
58-
.join(".dotfiles/os/*nix/user/.config/choose/data.json");
38+
let path = get_choose_dir().join("data.json");
39+
5940
let content = fs::read_to_string(path).unwrap();
6041
let data: Data = serde_json::from_str(content.as_str()).unwrap();
6142
data
6243
}
6344

6445
pub fn save_data(data: &Data) {
65-
let path = PathBuf::from(format!("{}", env::var("HOME").unwrap()))
66-
.join(".dotfiles/os/*nix/user/.config/choose/data.json");
46+
let path = get_choose_dir().join("data.json");
6747

6848
let deserialized = serde_json::to_string(data).unwrap();
6949
fs::write(path, deserialized).unwrap();
7050
}
51+
52+
pub fn get_choose_dir() -> PathBuf {
53+
let dotfiles_dir = match env::var("XDG_CONFIG_HOME") {
54+
Ok(val) => {
55+
let p = PathBuf::from(val);
56+
if p.is_absolute() {
57+
p
58+
} else {
59+
PathBuf::from(dirs::home_dir().unwrap()).join(".config")
60+
}
61+
}
62+
Err(..) => PathBuf::from(dirs::home_dir().unwrap()).join(".config"),
63+
};
64+
65+
dotfiles_dir.join("choose")
66+
}

0 commit comments

Comments
 (0)