Skip to content

Commit

Permalink
[Readout] Add disk space readout (#283)
Browse files Browse the repository at this point in the history
* chore - bump libmacchina versoin

* feat - add disk space readout and option for showing disk space percentage

* docs - udpate with info for added disk space readout

* chore - include gpu and disk space options in example themes

* fix - make default string consistant

* fix - include same bar functionality used for memory usage
  • Loading branch information
Rolv-Apneseth authored Mar 31, 2023
1 parent eab9991 commit e894f19
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 9 deletions.
4 changes: 2 additions & 2 deletions 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
Expand Up @@ -11,7 +11,7 @@ readme = "README.md"
build = "build.rs"

[dependencies]
libmacchina = { version = "6.4.1", features = ["version"] }
libmacchina = { version = "7.0.0", features = ["version"] }
bytesize = "1.1.0"
shellexpand = "3.0.0"
clap = { version = "4.0.32", features = ["derive"] }
Expand Down
2 changes: 2 additions & 0 deletions contrib/themes/Helium.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ backlight = "Brightness"
resolution = "Resolution"
cpu_load = "CPU Load"
cpu = "CPU"
gpu = "GPU"
disk_space = "Disk Space"
2 changes: 2 additions & 0 deletions contrib/themes/Hydrogen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ backlight = "Brightness"
resolution = "Resolution"
cpu_load = "CPU Load"
cpu = "CPU"
gpu = "GPU"
disk_space = "Disk Space"
2 changes: 2 additions & 0 deletions contrib/themes/Lithium.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ backlight = "Brightness"
resolution = "Resolution"
cpu_load = "CPU Load"
cpu = "CPU"
gpu = "GPU"
disk_space = "Disk Space"
21 changes: 20 additions & 1 deletion doc/macchina.1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "MACCHINA" "1" "2023-03-25"
.TH "MACCHINA" "1" "2023-03-28"
.P
.SH NAME
.P
Expand Down Expand Up @@ -94,6 +94,16 @@ Prints help information.\&
Prints version information.\&
.P
.RE
\fB-m, --memory-percentage\fR
.RS 4
Show memory usage in percentage
.P
.RE
\fB-p, --disk-space-percentage\fR
.RS 4
Show disk space usage in percentage
.P
.RE
\fB-o, --show\fR
.RS 4
Display only the specified readouts.\&
Expand Down Expand Up @@ -272,6 +282,15 @@ Battery
.IP \(bu 4
.\}
GPU
.RE
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.IP \(bu 4
.\}
DiskSpace

.RE
.P
Expand Down
7 changes: 7 additions & 0 deletions doc/macchina.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ on performance.
*-v, --version*
Prints version information.

*-m, --memory-percentage*
Show memory usage in percentage

*-p, --disk-space-percentage*
Show disk space usage in percentage

*-o, --show*
Display only the specified readouts.

Expand All @@ -87,6 +93,7 @@ on performance.
- Memory
- Battery
- GPU
- DiskSpace

*--ascii-artists*
Lists the original artists of the ASCII art used by macchina.
Expand Down
9 changes: 8 additions & 1 deletion doc/macchina.7
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "MACCHINA" "7" "2023-03-25"
.TH "MACCHINA" "7" "2023-03-28"
.P
.SH NAME
macchina - theming.\&
Expand Down Expand Up @@ -447,5 +447,12 @@ Defines the text of the GPU readout(s), e.\&g.\&:
gpu = "GPU"
.P
.RE
.SS disk_space
Defines the text of the disk space readout, e.\&g.\&:
.P
.RS 4
disk_space = "Disk Space"
.P
.RE
.SH SEE ALSO
macchina(1)
5 changes: 5 additions & 0 deletions doc/macchina.7.scd
Original file line number Diff line number Diff line change
Expand Up @@ -309,5 +309,10 @@ Defines the text of the GPU readout(s), e.g.:

gpu = "GPU"

## disk_space
Defines the text of the disk space readout, e.g.:

disk_space = "Disk Space"

# SEE ALSO
macchina(1)
7 changes: 7 additions & 0 deletions macchina.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ current_shell = true
# processor.
physical_cores = true

# Display percentage next to memory usage
memory_percentage = false

# Display percentage next to disk space usage
disk_space_percentage = false

# Themes need to be placed in "$XDG_CONFIG_DIR/macchina/themes" beforehand.
# e.g.:
# if theme path is /home/foo/.config/macchina/themes/Sodium.toml
Expand Down Expand Up @@ -45,5 +51,6 @@ physical_cores = true
# - Memory
# - Battery
# - GPU
# - DiskSpace
# Example:
# show = ["Battery", "Memory", ...]
2 changes: 1 addition & 1 deletion src/bars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn num_to_blocks(value: u8) -> usize {

/// Returns a `usize` whose value can range from 0 up to 10 based on the given `value`.
/// This is very similar to `num_to_blocks` but the calculations are done in a different way.
pub fn memory(used: u64, total: u64) -> usize {
pub fn usage(used: u64, total: u64) -> usize {
let used = used as f64;
let total = total as f64;

Expand Down
17 changes: 16 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,20 @@ pub struct Opt {
#[clap(short = 'K', long = "long-kernel", help = "Lengthens kernel output")]
pub long_kernel: bool,

#[clap(short = 'm', long = "memory-percentage", help = "Show memory usage in percentage")]
#[clap(
short = 'm',
long = "memory-percentage",
help = "Show memory usage in percentage"
)]
pub memory_percentage: bool,

#[clap(
short = 'p',
long = "disk-space-percentage",
help = "Show disk space usage in percentage"
)]
pub disk_space_percentage: bool,

#[clap(
short = 'C',
long = "physical-cores",
Expand Down Expand Up @@ -125,6 +136,10 @@ impl Opt {
self.memory_percentage = args.memory_percentage;
}

if args.disk_space_percentage {
self.disk_space_percentage = args.disk_space_percentage;
}

if args.physical_cores {
self.physical_cores = true;
}
Expand Down
36 changes: 34 additions & 2 deletions src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub enum ReadoutKey {
Memory,
Battery,
GPU,
DiskSpace,
}

impl Display for ReadoutKey {
Expand All @@ -57,6 +58,7 @@ impl Display for ReadoutKey {
Self::Memory => write!(f, "Memory"),
Self::Battery => write!(f, "Battery"),
Self::GPU => write!(f, "GPU"),
Self::DiskSpace => write!(f, "DiskSpace"),
}
}
}
Expand Down Expand Up @@ -192,6 +194,9 @@ pub fn get_all_readouts<'a>(
handle_readout_window_manager(&mut readout_values, &general_readout)
}
ReadoutKey::GPU => handle_readout_gpu(&mut readout_values, &general_readout),
ReadoutKey::DiskSpace => {
handle_readout_disk_space(&mut readout_values, &general_readout, theme, opt)
}
};
}

