Use pairs of buffer values to render braille

This commit is contained in:
clerie 2024-01-14 00:17:19 +01:00
parent f76b5f4f5e
commit ac4777e50c
4 changed files with 32 additions and 5 deletions

View File

@ -2,6 +2,7 @@ use crate::cpustats::get_cpu_usage;
use crate::cpustats::get_clk_tck; use crate::cpustats::get_clk_tck;
use crate::ansiiescape; use crate::ansiiescape;
use crate::braille::braille_symbol; use crate::braille::braille_symbol;
use crate::vectools::ValuePairs;
pub struct CpuUsageBuffer { pub struct CpuUsageBuffer {
clk_tck: u32, clk_tck: u32,
@ -48,6 +49,15 @@ fn cpu_usages_to_braille(a: u32, b: u32) -> u8 {
a_braille + shift_braille_one_col(b_braille) a_braille + shift_braille_one_col(b_braille)
} }
fn max(a: u32, b: u32) -> u32 {
if a > b {
return a;
}
else {
return b;
}
}
impl CpuUsageBuffer { impl CpuUsageBuffer {
pub fn new() -> Self { pub fn new() -> Self {
let first_value = get_cpu_usage(); let first_value = get_cpu_usage();
@ -73,9 +83,9 @@ impl CpuUsageBuffer {
pub fn braille(&self) -> String { pub fn braille(&self) -> String {
let mut out: String = String::new(); let mut out: String = String::new();
for n in 0..(self.cpu_num()) { for n in 0..(self.cpu_num()) {
for u in &self.buffer { for u in ValuePairs::new(self.buffer.clone().into_iter()) {
out += &cpu_usage_to_color(u[n]); out += &cpu_usage_to_color(max(u.0[n], u.1[n]));
out += &braille_symbol(cpu_usages_to_braille(u[n], u[n])); out += &braille_symbol(cpu_usages_to_braille(u.0[n], u.1[n]));
} }
out += &ansiiescape::reset(); out += &ansiiescape::reset();
out += "\n"; out += "\n";

View File

@ -2,3 +2,4 @@ pub mod ansiiescape;
pub mod braille; pub mod braille;
pub mod cpustats; pub mod cpustats;
pub mod cpuusagebuffer; pub mod cpuusagebuffer;
pub mod vectools;

View File

@ -2,11 +2,10 @@ use std::thread::sleep;
use std::time; use std::time;
use fuedra_schwitzt::cpuusagebuffer::CpuUsageBuffer; use fuedra_schwitzt::cpuusagebuffer::CpuUsageBuffer;
use fuedra_schwitzt::ansiiescape::cursor_up; use fuedra_schwitzt::ansiiescape::cursor_up;
use fuedra_schwitzt::vectools;
fn main() { fn main() {
// Init buffer // Init buffer
let mut buffer = CpuUsageBuffer::new(); let mut buffer = CpuUsageBuffer::new();

17
src/vectools.rs Normal file
View File

@ -0,0 +1,17 @@
pub struct ValuePairs<I: Iterator> {
iter: I,
}
impl<I: Iterator> ValuePairs<I> {
pub fn new(iter: I) -> ValuePairs<I> {
ValuePairs { iter }
}
}
impl<I: Iterator> Iterator for ValuePairs<I> {
type Item = (I::Item, I::Item);
fn next(&mut self) -> Option<Self::Item> {
Some((self.iter.next()?, self.iter.next()?))
}
}