-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from jw/cpu_graph
Added cpu graph to process list and updated dependencies.
- Loading branch information
Showing
12 changed files
with
645 additions
and
336 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ use crate::{ | |
tui::{Event, Frame}, | ||
}; | ||
|
||
pub mod battery; | ||
pub mod fps; | ||
pub mod process; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.