Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

kdash-rs / kdash / 2304089135

11 May 2022 - 0:27 coverage increased (+0.002%) to 41.216%
2304089135

Pull #201

github

GitHub
Merge 078834834 into 620d6b7bd
Pull Request #201: Bump clap from 3.1.8 to 3.1.18

915 of 4123 branches covered (22.19%)

Branch coverage included in aggregate %.

2541 of 4262 relevant lines covered (59.62%)

8.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

42.37
/src/ui/utils.rs
1
use std::collections::BTreeMap;
2

3
use tui::{
4
  backend::Backend,
5
  layout::{Constraint, Direction, Layout, Rect},
6
  style::{Color, Modifier, Style},
7
  symbols,
8
  text::{Span, Spans, Text},
9
  widgets::{Block, Borders, Paragraph, Row},
10
  Frame,
11
};
12
// Utils
13

14
// default colors
15
pub const COLOR_TEAL: Color = Color::Rgb(35, 50, 55);
16
pub const COLOR_CYAN: Color = Color::Rgb(0, 230, 230);
17
pub const COLOR_LIGHT_BLUE: Color = Color::Rgb(138, 196, 255);
18
pub const COLOR_YELLOW: Color = Color::Rgb(249, 229, 113);
19
pub const COLOR_GREEN: Color = Color::Rgb(72, 213, 150);
20
pub const COLOR_RED: Color = Color::Rgb(249, 167, 164);
21
pub const COLOR_ORANGE: Color = Color::Rgb(255, 170, 66);
22
pub const COLOR_WHITE: Color = Color::Rgb(255, 255, 255);
23
// light theme colors
24
pub const COLOR_MAGENTA: Color = Color::Rgb(139, 0, 139);
25
pub const COLOR_GRAY: Color = Color::Rgb(91, 87, 87);
26
pub const COLOR_BLUE: Color = Color::Rgb(0, 82, 163);
27
pub const COLOR_GREEN_DARK: Color = Color::Rgb(20, 97, 73);
28
pub const COLOR_RED_DARK: Color = Color::Rgb(173, 25, 20);
29
pub const COLOR_ORANGE_DARK: Color = Color::Rgb(184, 49, 15);
30

31
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
Branches [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7], [0, 8], [0, 9], [0, 11], [0, 12], [0, 13]] missed. 1,249×
32
pub enum Styles {
33
  Default,
34
  Logo,
35
  Failure,
36
  Warning,
37
  Success,
38
  Primary,
39
  Secondary,
40
  Help,
41
  Background,
42
}
43

44
pub fn theme_styles(light: bool) -> BTreeMap<Styles, Style> {
60×
45
  if light {
Branches [[0, 1]] missed. 60×
46
    BTreeMap::from([
!
47
      (Styles::Default, Style::default().fg(COLOR_GRAY)),
!
48
      (Styles::Logo, Style::default().fg(COLOR_GREEN_DARK)),
!
49
      (Styles::Failure, Style::default().fg(COLOR_RED_DARK)),
!
50
      (Styles::Warning, Style::default().fg(COLOR_ORANGE_DARK)),
!
51
      (Styles::Success, Style::default().fg(COLOR_GREEN_DARK)),
!
52
      (Styles::Primary, Style::default().fg(COLOR_BLUE)),
!
53
      (Styles::Secondary, Style::default().fg(COLOR_MAGENTA)),
!
54
      (Styles::Help, Style::default().fg(COLOR_BLUE)),
!
55
      (
!
UNCOV
56
        Styles::Background,
!
57
        Style::default().bg(COLOR_WHITE).fg(COLOR_GRAY),
!
58
      ),
59
    ])
60
  } else {
61
    BTreeMap::from([
60×
62
      (Styles::Default, Style::default().fg(COLOR_WHITE)),
60×
63
      (Styles::Logo, Style::default().fg(COLOR_GREEN)),
60×
64
      (Styles::Failure, Style::default().fg(COLOR_RED)),
60×
65
      (Styles::Warning, Style::default().fg(COLOR_ORANGE)),
60×
66
      (Styles::Success, Style::default().fg(COLOR_GREEN)),
60×
67
      (Styles::Primary, Style::default().fg(COLOR_CYAN)),
60×
68
      (Styles::Secondary, Style::default().fg(COLOR_YELLOW)),
60×
69
      (Styles::Help, Style::default().fg(COLOR_LIGHT_BLUE)),
60×
70
      (
60×
71
        Styles::Background,
60×
72
        Style::default().bg(COLOR_TEAL).fg(COLOR_WHITE),
60×
73
      ),
74
    ])
75
  }
76
}
60×
77

