Skip to content

Commit

Permalink
Merge pull request #30 from jw/cpu_graph
Browse files Browse the repository at this point in the history
Added cpu graph to process list and updated dependencies.
  • Loading branch information
jw authored May 12, 2024
2 parents 7cc8acd + 47ede42 commit 93d4a1e
Show file tree
Hide file tree
Showing 12 changed files with 645 additions and 336 deletions.
478 changes: 247 additions & 231 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,30 @@ name = "processbar"
path = "src/processbar.rs"

[dependencies]
anyhow = "1.0.81"
anyhow = "1.0.83"
battery = "0.7.8"
better-panic = "0.3.0"
clap = { version = "4.5.2", features = ["std", "color", "help", "usage", "error-context", "suggestions", "derive", "cargo", "wrap_help", "unicode", "string", "unstable-styles"] }
color-eyre = "0.6.2"
clap = { version = "4.5.4", features = ["std", "color", "help", "usage", "error-context", "suggestions", "derive", "cargo", "wrap_help", "unicode", "string", "unstable-styles"] }
color-eyre = "0.6.3"
crossterm = { version = "0.27.0", features = ["serde", "event-stream"] }
directories = "5.0.1"
dirs = "5.0.1"
futures = "0.3.30"
humansize = "2.1.3"
lazy_static = "1.4.0"
libc = "0.2.153"
libc = "0.2.154"
log = "0.4.21"
log4rs = "1.3.0"
owo-colors = "4.0.0"
procfs = "0.16.0"
json5 = "0.4.1"
ratatui = "0.26.1"
serde = { version = "1.0.196", features = ["derive"] }
ratatui = { version = "0.26.2", features = ["default", "unstable-widget-ref"] }
serde = { version = "1.0.201", features = ["derive"] }
signal-hook = "0.3.17"
strip-ansi-escapes = "0.2.0"
strum = { version = "0.26.1", features = ["derive"] }
tokio = { version = "1.36.0", features = ["full"] }
tokio-util = "0.7.10"
strum = { version = "0.26.2", features = ["derive"] }
tokio = { version = "1.37.0", features = ["full"] }
tokio-util = "0.7.11"
tracing = "0.1.40"
tracing-error = "0.2.0"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "serde"] }
Expand Down
11 changes: 7 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tokio::sync::mpsc;

use crate::{
action::Action,
components::{fps::FpsCounter, process::Process, Component},
components::{battery::Battery, fps::FpsCounter, process::Process, Component},
config::Config,
tui,
};
Expand All @@ -30,12 +30,15 @@ pub struct App {

impl App {
pub fn new(tick_rate: f64, frame_rate: f64, debug: bool) -> Result<Self> {
let process = Process::new();
let mut process = Process::new();
process.refresh();
let battery = Battery::new();

let components: Vec<Box<dyn Component>> = if debug {
let fps = FpsCounter::new();
vec![Box::new(process), Box::new(fps)]
vec![Box::new(process), Box::new(battery), Box::new(fps)]
} else {
vec![Box::new(process)]
vec![Box::new(process), Box::new(battery)]
};
let config = Config::new()?;
let mode = Mode::Process;
Expand Down
1 change: 1 addition & 0 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
tui::{Event, Frame},
};

pub mod battery;
pub mod fps;
pub mod process;

Expand Down
84 changes: 84 additions & 0 deletions src/components/battery.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use battery as battery_model;
use battery::State;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::text::Line;

use crate::action::Action;
use crate::components::Component;
use crate::tui::Frame;

#[derive(Debug)]
pub struct Battery {
battery: Option<battery_model::Battery>,
}

impl Default for Battery {
fn default() -> Self {
Self::new()
}
}

impl Battery {
pub fn new() -> Self {
Self { battery: None }
}
}

impl Component for Battery {
fn init(&mut self) -> color_eyre::Result<()> {
let batteries = battery_model::Manager::new().unwrap().batteries();
if batteries.is_ok() {
let b = batteries.unwrap().next().unwrap().unwrap();
self.battery = Some(b);
}
Ok(())
}

fn update(&mut self, _action: Action) -> color_eyre::Result<Option<Action>> {
let _ = self.init();
Ok(None)
}

fn draw(&mut self, f: &mut Frame<'_>, rect: Rect) -> color_eyre::Result<()> {
let layout =
Layout::new(Direction::Horizontal, vec![Constraint::Percentage(100)]).split(rect);
let mut state = "○";
if self.battery.is_some() {
state = match self.battery.as_mut().unwrap().state() {
State::Charging => "▲",
State::Discharging => "▼",
State::Full => "■",
State::Unknown => "○",
State::Empty => "○",
_ => "○",
};
}
let soc = self.battery.as_mut().unwrap().state_of_charge().value * 100.0;
let percentage = format!("{}%", soc as u32);
let status = format!("{}{} {}", "BAT", state, percentage);
let line = Line::from(status);
f.render_widget(line, layout[0]);
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use log::info;
use ratatui::{backend::TestBackend, prelude::*};

#[test]
fn test_battery() {
let mut battery = Battery::default();
let _ = battery.init();
let backend = TestBackend::new(40, 20);
let mut terminal = Terminal::new(backend).unwrap();
let _ = terminal.draw(|frame| {
let _r = battery.draw(frame, Rect::new(3, 3, 10, 1));
let b = frame.buffer_mut();
info!("{:#?}", b);
});
assert_eq!(true, true)
}
}
63 changes: 63 additions & 0 deletions src/components/cpu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::text::Line;

use crate::action::Action;
use crate::components::Component;
use crate::tui::Frame;

#[derive(Debug)]
pub struct Cpu {
state: bool,
}

impl Default for Battery {
fn default() -> Self {
Self::new()
}
}

impl Cpu {
pub fn new() -> Self {
Self { state: false }
}
}

impl Component for Cpu {
fn init(&mut self) -> color_eyre::Result<()> {
Ok(())
}

fn update(&mut self, _action: Action) -> color_eyre::Result<Option<Action>> {
let _ = self.init();
Ok(None)
}

fn draw(&mut self, f: &mut Frame<'_>, rect: Rect) -> color_eyre::Result<()> {
let layout =
Layout::new(Direction::Horizontal, vec![Constraint::Percentage(100)]).split(rect);
let message = format!("{}", ".");
let line = Line::from(status);
f.render_widget(line, layout[0]);
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use ratatui::{backend::TestBackend, prelude::*};

#[test]
fn test_cpu() {
let mut cpu = Cpu::default();
let _ = battery.init();
let backend = TestBackend::new(40, 20);
let mut terminal = Terminal::new(backend).unwrap();
let _ = terminal.draw(|frame| {
let _r = battery.draw(frame, Rect::new(3, 3, 10, 1));
let b = frame.buffer_mut();
println!("{:#?}", b);
});
assert_eq!(true, true)
}
}
Loading

0 comments on commit 93d4a1e

Please sign in to comment.