Skip to content

Commit 6c733ef

Browse files
author
Stephan Dilly
committed
remove necesity of a public custom Color type ColorDef
1 parent 5f8c333 commit 6c733ef

File tree

2 files changed

+74
-82
lines changed

2 files changed

+74
-82
lines changed

src/tabs/revlog/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,17 @@ impl Revlog {
166166

167167
txt.push(Text::Styled(
168168
Cow::from(&e.hash[0..7]),
169-
theme.table(0, selected),
169+
theme.commit_hash(selected),
170170
));
171171
txt.push(splitter.clone());
172172
txt.push(Text::Styled(
173173
Cow::from(e.time.as_str()),
174-
theme.table(1, selected),
174+
theme.commit_time(selected),
175175
));
176176
txt.push(splitter.clone());
177177
txt.push(Text::Styled(
178178
Cow::from(e.author.as_str()),
179-
theme.table(2, selected),
179+
theme.commit_author(selected),
180180
));
181181
txt.push(splitter.clone());
182182
txt.push(Text::Styled(

src/ui/style.rs

Lines changed: 71 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -8,81 +8,92 @@ use std::io::{Read, Write};
88
use std::path::PathBuf;
99
use tui::style::{Color, Modifier, Style};
1010

11-
#[derive(Serialize, Deserialize, Debug, Default, Clone, Copy)]
11+
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
1212
pub struct Theme {
13-
selected_tab: ColorDef,
14-
command_foreground: ColorDef,
15-
command_background: ColorDef,
16-
command_disabled: ColorDef,
17-
diff_line_add: ColorDef,
18-
diff_line_delete: ColorDef,
19-
diff_file_added: ColorDef,
20-
diff_file_removed: ColorDef,
21-
diff_file_moved: ColorDef,
22-
diff_file_modified: ColorDef,
23-
table_colors: [ColorDef; 3],
13+
#[serde(with = "ColorDef")]
14+
selected_tab: Color,
15+
#[serde(with = "ColorDef")]
16+
command_foreground: Color,
17+
#[serde(with = "ColorDef")]
18+
command_background: Color,
19+
#[serde(with = "ColorDef")]
20+
command_disabled: Color,
21+
#[serde(with = "ColorDef")]
22+
diff_line_add: Color,
23+
#[serde(with = "ColorDef")]
24+
diff_line_delete: Color,
25+
#[serde(with = "ColorDef")]
26+
diff_file_added: Color,
27+
#[serde(with = "ColorDef")]
28+
diff_file_removed: Color,
29+
#[serde(with = "ColorDef")]
30+
diff_file_moved: Color,
31+
#[serde(with = "ColorDef")]
32+
diff_file_modified: Color,
33+
#[serde(with = "ColorDef")]
34+
commit_hash: Color,
35+
#[serde(with = "ColorDef")]
36+
commit_time: Color,
37+
#[serde(with = "ColorDef")]
38+
commit_author: Color,
2439
}
2540

2641
pub const DARK_THEME: Theme = Theme {
27-
selected_tab: ColorDef::Yellow,
28-
command_foreground: ColorDef::White,
29-
command_background: ColorDef::Rgb(0, 0, 100),
30-
command_disabled: ColorDef::DarkGray,
31-
diff_line_add: ColorDef::Green,
32-
diff_line_delete: ColorDef::Red,
33-
diff_file_added: ColorDef::LightGreen,
34-
diff_file_removed: ColorDef::LightRed,
35-
diff_file_moved: ColorDef::LightMagenta,
36-
diff_file_modified: ColorDef::Yellow,
37-
table_colors: [
38-
ColorDef::Magenta,
39-
ColorDef::Blue,
40-
ColorDef::Green,
41-
],
42+
selected_tab: Color::Yellow,
43+
command_foreground: Color::White,
44+
command_background: Color::Rgb(0, 0, 100),
45+
command_disabled: Color::DarkGray,
46+
diff_line_add: Color::Green,
47+
diff_line_delete: Color::Red,
48+
diff_file_added: Color::LightGreen,
49+
diff_file_removed: Color::LightRed,
50+
diff_file_moved: Color::LightMagenta,
51+
diff_file_modified: Color::Yellow,
52+
commit_hash: Color::Magenta,
53+
commit_time: Color::Blue,
54+
commit_author: Color::Green,
4255
};
4356

4457
impl Theme {
4558
pub fn block(&self, focus: bool) -> Style {
4659
if focus {
4760
Style::default()
4861
} else {
49-
Style::default().fg(self.command_disabled.into())
62+
Style::default().fg(self.command_disabled)
5063
}
5164
}
5265

5366
pub fn tab(&self, selected: bool) -> Style {
5467
if selected {
55-
Style::default().fg(self.selected_tab.into())
68+
Style::default().fg(self.selected_tab)
5669
} else {
5770
Style::default()
5871
}
5972
}
6073

6174
pub fn text(&self, enabled: bool, selected: bool) -> Style {
6275
match (enabled, selected) {
63-
(false, _) => {
64-
Style::default().fg(self.command_disabled.into())
65-
}
76+
(false, _) => Style::default().fg(self.command_disabled),
6677
(true, false) => Style::default(),
6778
(true, true) => {
68-
Style::default().bg(self.command_background.into())
79+
Style::default().bg(self.command_background)
6980
}
7081
}
7182
}
7283

7384
pub fn item(&self, typ: StatusItemType, selected: bool) -> Style {
7485
let style = match typ {
7586
StatusItemType::New => {
76-
Style::default().fg(self.diff_file_added.into())
87+
Style::default().fg(self.diff_file_added)
7788
}
7889
StatusItemType::Modified => {
79-
Style::default().fg(self.diff_file_modified.into())
90+
Style::default().fg(self.diff_file_modified)
8091
}
8192
StatusItemType::Deleted => {
82-
Style::default().fg(self.diff_file_removed.into())
93+
Style::default().fg(self.diff_file_removed)
8394
}
8495
StatusItemType::Renamed => {
85-
Style::default().fg(self.diff_file_moved.into())
96+
Style::default().fg(self.diff_file_moved)
8697
}
8798
_ => Style::default(),
8899
};
@@ -92,7 +103,7 @@ impl Theme {
92103

93104
fn apply_select(&self, style: Style, selected: bool) -> Style {
94105
if selected {
95-
style.bg(self.command_background.into())
106+
style.bg(self.command_background)
96107
} else {
97108
style
98109
}
@@ -105,10 +116,10 @@ impl Theme {
105116
) -> Style {
106117
let style = match typ {
107118
DiffLineType::Add => {
108-
Style::default().fg(self.diff_line_add.into())
119+
Style::default().fg(self.diff_line_add)
109120
}
110121
DiffLineType::Delete => {
111-
Style::default().fg(self.diff_line_delete.into())
122+
Style::default().fg(self.diff_line_delete)
112123
}
113124
DiffLineType::Header => {
114125
Style::default().modifier(Modifier::BOLD)
@@ -120,21 +131,33 @@ impl Theme {
120131
}
121132

122133
pub fn text_danger(&self) -> Style {
123-
Style::default().fg(self.diff_file_removed.into())
134+
Style::default().fg(self.diff_file_removed)
124135
}
125136

126137
pub fn toolbar(&self, enabled: bool) -> Style {
127138
if enabled {
128-
Style::default().fg(self.command_foreground.into())
139+
Style::default().fg(self.command_foreground)
129140
} else {
130-
Style::default().fg(self.command_disabled.into())
141+
Style::default().fg(self.command_disabled)
131142
}
132-
.bg(self.command_background.into())
143+
.bg(self.command_background)
133144
}
134145

135-
pub fn table(&self, column: usize, selected: bool) -> Style {
146+
pub fn commit_hash(&self, selected: bool) -> Style {
147+
self.apply_select(
148+
Style::default().fg(self.commit_hash),
149+
selected,
150+
)
151+
}
152+
pub fn commit_time(&self, selected: bool) -> Style {
153+
self.apply_select(
154+
Style::default().fg(self.commit_time),
155+
selected,
156+
)
157+
}
158+
pub fn commit_author(&self, selected: bool) -> Style {
136159
self.apply_select(
137-
Style::default().fg(self.table_colors[column].into()),
160+
Style::default().fg(self.commit_author),
138161
selected,
139162
)
140163
}
@@ -182,7 +205,8 @@ impl Theme {
182205
/// we duplicate the Color definition from `tui` crate to implement Serde serialisation
183206
/// this enum can be removed once [tui-#292](https://github.com/fdehau/tui-rs/issues/292) is resolved
184207
#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
185-
pub enum ColorDef {
208+
#[serde(remote = "Color")]
209+
enum ColorDef {
186210
Reset,
187211
Black,
188212
Red,
@@ -203,35 +227,3 @@ pub enum ColorDef {
203227
Rgb(u8, u8, u8),
204228
Indexed(u8),
205229
}
206-
207-
impl Default for ColorDef {
208-
fn default() -> Self {
209-
ColorDef::Reset
210-
}
211-
}
212-
213-
impl From<ColorDef> for Color {
214-
fn from(def: ColorDef) -> Self {
215-
match def {
216-
ColorDef::Reset => Color::Reset,
217-
ColorDef::Black => Color::Black,
218-
ColorDef::Red => Color::Red,
219-
ColorDef::Green => Color::Green,
220-
ColorDef::Yellow => Color::Yellow,
221-
ColorDef::Blue => Color::Blue,
222-
ColorDef::Magenta => Color::Magenta,
223-
ColorDef::Cyan => Color::Cyan,
224-
ColorDef::Gray => Color::Gray,
225-
ColorDef::DarkGray => Color::DarkGray,
226-
ColorDef::LightRed => Color::LightRed,
227-
ColorDef::LightGreen => Color::LightGreen,
228-
ColorDef::LightYellow => Color::LightYellow,
229-
ColorDef::LightBlue => Color::LightBlue,
230-
ColorDef::LightMagenta => Color::LightMagenta,
231-
ColorDef::LightCyan => Color::LightCyan,
232-
ColorDef::White => Color::White,
233-
ColorDef::Rgb(a, b, c) => Color::Rgb(a, b, c),
234-
ColorDef::Indexed(x) => Color::Indexed(x),
235-
}
236-
}
237-
}

0 commit comments

Comments
 (0)