Skip to content

Commit 919edc1

Browse files
committed
changed: some clones to borrows
1 parent 74704f5 commit 919edc1

File tree

2 files changed

+45
-38
lines changed

2 files changed

+45
-38
lines changed

src/app/connection.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ impl Connection {
5353
self.log.push_front(code);
5454
}
5555

56-
pub fn log(&self) -> VecDeque<Result<u16, ()>> {
57-
self.log.clone()
56+
pub const fn log(&self) -> &VecDeque<Result<u16, ()>> {
57+
&self.log
5858
}
5959

60-
pub fn addr(&self) -> String {
60+
pub fn addr(&self) -> Cow<'_, str> {
6161
match &self.conn_type {
62-
ConnectionType::Web { url } => url.clone(),
63-
ConnectionType::Local { ip } => ip.to_string(),
62+
ConnectionType::Web { url } => Cow::Borrowed(url),
63+
ConnectionType::Local { ip } => Cow::Owned(ip.to_string()),
6464
}
6565
}
6666
}

src/ui.rs

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,41 +32,29 @@ fn render_tab_live(f: &mut Frame, app: &App) {
3232
let mut list_items: Vec<Line<'_>> = Vec::new();
3333

3434
for conn in &conns {
35-
// Compute online status color.
36-
// Green is OK, red is bad, etc.
37-
let status_color = {
38-
if conn.log().front().is_none() {
39-
Color::Gray // Requests have not been sent yet.
40-
} else {
41-
match conn.log()[0].as_ref() {
42-
Ok(code) => match code {
43-
200 => Color::Green,
44-
_ => Color::Yellow,
45-
},
46-
_ => Color::Red,
47-
}
48-
}
49-
};
35+
let color = conn
36+
.log()
37+
.front()
38+
.map_or(Color::Gray, |f| status_to_color(*f, &conn.conn_type));
5039

5140
let url = conn.addr();
5241

5342
let conn_output: Line<'_> = match app.output_fmt {
5443
OutputFmt::Bullet => Line::from(vec![
55-
Span::from(" ■ ").style(status_color),
56-
Span::from(format!("{} ({})", conn.name.clone(), url)),
44+
Span::from(" ■ ").style(color),
45+
Span::from(format!("{} ({})", conn.name, url)),
5746
]),
58-
OutputFmt::Line => Line::from(Span::from(format!(" {} ({})", conn.name.clone(), url)))
59-
.style(
60-
Style::new()
61-
.bg(status_color)
62-
.fg(if status_color == Color::DarkGray {
63-
Color::DarkGray
64-
} else {
65-
Color::Black
66-
})
67-
.add_modifier(Modifier::BOLD)
68-
.add_modifier(Modifier::ITALIC),
69-
),
47+
OutputFmt::Line => Line::from(Span::from(format!(" {} ({})", conn.name, url))).style(
48+
Style::new()
49+
.bg(color)
50+
.fg(if color == Color::DarkGray {
51+
Color::DarkGray
52+
} else {
53+
Color::Black
54+
})
55+
.add_modifier(Modifier::BOLD)
56+
.add_modifier(Modifier::ITALIC),
57+
),
7058
};
7159

7260
list_items.push(conn_output);
@@ -89,12 +77,17 @@ fn render_tab_log(f: &mut Frame, app: &App) {
8977
let mut text = Vec::new();
9078

9179
for (i, status) in conn.log().iter().enumerate() {
92-
let (color, desc) = match status {
93-
Ok(code) if *code == 200 => (Color::Green, code.to_string()),
94-
Ok(code) => (Color::Yellow, code.to_string()),
95-
Err(()) => (Color::Red, "Something went wrong".to_string()),
80+
let desc = match status {
81+
Ok(code) if *code == 200 => code.to_string(),
82+
Ok(code) => code.to_string(),
83+
Err(()) => "Something went wrong".to_string(),
9684
};
9785

86+
let color = conn
87+
.log()
88+
.front()
89+
.map_or(Color::Gray, |f| status_to_color(*f, &conn.conn_type));
90+
9891
// Identify the latest status.
9992
let left = if i == 0 {
10093
Span::styled(" NOW ", Style::new().bg(color).fg(Color::Black).bold())
@@ -120,3 +113,17 @@ fn render_tab_log(f: &mut Frame, app: &App) {
120113

121114
f.render_widget(info, Rect::new(2, 1, f.area().width, f.area().height - 1));
122115
}
116+
117+
const fn status_to_color(status: Result<u16, ()>, conn_type: &ConnectionType) -> Color {
118+
let Ok(code) = status else {
119+
return Color::Red;
120+
};
121+
122+
match conn_type {
123+
ConnectionType::Web { .. } => match code {
124+
200 => Color::Green,
125+
_ => Color::Yellow,
126+
},
127+
ConnectionType::Local { .. } => Color::Green,
128+
}
129+
}

0 commit comments

Comments
 (0)