Skip to content

Commit 939ced7

Browse files
author
Peixian Wang
committed
Better color scheme + naming
1 parent 5ae9a8b commit 939ced7

File tree

5 files changed

+124
-83
lines changed

5 files changed

+124
-83
lines changed

src/app.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ pub enum SelectedTab {
9696
#[default]
9797
#[strum(to_string = "General Information")]
9898
General,
99-
// #[strum(to_string = "Index")]
100-
// Index,
10199
#[strum(to_string = "Process Group Info")]
102100
ProcessGroup,
103101
#[strum(to_string = "Process Info")]
@@ -169,11 +167,15 @@ impl Default for App<'_> {
169167

170168
impl App<'_> {
171169
/// Constructs a new instance of [`App`].
172-
pub fn new(filepath: String) -> Self {
170+
pub fn new(filepath: String, colors: Option<CommonColors> ) -> Self {
173171
let now = Instant::now();
174172
let parser = parser::CDParser::new(&filepath).unwrap();
175173
let mut ret = Self::default();
174+
176175
ret.filepath = filepath.clone();
176+
if let Some(colors) = colors {
177+
ret.colors = colors;
178+
}
177179

178180
ret.index_map = parser.build_index().unwrap();
179181
ret.crash_dump = parser.parse(&ret.index_map).unwrap();
@@ -455,17 +457,17 @@ impl App<'_> {
455457

456458
pub fn get_heap_info(&self, pid: &str) -> io::Result<Text> {
457459
self.parser
458-
.get_heap_info(&self.crash_dump, &self.filepath, pid)
460+
.get_heap_info(&self.crash_dump, &self.filepath, pid, &self.colors)
459461
}
460462

461463
pub fn get_stack_info(&self, pid: &str) -> io::Result<Text> {
462464
self.parser
463-
.get_stack_info(&self.crash_dump, &self.filepath, pid)
465+
.get_stack_info(&self.crash_dump, &self.filepath, pid, &self.colors)
464466
}
465467

466468
pub fn get_message_queue_info(&self, pid: &str) -> io::Result<Text> {
467469
self.parser
468-
.get_message_queue_info(&self.crash_dump, &self.filepath, pid)
470+
.get_message_queue_info(&self.crash_dump, &self.filepath, pid, &self.colors)
469471
}
470472
}
471473

@@ -583,7 +585,7 @@ impl Widget for &mut App<'_> {
583585
),
584586
]))
585587
.border_style(Style::default().fg(self.colors.border_color))
586-
.style(Style::default().bg(Color::Black));
588+
.style(Style::default().bg(self.colors.background_color));
587589

