Skip to content

Commit 4ecd614

Browse files
committed
Refacrored to extract themes
1 parent 888113b commit 4ecd614

File tree

10 files changed

+175
-88
lines changed

10 files changed

+175
-88
lines changed

src/app.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use crate::dbgp::client::Property;
99
use crate::dbgp::client::StackGetResponse;
1010
use crate::event::input::AppEvent;
1111
use crate::notification::Notification;
12+
use crate::theme::Scheme;
13+
use crate::theme::Theme;
1214
use crate::view::help::HelpView;
1315
use crate::view::layout::LayoutView;
1416
use crate::view::listen::ListenView;
@@ -152,6 +154,7 @@ pub struct App {
152154

153155
pub snapshot_notify: Arc<Notify>,
154156
pub context_depth: u8,
157+
pub theme: Theme,
155158

156159
pub analyzed_files: AnalyzedFiles,
157160
}
@@ -172,6 +175,7 @@ impl App {
172175
counter: 0,
173176
context_depth: 4,
174177

178+
theme: Theme::Dark,
175179
server_status: None,
176180
command_input: Input::default(),
177181
command_response: None,
@@ -509,4 +513,8 @@ impl App {
509513
}
510514
Ok(())
511515
}
516+
517+
pub(crate) fn theme(&self) -> Scheme {
518+
self.theme.scheme()
519+
}
512520
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub mod event;
55
pub mod notification;
66
pub mod view;
77
pub mod analyzer;
8+
pub mod theme;
89

910
use app::App;
1011
use better_panic::Settings;

src/theme.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use ratatui::style::{Color, Style};
2+
3+
pub enum Theme {
4+
Dark,
5+
}
6+
7+
impl Theme {
8+
pub fn scheme(&self) -> Scheme {
9+
match self {
10+
Theme::Dark => Scheme{
11+
syntax_variable: Style::default().fg(Color::LightBlue),
12+
syntax_type: Style::default().fg(Color::LightRed),
13+
syntax_type_object: Style::default().fg(Color::LightMagenta),
14+
syntax_literal: Style::default().fg(Color::LightBlue),
15+
syntax_literal_string: Style::default().fg(Color::LightGreen),
16+
syntax_label: Style::default().fg(Color::White),
17+
syntax_brace: Style::default().fg(Color::White),
18+
19+
notification_info: Style::default().fg(Color::Black).bg(Color::Green),
20+
notification_error: Style::default().fg(Color::White).bg(Color::Red),
21+
22+
pane_border_active: Style::default().fg(Color::Green),
23+
pane_border_inactive: Style::default().fg(Color::DarkGray),
24+
25+
source_line: Style::default(),
26+
source_line_no: Style::default().fg(Color::Yellow),
27+
source_line_highlight: Style::default().bg(Color::Blue),
28+
source_annotation: Style::default().fg(Color::DarkGray),
29+
30+
stack_line: Style::default().fg(Color::White),
31+
},
32+
}
33+
}
34+
}
35+
36+
pub struct Scheme {
37+
pub syntax_variable: Style,
38+
pub syntax_type: Style,
39+
pub syntax_type_object: Style,
40+
pub syntax_literal: Style,
41+
pub syntax_literal_string: Style,
42+
pub syntax_label: Style,
43+
pub syntax_brace: Style,
44+
45+
pub notification_info: Style,
46+
pub notification_error: Style,
47+
48+
pub pane_border_active: Style,
49+
pub pane_border_inactive: Style,
50+
51+
pub source_line: Style,
52+
pub source_line_no: Style,
53+
pub source_line_highlight: Style,
54+
pub source_annotation: Style,
55+
56+
pub stack_line: Style,
57+
}
58+
59+
pub enum Role {
60+
}
61+
62+

src/view/context.rs

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use super::View;
22
use crate::app::App;
33
use crate::dbgp::client::Property;
4+
use crate::dbgp::client::PropertyType;
45
use crate::event::input::AppEvent;
6+
use crate::theme::Scheme;
57
use ratatui::layout::Rect;
6-
use ratatui::style::Color;
7-
use ratatui::style::Style;
88
use ratatui::text::Line;
99
use ratatui::text::Span;
1010
use ratatui::widgets::Paragraph;
@@ -28,7 +28,7 @@ impl View for ContextComponent {
2828
None => return,
2929
};
3030
let mut lines: Vec<Line> = vec![];
31-
draw_properties(&context.properties, &mut lines, 0);
31+
draw_properties(&app.theme(), &context.properties, &mut lines, 0);
3232

3333
frame.render_widget(
3434
Paragraph::new(lines)
@@ -39,37 +39,58 @@ impl View for ContextComponent {
3939
}
4040
}
4141

