Skip to content

Commit

Permalink
Add some new modules
Browse files Browse the repository at this point in the history
  • Loading branch information
devanlooches committed Nov 27, 2021
1 parent c6ca27f commit 96b9fd4
Show file tree
Hide file tree
Showing 6 changed files with 334 additions and 62 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rocketfetch"
version = "0.6.0"
version = "0.6.1"
authors = ["Devan Looches <[email protected]>"]
edition = "2021"
include = ["src/**/*"]
Expand All @@ -15,6 +15,7 @@ categories = ["command-line-utilities"]
console = { version = "0.15.0", default-features = false, features = ["ansi-parsing"] }
dirs = { version = "4.0.0", default-features = false }
libmacchina = { version = "3.8.6", default-features = false }
regex = {version = "1.5.4", default-features = false, features = ["unicode-perl"] }
secfmt = { version = "0.1.1", default-features = false }
serde = { version = "1.0.130", default-features = false }
serde_derive = { version = "1.0.130", default-features = false }
Expand Down
69 changes: 67 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
- [Kernel Header](#kernel_header_configuration)
- [Uptime Header](#uptime_header_configuration)
- [Packages Header](#packages_header_configuration)
- [Packages Header](#packages_header_configuration)
- [Shell Header](#shell_header_configuration)
- [Resolution Header](#resolution_header_configuration)
- [Desktop Environment Header](#desktop_environment_header_configuration)
- [Window Manager Header](#window_manager_header_configuration)
- [Terminal Header](#terminal_header_configuration)
- [Custom Header](#custom_header_configuration)
- [Default Configuration](#default_configuration)
- [Todo](#todo)
Expand Down Expand Up @@ -131,6 +137,41 @@ OPTIONS:
| pre_text | "Packages: " | Text that comes before this module |
| output_style | "white" | A format string with each word separated by dots that describes the style of the output text |

### Shell Header <a name="shell_header_configuration"></a>
| Value | Default | Description |
|-----------------|----------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|
| pre_text_style | "bold.yellow" | A format string with each word separated by dots that describes the style of the text that comes before this module |
| pre_text | "Shell: " | Text that comes before this module |
| output_style | "white" | A format string with each word separated by dots that describes the style of the output text |

### Resolution Header <a name="resolution_header_configuration"></a>
| Value | Default | Description |
|-----------------|----------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|
| pre_text_style | "bold.yellow" | A format string with each word separated by dots that describes the style of the text that comes before this module |
| pre_text | "Resolution: " | Text that comes before this module |
| output_style | "white" | A format string with each word separated by dots that describes the style of the output text |

### Desktop Environment Header <a name="desktop_environment_header_configuration"></a>
| Value | Default | Description |
|-----------------|----------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|
| pre_text_style | "bold.yellow" | A format string with each word separated by dots that describes the style of the text that comes before this module |
| pre_text | "Desktop Environment: " | Text that comes before this module |
| output_style | "white" | A format string with each word separated by dots that describes the style of the output text |

### Window Manager Header <a name="window_manager_header_configuration"></a>
| Value | Default | Description |
|-----------------|----------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|
| pre_text_style | "bold.yellow" | A format string with each word separated by dots that describes the style of the text that comes before this module |
| pre_text | "Window Manager: " | Text that comes before this module |
| output_style | "white" | A format string with each word separated by dots that describes the style of the output text |

### Terminal Header <a name="terminal_header_configuration"></a>
| Value | Default | Description |
|-----------------|----------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|
| pre_text_style | "bold.yellow" | A format string with each word separated by dots that describes the style of the text that comes before this module |
| pre_text | "Terminal: " | Text that comes before this module |
| output_style | "white" | A format string with each word separated by dots that describes the style of the output text |

### Custom Header <a name="custom_header_configuration"></a>
| Value | Description |
|----------------|---------------------------------------------------------------------------------------------------------------|
Expand All @@ -142,7 +183,7 @@ OPTIONS:
# Default Configuration <a name="default_configuration"></a>
A default Configuration will look like so:
```toml
module_order = "user delimiter os host kernel uptime packages"
module_order = "user delimiter os host kernel uptime packages shell resolution desktop_environment window_manager terminal"
offset = 4
logo_cmd = "auto"

Expand Down Expand Up @@ -195,7 +236,31 @@ time_format = "$days days, $hours hours, $minutes minutes"
pre_text_style = "bold.yellow"
pre_text = "Packages: "
output_style = "white"
package_manager = "auto"

[shell]
pre_text_style = "bold.yellow"
pre_text = "Shell: "
output_style = "white"

[resolution]
pre_text_style = "bold.yellow"
pre_text = "Resolution: "
output_style = "white"

[desktop_environment]
pre_text_style = "bold.yellow"
pre_text = "Desktop Environment: "
output_style = "white"

[window_manager]
pre_text_style = "bold.yellow"
pre_text = "Window Manager: "
output_style = "white"

[terminal]
pre_text_style = "bold.yellow"
pre_text = "Terminal: "
output_style = "white"
```

# Todo <a name="todo"></a>
Expand Down
27 changes: 26 additions & 1 deletion config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module_order = "user delimiter os host kernel uptime packages"
module_order = "user delimiter os host kernel uptime packages shell resolution desktop_environment window_manager terminal"
offset = 4
logo_cmd = "auto"

Expand Down Expand Up @@ -51,3 +51,28 @@ time_format = "$days days, $hours hours, $minutes minutes"
pre_text_style = "bold.yellow"
pre_text = "Packages: "
output_style = "white"

[shell]
pre_text_style = "bold.yellow"
pre_text = "Shell: "
output_style = "white"

[resolution]
pre_text_style = "bold.yellow"
pre_text = "Resolution: "
output_style = "white"

[desktop_environment]
pre_text_style = "bold.yellow"
pre_text = "Desktop Environment: "
output_style = "white"

[window_manager]
pre_text_style = "bold.yellow"
pre_text = "Window Manager: "
output_style = "white"

[terminal]
pre_text_style = "bold.yellow"
pre_text = "Terminal: "
output_style = "white"
106 changes: 58 additions & 48 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ pub struct Config {
kernel: Kernel,
uptime: Uptime,
packages: Packages,
shell: Shell,
resolution: Resolution,
desktop_environment: DesktopEnvironment,
window_manager: WindowManager,
terminal: Terminal,

#[serde(flatten)]
custom_modules: HashMap<String, Module>,
Expand Down Expand Up @@ -113,6 +118,11 @@ impl Config {
let kernel = self_clone.kernel;
let uptime = self_clone.uptime;
let packages = self_clone.packages;
let shell = self_clone.shell;
let resolution = self_clone.resolution;
let desktop_environment = self_clone.desktop_environment;
let window_manager = self_clone.window_manager;
let terminal = self_clone.terminal;
let custom = self_clone.custom_modules;
if modules.contains(&"user") {
let handle = thread::spawn(move || (String::from("user"), user.get_info()));
Expand All @@ -138,6 +148,32 @@ impl Config {
let handle = thread::spawn(move || (String::from("packages"), packages.get_info()));
handles.push(handle);
}
if modules.contains(&"shell") {
let handle = thread::spawn(move || (String::from("shell"), shell.get_info()));
handles.push(handle);
}
if modules.contains(&"resolution") {
let handle = thread::spawn(move || (String::from("resolution"), resolution.get_info()));
handles.push(handle);
}
if modules.contains(&"desktop_environment") {
let handle = thread::spawn(move || {
(
String::from("desktop_environment"),
desktop_environment.get_info(),
)
});
handles.push(handle);
}
if modules.contains(&"window_manager") {
let handle =
thread::spawn(move || (String::from("window_manager"), window_manager.get_info()));
handles.push(handle);
}
if modules.contains(&"terminal") {
let handle = thread::spawn(move || (String::from("terminal"), terminal.get_info()));
handles.push(handle);
}
for (name, module) in custom {
let handle = thread::spawn(move || (name, module.get_info()));
handles.push(handle);
Expand All @@ -149,51 +185,17 @@ impl Config {
let mut vec: Vec<String> = Vec::new();
for (i, module) in self.module_order.split_whitespace().enumerate() {
match module {
"user" => vec.push(
modules_unordered
.get(&String::from("user"))
.unwrap()
.to_string(),
),
"delimiter" => vec.push(self.delimiter.get_info(measure_text_width(&vec[i - 1]))),
"os" => vec.push(
modules_unordered
.get(&String::from("os"))
.unwrap()
.to_string(),
),
"host" => vec.push(
modules_unordered
.get(&String::from("host"))
.unwrap()
.to_string(),
),
"kernel" => vec.push(
modules_unordered
.get(&String::from("kernel"))
.unwrap()
.to_string(),
),
"uptime" => vec.push(
modules_unordered
.get(&String::from("uptime"))
.unwrap()
.to_string(),
),
"packages" => vec.push(
modules_unordered
.get(&String::from("packages"))
.unwrap()
.to_string(),
),
v => {
let error = format!("Unknown module: {}", v);
let module = handle_error!(
modules_unordered.get(v).ok_or(""),
error,
"Make sure you have this module defined."
vec.push(
handle_error!(
modules_unordered.get(&String::from(v)).ok_or(""),
error,
"Make sure you have this module defined"
)
.to_string(),
);
vec.push(module.to_string());
}
}
}
Expand Down Expand Up @@ -270,7 +272,7 @@ impl Config {
if self.logo_cmd.is_empty() || self.logo_cmd == "auto" {
self.logo()
} else {
Config::run_cmd(&self.logo_cmd)
Config::run_cmd(&self.logo_cmd, "Failed to run logo command")
.lines()
.map(|v| v.to_string())
.collect::<Vec<String>>()
Expand Down Expand Up @@ -298,17 +300,18 @@ impl Config {
}
}

pub fn run_cmd(cmd: &str) -> String {
pub fn run_cmd(cmd: &str, error_msg: &str) -> String {
use std::process::Command;
let output = if cfg!(target_os = "windows") {
let command_run = Command::new("cmd").args(&["/C", cmd]).output();
handle_error!(command_run, "Failed to run command")
handle_error!(command_run, error_msg)
} else {
let command_run = Command::new("sh").args(["-c", cmd]).output();
handle_error!(command_run, "Failed to run command")
};
handle_error!(command_run, error_msg)
}
.stdout;
handle_error!(
String::from_utf8(output.stdout.clone()),
String::from_utf8(output.clone()),
"Failed to read stdout from command"
)
.trim()
Expand Down Expand Up @@ -474,7 +477,9 @@ impl Default for Config {
fn default() -> Self {
Config {
offset: 4,
module_order: String::from("user delimiter os host kernel uptime packages"),
module_order: String::from(
"user delimiter os host kernel uptime packages shell resolution desktop_environment window_manager terminal",
),
logo_cmd: String::from("auto"),
format: Format::default(),
user: User::default(),
Expand All @@ -485,6 +490,11 @@ impl Default for Config {
uptime: Uptime::default(),
custom_modules: HashMap::new(),
packages: Packages::default(),
shell: Shell::default(),
resolution: Resolution::default(),
desktop_environment: DesktopEnvironment::default(),
window_manager: WindowManager::default(),
terminal: Terminal::default(),
}
}
}
Expand Down
Loading

0 comments on commit 96b9fd4

Please sign in to comment.