588590
let paragraph = Paragraph::new(help_text)
589591
.block(block)
@@ -656,29 +658,29 @@ impl SelectedTab {
656658
let memory_information_header = Line::from(vec![
657659
Span::styled(
658660
"Memory Information:",
659-
Style::default().fg(app.colors.header_text),
661+
Style::default().fg(app.colors.info_preamble),
660662
),
661663
Span::raw("\n"),
662664
]);
663665

664666
let process_count = Line::from(vec![
665-
Span::styled("Process Count: ", Style::default().fg(app.colors.info_text)),
667+
Span::styled("Process Count: ", Style::default().fg(app.colors.info_preamble)),
666668
Span::styled(
667669
process_count.to_string(),
668670
Style::default().fg(app.colors.default_text),
669671
),
670672
]);
671673

672674
let ets_count = Line::from(vec![
673-
Span::styled("ETS Tables: ", Style::default().fg(app.colors.info_text)),
675+
Span::styled("ETS Tables: ", Style::default().fg(app.colors.info_preamble)),
674676
Span::styled(
675677
ets_count.to_string(),
676678
Style::default().fg(app.colors.default_text),
677679
),
678680
]);
679681

680682
let fn_count = Line::from(vec![
681-
Span::styled("Funs: ", Style::default().fg(app.colors.info_text)),
683+
Span::styled("Funs: ", Style::default().fg(app.colors.info_preamble)),
682684
Span::styled(
683685
fn_count.to_string(),
684686
Style::default().fg(app.colors.default_text),
@@ -753,7 +755,7 @@ impl SelectedTab {
753755
InfoOrIndex::Info(ref proc_info) => {
754756
let proc_info: &types::ProcInfo = proc_info;
755757
active_proc_info = proc_info.clone();
756-
active_proc_info.format_as_ratatui_text()
758+
active_proc_info.format_as_ratatui_text(&app.colors)
757759
}
758760
InfoOrIndex::Index(_) => {
759761
Text::raw(format!("Index for pid: {:?}", selected_pid).to_string())
@@ -832,7 +834,7 @@ impl SelectedTab {
832834
InfoOrIndex::Info(ref proc_info) => {
833835
let proc_info: &types::ProcInfo = proc_info;
834836
active_proc_info = proc_info.clone();
835-
active_proc_info.format_as_ratatui_text()
837+
active_proc_info.format_as_ratatui_text(&app.colors)
836838
}
837839
InfoOrIndex::Index(_) => {
838840
Text::raw(format!("Index for pid: {:?}", selected_pid).to_string())

src/config.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ pub struct CommonColors {
2222
pub header_text: Color,
2323
pub header_background: Color,
2424
pub highlight_background: Color, // e.g., for selected items background
25-
pub error_text: Color,
26-
pub warning_text: Color,
25+
pub info_preamble: Color,
2726
pub info_text: Color,
28-
// Add other commonly used colors if needed
27+
2928
pub border_color: Color,
3029
pub title_color: Color,
30+
31+
pub alt_color_1: Color,
32+
pub alt_color_2: Color,
33+
pub alt_color_3: Color,
34+
pub background_color: Color
3135
}
3236

3337
impl Default for CommonColors {
@@ -39,11 +43,14 @@ impl Default for CommonColors {
3943
header_text: Color::White,
4044
header_background: Color::Blue,
4145
highlight_background: Color::DarkGray,
42-
error_text: Color::Red,
43-
warning_text: Color::Yellow,
44-
info_text: Color::Cyan,
46+
info_preamble: Color::Yellow,
47+
info_text: Color::White,
4548
border_color: Color::Gray,
4649
title_color: Color::White,
50+
alt_color_1: Color::Blue,
51+
alt_color_2: Color::Magenta,
52+
alt_color_3: Color::Red,
53+
background_color: Color::Black,
4754
}
4855
}
4956
}

src/main.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ use crate::{
2121
event::{Event, EventHandler},
2222
handler::handle_key_events,
2323
tui::Tui,
24+
config::CommonColors,
2425
};
26+
use ratatui::style::Color;
27+
2528

2629
pub mod app;
2730
pub mod config;
@@ -44,6 +47,10 @@ struct Args {
4447
/// Path to the crash dump
4548
#[arg(required = true)]
4649
filepath: String,
50+
51+
/// Turns on light mode
52+
#[clap(long, short, action)]
53+
light_mode: bool,
4754
}
4855

4956
#[tokio::main]
@@ -52,7 +59,28 @@ async fn main() -> AppResult<()> {
5259

5360
if args.action == "tui" {
5461
// Create an application.
55-
let mut app = App::new(args.filepath);
62+
63+
let colors = if !args.light_mode {
64+
CommonColors::default()
65+
} else {
66+
CommonColors {
67+
default_text: Color::Black,
68+
highlight_text: Color::Gray,
69+
header_text: Color::Black,
70+
header_background: Color::Yellow,
71+
highlight_background: Color::DarkGray,
72+
info_preamble: Color::Blue,
73+
info_text: Color::Cyan,
74+
border_color: Color::Gray,
75+
title_color: Color::Black,
76+
alt_color_1: Color::Blue,
77+
alt_color_2: Color::Magenta,
78+
alt_color_3: Color::Red,
79+
background_color: Color::White,
80+
}
81+
};
82+
83+
let mut app = App::new(args.filepath, Some(colors));
5684

5785
// Initialize the terminal user interface.
5886
let backend = CrosstermBackend::new(io::stdout());

src/parser/parser.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
//! information from the crash dump.
1818
1919
use crate::parser::*;
20+
use crate::config::CommonColors;
2021
use dashmap::DashMap;
2122
use grep::{
2223
regex::RegexMatcher,
@@ -222,14 +223,13 @@ impl CDParser {
222223
crash_dump: &'a CrashDump,
223224
filepath: &String,
224225
id: &str,
226+
colors: &CommonColors,
225227
) -> io::Result<Text<'a>> {
226228
// seeks to the file using the byteoffsets in the dict and just retrives the raw data
227-
//println!("{:?}", filepath);
228-
// println!("{:?}, {:#?}", id, crash_dump.processes_heap.get(id));
229229
if let Some(process_heap_ref) = crash_dump.processes_heap.get(id) {
230230
if let InfoOrIndex::Index(ref heap_index) = *process_heap_ref.value() {
231231
let file = OpenOptions::new().read(true).open(filepath)?;
232-
return crash_dump.load_proc_heap(heap_index, &file);
232+
return crash_dump.load_proc_heap(heap_index, &file, colors);
233233
}
234234
}
235235
Ok(Text::from(""))
@@ -240,12 +240,13 @@ impl CDParser {
240240
crash_dump: &'a CrashDump,
241241
filepath: &String,
242242
id: &str,
243+
colors: &CommonColors,
243244
) -> io::Result<Text<'a>> {
244245
if let Some(stack_info_ref) = crash_dump.processes_stack.get(id) {
245246
if let InfoOrIndex::Index(ref stack_index) = *stack_info_ref.value() {
246247
let file = OpenOptions::new().read(true).open(filepath)?;
247248

248-
return crash_dump.load_proc_stack(stack_index, &file);
249+
return crash_dump.load_proc_stack(stack_index, &file, colors);
249250
}
250251
}
251252
Ok(Text::from(""))
@@ -256,12 +257,13 @@ impl CDParser {
256257
crash_dump: &'a CrashDump,
257258
filepath: &String,
258259
id: &str,
260+
colors: &CommonColors,
259261
) -> io::Result<Text<'a>> {
260262
if let Some(mq_index_ref) = crash_dump.processes_messages.get(id) {
261263
if let InfoOrIndex::Index(ref mq_index) = *mq_index_ref.value() {
262264
let file = OpenOptions::new().read(true).open(filepath)?;
263265

264-
return crash_dump.load_proc_message_queue(mq_index, &file);
266+
return crash_dump.load_proc_message_queue(mq_index, &file, colors);
265267
}
266268
}
267269

0 commit comments

Comments
 (0)