Expand Down Expand Up @@ -406,10 +411,13 @@ fn handle_readout_memory(readout_values: &mut Vec<Readout>, theme: &Theme, opt:
match (total, used) {
(Ok(total), Ok(used)) => {
if theme.get_bar().is_visible() {
let bar = create_bar(theme, crate::bars::memory(used, total));
let bar = create_bar(theme, crate::bars::usage(used, total));
readout_values.push(Readout::new(ReadoutKey::Memory, bar))
} else {
readout_values.push(Readout::new(ReadoutKey::Memory, format_mem(total, used, opt.memory_percentage)))
readout_values.push(Readout::new(
ReadoutKey::Memory,
format_mem(total, used, opt.memory_percentage),
))
}
}
(Err(e), _) | (_, Err(e)) => readout_values.push(Readout::new_err(ReadoutKey::Memory, e)),
Expand Down Expand Up @@ -500,3 +508,27 @@ fn handle_readout_gpu(readout_values: &mut Vec<Readout>, general_readout: &Gener
Err(e) => readout_values.push(Readout::new_err(ReadoutKey::GPU, e)),
};
}

fn handle_readout_disk_space(
readout_values: &mut Vec<Readout>,
general_readout: &GeneralReadout,
theme: &Theme,
opt: &Opt,
) {
use crate::format::disk_space as format_disk_space;

match general_readout.disk_space() {
Ok((used, total)) => {
if theme.get_bar().is_visible() {
let bar = create_bar(theme, crate::bars::usage(used, total));
readout_values.push(Readout::new(ReadoutKey::DiskSpace, bar))
} else {
readout_values.push(Readout::new(
ReadoutKey::DiskSpace,
format_disk_space(used, total, opt.disk_space_percentage),
))
}
}
Err(e) => readout_values.push(Readout::new_err(ReadoutKey::DiskSpace, e)),
}
}
15 changes: 15 additions & 0 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,18 @@ pub fn packages(packages: Vec<(PackageManager, usize)>) -> Result<String, Readou

Ok(string)
}

pub fn disk_space(used: u64, total: u64, percentage: bool) -> String {
let used_kb = ByteSize::b(used);
let total_kb = ByteSize::b(total);

let mut output = String::new();
output.push_str(&format!("{used_kb} / {total_kb}"));

if percentage {
let p = (used as f64 / total as f64 * 100f64).ceil() as usize;
output.push_str(&format!(" ({p}%)"));
};

output
}
1 change: 1 addition & 0 deletions src/theme/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ impl Theme {
ReadoutKey::Uptime => self.keys.get_uptime(),
ReadoutKey::Memory => self.keys.get_memory(),
ReadoutKey::GPU => self.keys.get_gpu(),
ReadoutKey::DiskSpace => self.keys.get_disk_space(),
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/theme/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ pub struct Keys {
pub cpu_load: Option<String>,
pub cpu: Option<String>,
pub gpu: Option<String>,
pub disk_space: Option<String>,
}

impl Default for Keys {
Expand All @@ -324,6 +325,7 @@ impl Default for Keys {
cpu_load: Some(String::from("CPU Load")),
cpu: Some(String::from("CPU")),
gpu: Some(String::from("GPU")),
disk_space: Some(String::from("Disk Space")),
}
}
}
Expand Down Expand Up @@ -480,4 +482,12 @@ impl Keys {

"GPU"
}

pub fn get_disk_space(&self) -> &str {
if let Some(d) = &self.disk_space {
return d;
}

"Disk Space"
}
}

0 comments on commit e894f19

Please sign in to comment.