42-
pub fn draw_properties(properties: &Vec<Property>, lines: &mut Vec<Line>, level: usize) {
42+
pub fn draw_properties(
43+
theme: &Scheme,
44+
properties: &Vec<Property>,
45+
lines: &mut Vec<Line>,
46+
level: usize,
47+
) {
4348
for property in properties {
44-
let value = property.value.clone().unwrap_or("".to_string());
45-
lines.push(Line::from(vec![
49+
let mut spans = vec![
4650
Span::raw(" ".repeat(level)),
47-
Span::styled(property.name.to_string(), Style::default().fg(Color::White)),
51+
Span::styled(property.name.to_string(), theme.syntax_label),
4852
Span::raw(" ".to_string()),
4953
Span::styled(
50-
property.property_type.to_string(),
51-
Style::default().fg(Color::Blue),
54+
property.type_name(),
55+
match property.property_type {
56+
PropertyType::Object => theme.syntax_type_object,
57+
_ => theme.syntax_type,
58+
},
5259
),
5360
Span::raw(" = ".to_string()),
54-
match property.property_type.as_str() {
55-
"bool" => Span::styled(value, Style::default().fg(Color::LightRed)),
56-
"int" => Span::styled(value, Style::default().fg(Color::LightBlue)),
57-
"float" => Span::styled(value, Style::default().fg(Color::LightBlue)),
58-
"string" => Span::styled(value, Style::default().fg(Color::LightGreen)),
59-
"array" => Span::styled(value, Style::default().fg(Color::Cyan)),
60-
"hash" => Span::styled(value, Style::default().fg(Color::Cyan)),
61-
"object" => match &property.classname {
62-
Some(name) => Span::styled(name.to_string(), Style::default().fg(Color::Red)),
63-
None => Span::styled(value, Style::default().fg(Color::Red)),
64-
},
65-
"resource" => Span::styled(value, Style::default().fg(Color::Red)),
66-
"undefined" => Span::styled(value, Style::default().fg(Color::White)),
67-
_ => Span::raw(value),
68-
},
69-
]));
61+
render_value(theme, property),
62+
];
63+
64+
let delimiters = match property.property_type {
65+
PropertyType::Array => ("[", "]"),
66+
_ => ("{", "}"),
67+
};
7068

7169
if !property.children.is_empty() {
72-
draw_properties(&property.children, lines, level + 1);
70+
spans.push(Span::raw(delimiters.0).style(theme.syntax_brace));
7371
}
72+
73+
lines.push(Line::from(spans));
74+
75+
if !property.children.is_empty() {
76+
draw_properties(theme, &property.children, lines, level + 1);
77+
lines.push(Line::from(vec![Span::raw(delimiters.1)]).style(theme.syntax_brace));
78+
}
79+
}
80+
}
81+
82+
pub fn render_value<'a>(theme: &Scheme, property: &Property) -> Span<'a> {
83+
let value = property.value.clone().unwrap_or("".to_string());
84+
match property.property_type {
85+
PropertyType::Bool => Span::styled(value, theme.syntax_literal),
86+
PropertyType::Int => Span::styled(value, theme.syntax_literal),
87+
PropertyType::Float => Span::styled(value, theme.syntax_literal),
88+
PropertyType::String => Span::styled(format!("\"{}\"", value), theme.syntax_literal_string),
89+
PropertyType::Array => Span::styled(value, theme.syntax_literal),
90+
PropertyType::Hash => Span::styled(value, theme.syntax_literal),
91+
PropertyType::Object => Span::styled(value, theme.syntax_literal),
92+
PropertyType::Resource => Span::styled(value, theme.syntax_literal),
93+
PropertyType::Undefined => Span::styled(value, theme.syntax_literal),
94+
_ => Span::styled(value, theme.syntax_literal),
7495
}
7596
}

src/view/debug.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/view/layout.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use ratatui::style::Style;
1616
use ratatui::style::Stylize;
1717
use ratatui::text::Line;
1818
use ratatui::text::Span;
19+
use ratatui::widgets::Clear;
1920
use ratatui::widgets::Paragraph;
2021
use ratatui::Frame;
2122

@@ -37,6 +38,7 @@ impl View for LayoutView {
3738
.constraints(constraints)
3839
.split(area);
3940

41+
f.render_widget(Clear::default(), rows[0]);
4042
f.render_widget(status_widget(app), rows[0]);
4143

4244
match app.view_current {
@@ -99,15 +101,11 @@ fn status_widget(app: &App) -> Paragraph {
99101
true => format!(" {} ", app.notification.message.clone()),
100102
false => "".to_string(),
101103
},
102-
Style::default()
103-
.fg(match app.notification.level {
104-
NotificationLevel::Info => Color::Green,
105-
_ => Color::White,
106-
})
107-
.bg(match app.notification.level {
108-
NotificationLevel::Error => Color::Red,
109-
_ => Color::Black,
110-
}),
104+
match app.notification.level {
105+
NotificationLevel::Error => app.theme().notification_error,
106+
NotificationLevel::Info => app.theme().notification_info,
107+
NotificationLevel::None => Style::default(),
108+
},
111109
),
112110
])])
113111
}

src/view/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
pub mod debug;
21
pub mod layout;
32
pub mod listen;
43
pub mod session;

src/view/session.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ use crossterm::event::KeyCode;
1111
use ratatui::layout::Constraint;
1212
use ratatui::layout::Layout;
1313
use ratatui::layout::Rect;
14-
use ratatui::style::Color;
15-
use ratatui::style::Style;
1614
use ratatui::widgets::Block;
1715
use ratatui::widgets::Borders;
16+
use ratatui::widgets::Clear;
1817
use ratatui::Frame;
1918

2019
pub struct SessionView {}
@@ -125,14 +124,14 @@ fn build_pane_widget(frame: &mut Frame, app: &App, pane: &Pane, area: Rect, inde
125124
ComponentType::Stack => "Stack".to_string(),
126125
})
127126
.style(
128-
Style::default().fg(if index == app.session_view.current_pane {
129-
Color::Green
130-
} else {
131-
Color::DarkGray
132-
}),
127+
match index == app.session_view.current_pane {
128+
true => app.theme().pane_border_active,
129+
false => app.theme().pane_border_inactive,
130+
}
133131
);
134132

135133
frame.render_widget(&block, area);
134+
frame.render_widget(Clear::default(), block.inner(area));
136135

137136
match pane.component_type {
138137
ComponentType::Source => {

0 commit comments

Comments
 (0)