78
pub fn title_style(txt: &str) -> Span<'_> {
1×
79
  Span::styled(txt, style_bold())
1×
80
}
1×
81

82
pub fn title_style_logo(txt: &str, light: bool) -> Span<'_> {
!
83
  Span::styled(
!
84
    txt,
85
    style_logo(light)
!
86
      .add_modifier(Modifier::BOLD)
87
      .add_modifier(Modifier::ITALIC),
88
  )
89
}
!
90

91
pub fn style_bold() -> Style {
1×
92
  Style::default().add_modifier(Modifier::BOLD)
1×
93
}
1×
94

95
pub fn style_default(light: bool) -> Style {
15×
96
  *theme_styles(light).get(&Styles::Default).unwrap()
15×
97
}
15×
98
pub fn style_logo(light: bool) -> Style {
!
99
  *theme_styles(light).get(&Styles::Logo).unwrap()
!
100
}
!
101
pub fn style_failure(light: bool) -> Style {
1×
102
  *theme_styles(light).get(&Styles::Failure).unwrap()
1×
103
}
1×
104
pub fn style_warning(light: bool) -> Style {
!
105
  *theme_styles(light).get(&Styles::Warning).unwrap()
!
106
}
!
107
pub fn style_success(light: bool) -> Style {
!
108
  *theme_styles(light).get(&Styles::Success).unwrap()
!
109
}
!
110
pub fn style_primary(light: bool) -> Style {
37×
111
  *theme_styles(light).get(&Styles::Primary).unwrap()
37×
112
}
37×
113
pub fn style_help(light: bool) -> Style {
!
114
  *theme_styles(light).get(&Styles::Help).unwrap()
!
115
}
!
116

117
pub fn style_secondary(light: bool) -> Style {
7×
118
  *theme_styles(light).get(&Styles::Secondary).unwrap()
7×
119
}
7×
120

121
pub fn style_main_background(light: bool) -> Style {
!
122
  *theme_styles(light).get(&Styles::Background).unwrap()
!
123
}
!
124

125
pub fn style_highlight() -> Style {
3×
126
  Style::default().add_modifier(Modifier::REVERSED)
3×
127
}
3×
128

129
pub fn get_gauge_style(enhanced_graphics: bool) -> symbols::line::Set {
!
130
  if enhanced_graphics {
Branches [[0, 0], [0, 1]] missed. !
131
    symbols::line::THICK
!
132
  } else {
133
    symbols::line::NORMAL
!
134
  }
135
}
!
136

137
pub fn table_header_style(cells: Vec<&str>, light: bool) -> Row<'_> {
2×
138
  Row::new(cells).style(style_default(light)).bottom_margin(0)
2×
139
}
2×
140

141
pub fn horizontal_chunks(constraints: Vec<Constraint>, size: Rect) -> Vec<Rect> {
!
142
  Layout::default()
!
143
    .constraints(<Vec<Constraint> as AsRef<[Constraint]>>::as_ref(
!
144
      &constraints,
145
    ))
146
    .direction(Direction::Horizontal)
!
147
    .split(size)
!
148
}
!
149

150
pub fn horizontal_chunks_with_margin(
!
151
  constraints: Vec<Constraint>,
152
  size: Rect,
153
  margin: u16,
154
) -> Vec<Rect> {
155
  Layout::default()
!
156
    .constraints(<Vec<Constraint> as AsRef<[Constraint]>>::as_ref(
!
157
      &constraints,
158
    ))
159
    .direction(Direction::Horizontal)
!
160
    .margin(margin)
161
    .split(size)
!
162
}
!
163

