Skip to content

Commit

Permalink
0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vars1ty committed Jan 15, 2023
1 parent 58413e5 commit e7c8571
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 26 deletions.
20 changes: 20 additions & 0 deletions ALIASES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Aliases
These aliases are only valid for commands (+ tooltip commands).
***
- `%username%` - Username
- `%hostname%` - Hostname
- `%shell%` - Active Session Shell
- `%distro%` - Distribution name
- `%distro_id%` - Distribution ID, for example: `arch`
- `%distro_build_id%` - Distribution Build ID, for example `rolling`
- `%total_mem%` - Total amount of installed memory (in GB)
- `%cached_mem%` - Total amount of cached memory (in GB)
- `%available_mem%` - Total amount of available memory (in GB)
- `%used_mem%` - Total amount of used memory (in GB)

## I can just use `whoami`, why all of this?
You may use completely dynamic commands like `whoami` if you want, the benefit over aliases though are:

1. Lower overhead due to being cached and retrieved via `libc`, rather than expensive commands
2. A lot faster to process
3. Cached at startup, then reused afterwards
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "hybrid-bar"
authors = [ "varsity <[email protected]>" ]
version = "0.3.9"
version = "0.4.0"
edition = "2021"
description = "A simple status bar made for wlroots compositors."
license = "MIT"
Expand Down
34 changes: 15 additions & 19 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,27 @@ macro_rules! log {
#[macro_export]
/// Executes a bash command and outputs it to `result`.
macro_rules! execute {
($($cmd:expr),*) => {
{
let mut result;
$(
if $cmd.is_empty() {
// Return a stack-allocated string containing no content.
drop(heapless::String::<0>::default());
}
($cmd:expr) => {{
let mut result;
if $cmd.is_empty() {
// Return a stack-allocated string containing no content.
drop(heapless::String::<0>::default());
}

result = String::from_utf8_lossy(
&std::process::Command::new($crate::constants::PROC_TARGET)
result = String::from_utf8_lossy(
&std::process::Command::new($crate::constants::PROC_TARGET)
.args(["-c", $cmd])
.output()
.unwrap()
.stdout)
.to_string();
.stdout,
)
.to_string();

// Remove the last character as its a new line.
result.pop();
)*
// Remove the last character as its a new line.
result.pop();

// Could use drop(&$result) but then Clippy would whine.
result
}
};
result
}};
}

#[macro_export]
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ extern crate lazy_static;
#[macro_use]
mod macros;

#[path = "utils/aliases.rs"]
mod aliases;
#[path = "widgets/box_widget.rs"]
mod box_widget;
#[path = "widgets/button_widget.rs"]
Expand Down
80 changes: 80 additions & 0 deletions src/utils/aliases.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use crate::config::SYSINFO;

/// Replaces `find` with `replace` if found.
fn replace_if_present(content: &mut String, find: &str, replace: &str, found_any: &mut bool) {
if content.contains(find) {
*content = content.replace(find, replace);
*found_any = true;
}
}

/// Checks if the `content` contains any of the built-in aliases, then replaces it with the real
/// value.
pub fn use_aliases(content: &str) -> String {
// TODO: Clean this up.
let mut found_any = false;
let mut content = content.to_owned();
replace_if_present(
&mut content,
"%username%",
&SYSINFO.username,
&mut found_any,
);
replace_if_present(
&mut content,
"%hostname%",
&SYSINFO.hostname,
&mut found_any,
);
replace_if_present(&mut content, "%shell%", &SYSINFO.shell, &mut found_any);
replace_if_present(&mut content, "%kernel%", &SYSINFO.kernel, &mut found_any);
replace_if_present(
&mut content,
"%used_mem%",
&SYSINFO.used_mem,
&mut found_any,
);
replace_if_present(
&mut content,
"%distro_id%",
&SYSINFO.distro_id,
&mut found_any,
);
replace_if_present(
&mut content,
"%total_mem%",
&SYSINFO.total_mem,
&mut found_any,
);
replace_if_present(
&mut content,
"%cached_mem%",
&SYSINFO.cached_mem,
&mut found_any,
);
replace_if_present(
&mut content,
"%available_mem%",
&SYSINFO.available_mem,
&mut found_any,
);
replace_if_present(
&mut content,
"%distro%",
&SYSINFO.distro_name,
&mut found_any,
);
replace_if_present(
&mut content,
"%distro_build_id%",
&SYSINFO.distro_build_id,
&mut found_any,
);

if !found_any {
// Couldn't find any aliases present, run execute.
return execute!(&content);
}

content
}
4 changes: 2 additions & 2 deletions src/widgets/button_widget.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{structures::Align, ui, widget::HWidget};
use crate::{aliases::use_aliases, structures::Align, ui, widget::HWidget};
use glib::GString;
use gtk::{traits::*, *};
use std::time::Duration;
Expand Down Expand Up @@ -43,7 +43,7 @@ impl HWidget for ButtonWidget {
let tick = move || {
let mut new_tooltip = String::default();
new_tooltip.push_str(&tooltip_clone);
new_tooltip.push_str(&execute!(&tooltip_command_clone));
new_tooltip.push_str(&use_aliases(&tooltip_command_clone));

let tooltip_markup = button_clone
.tooltip_markup()
Expand Down
8 changes: 5 additions & 3 deletions src/widgets/label_widget.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{config, constants::PROC_TARGET, structures::Align, ui, widget::HWidget};
use crate::{
aliases::use_aliases, config, constants::PROC_TARGET, structures::Align, ui, widget::HWidget,
};
use glib::GString;
use gtk::{traits::*, *};
use std::{process::Stdio, sync::RwLock, time::Duration};
Expand Down Expand Up @@ -70,7 +72,7 @@ fn start_tooltip_loop(label_ref: &LabelWidget) {
let tick = move || {
let mut new_tooltip = String::default();
new_tooltip.push_str(&tooltip);
new_tooltip.push_str(&execute!(&tooltip_command));
new_tooltip.push_str(&use_aliases(&tooltip_command));

let tooltip_markup = label
.tooltip_markup()
Expand Down Expand Up @@ -100,7 +102,7 @@ fn start_label_loop(label: Label, text: String, command: String, update_rate: u6
if !listen {
let mut new_text = String::default();
new_text.push_str(&text);
new_text.push_str(&execute!(&command));
new_text.push_str(&use_aliases(&command));

if !label.text().eq(&new_text) {
// Not the same as new_text; redraw.
Expand Down

0 comments on commit e7c8571

Please sign in to comment.