164
pub fn vertical_chunks(constraints: Vec<Constraint>, size: Rect) -> Vec<Rect> {
1×
165
  Layout::default()
2×
166
    .constraints(<Vec<Constraint> as AsRef<[Constraint]>>::as_ref(
1×
167
      &constraints,
168
    ))
169
    .direction(Direction::Vertical)
1×
170
    .split(size)
1×
171
}
1×
172

173
pub fn vertical_chunks_with_margin(
1×
174
  constraints: Vec<Constraint>,
175
  size: Rect,
176
  margin: u16,
177
) -> Vec<Rect> {
178
  Layout::default()
2×
179
    .constraints(<Vec<Constraint> as AsRef<[Constraint]>>::as_ref(
1×
180
      &constraints,
181
    ))
182
    .direction(Direction::Vertical)
1×
183
    .margin(margin)
184
    .split(size)
1×
185
}
1×
186

187
pub fn layout_block(title: Span<'_>) -> Block<'_> {
1×
188
  Block::default().borders(Borders::ALL).title(title)
1×
189
}
1×
190

191
pub fn layout_block_default(title: &str) -> Block<'_> {
1×
192
  layout_block(title_style(title))
1×
193
}
1×
194

195
pub fn layout_block_active(title: &str, light: bool) -> Block<'_> {
!
196
  layout_block(title_style(title)).style(style_secondary(light))
!
197
}
!
198

199
pub fn layout_block_active_span(title: Spans<'_>, light: bool) -> Block<'_> {
1×
200
  Block::default()
2×
201
    .borders(Borders::ALL)
202
    .title(title)
1×
203
    .style(style_secondary(light))
1×
204
}
1×
205

206
pub fn layout_block_top_border(title: Spans<'_>) -> Block<'_> {
2×
207
  Block::default().borders(Borders::TOP).title(title)
2×
208
}
2×
209

210
pub fn title_with_dual_style<'a>(part_1: String, part_2: String, light: bool) -> Spans<'a> {
3×
211
  Spans::from(vec![
6×
212
    Span::styled(part_1, style_secondary(light).add_modifier(Modifier::BOLD)),
3×
213
    Span::styled(part_2, style_default(light).add_modifier(Modifier::BOLD)),
3×
214
  ])
215
}
3×
216

217
/// helper function to create a centered rect using up
218
/// certain percentage of the available rect `r`
219
pub fn centered_rect(width: u16, height: u16, r: Rect) -> Rect {
!
220
  let Rect {
221
    width: grid_width,
!
222
    height: grid_height,
!
223
    ..
224
  } = r;
225
  let outer_height = (grid_height / 2).saturating_sub(height / 2);
!
226

227
  let popup_layout = Layout::default()
!
228
    .direction(Direction::Vertical)
!
229
    .constraints(
230
      [
!
231
        Constraint::Length(outer_height),
!
232
        Constraint::Length(height),
!
233
        Constraint::Length(outer_height),
!
234
      ]
235
      .as_ref(),
236
    )
237
    .split(r);
!
238

239
  let outer_width = (grid_width / 2).saturating_sub(width / 2);
!
240

241
  Layout::default()
!
242
    .direction(Direction::Horizontal)
!
243
    .constraints(
244
      [
!
245
        Constraint::Length(outer_width),
!
246
        Constraint::Length(width),
!
247
        Constraint::Length(outer_width),
!
248
      ]
249
      .as_ref(),
250
    )
251
    .split(popup_layout[1])[1]
!
252
}
!
253

254
pub fn loading<B: Backend>(
!
255
  f: &mut Frame<'_, B>,
256
  block: Block<'_>,
257
  area: Rect,
258
  is_loading: bool,
259
  light: bool,
260
) {
261
  if is_loading {
Branches [[0, 0], [0, 1], [0, 2], [0, 3]] missed. !
262
    let text = "\n\n Loading ...\n\n".to_owned();
!
263
    let mut text = Text::from(text);
!
264
    text.patch_style(style_secondary(light));
!
265

266
    // Contains the text
267
    let paragraph = Paragraph::new(text)
!
268
      .style(style_secondary(light))
!
269
      .block(block);
!
270
    f.render_widget(paragraph, area);
!
271
  } else {
272
    f.render_widget(block, area)
!
273
  }
274
}
!
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2022 Coveralls, Inc