Skip to content

Commit 5a3de00

Browse files
committed
refactor: consolidate color_status into util, remove duplicate in queries/results
1 parent ece6dea commit 5a3de00

3 files changed

Lines changed: 19 additions & 26 deletions

File tree

src/queries.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::api::ApiClient;
2-
use crossterm::style::{Color, Stylize};
2+
use crossterm::style::Stylize;
33
use serde::{Deserialize, Serialize};
44

55
const SQL_KEYWORDS: &[&str] = &[
@@ -141,16 +141,6 @@ struct ListResponse {
141141
next_cursor: Option<String>,
142142
}
143143

144-
fn color_status(status: &str) -> String {
145-
let color = match status {
146-
"succeeded" => Color::Green,
147-
"failed" => Color::Red,
148-
"running" | "queued" | "pending" => Color::Yellow,
149-
_ => Color::Reset,
150-
};
151-
status.with(color).to_string()
152-
}
153-
154144
fn truncate_sql(sql: &str, max: usize) -> String {
155145
let flat = sql.split_whitespace().collect::<Vec<_>>().join(" ");
156146
if flat.chars().count() <= max {
@@ -193,7 +183,7 @@ pub fn list(
193183
.map(|r| {
194184
vec![
195185
r.id.clone(),
196-
color_status(&r.status),
186+
crate::util::color_status(&r.status),
197187
crate::util::format_date(&r.created_at),
198188
r.execution_time_ms
199189
.map(|ms| ms.to_string())
@@ -241,7 +231,7 @@ fn print_detail(r: &QueryRun, format: &str) {
241231
"table" => {
242232
let label = |l: &str| format!("{:<14}", l).dark_grey().to_string();
243233
println!("{}{}", label("id:"), r.id);
244-
println!("{}{}", label("status:"), color_status(&r.status));
234+
println!("{}{}", label("status:"), crate::util::color_status(&r.status));
245235
println!(
246236
"{}{}",
247237
label("created:"),

src/results.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::api::ApiClient;
2-
use crossterm::style::{Color, Stylize};
2+
use crossterm::style::Stylize;
33
use serde::{Deserialize, Serialize};
44

55
#[derive(Deserialize, Serialize)]
@@ -22,17 +22,6 @@ struct ListResponse {
2222
has_more: bool,
2323
}
2424

25-
fn color_status(status: &str) -> String {
26-
let color = match status {
27-
"ready" => Color::Green,
28-
"failed" => Color::Red,
29-
"pending" | "processing" => Color::Yellow,
30-
"expired" => Color::DarkGrey,
31-
_ => Color::Reset,
32-
};
33-
status.with(color).to_string()
34-
}
35-
3625
pub fn list(workspace_id: &str, limit: Option<u32>, offset: Option<u32>, format: &str) {
3726
let api = ApiClient::new(Some(workspace_id));
3827

@@ -59,7 +48,7 @@ pub fn list(workspace_id: &str, limit: Option<u32>, offset: Option<u32>, format:
5948
.map(|r| {
6049
let mut row = vec![
6150
r.id.clone(),
62-
color_status(&r.status),
51+
crate::util::color_status(&r.status),
6352
crate::util::format_date(&r.created_at),
6453
];
6554
if has_rows {

src/util.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,20 @@ fn colorize_json_value(v: &str) -> String {
328328
format!("{colored}{comma}")
329329
}
330330

331+
/// Color a status string for terminal output. Covers vocabulary from both
332+
/// query runs (succeeded/failed/running/queued/pending) and results (ready/expired/processing).
333+
pub fn color_status(status: &str) -> String {
334+
use crossterm::style::{Color, Stylize};
335+
let color = match status {
336+
"succeeded" | "ready" => Color::Green,
337+
"failed" => Color::Red,
338+
"running" | "queued" | "pending" | "processing" => Color::Yellow,
339+
"expired" => Color::DarkGrey,
340+
_ => Color::Reset,
341+
};
342+
status.with(color).to_string()
343+
}
344+
331345
/// Format an ISO date string compactly: "2024-03-15 14:23" (no seconds, no timezone).
332346
pub fn format_date(s: &str) -> String {
333347
let s = s.split('.').next().unwrap_or(s).trim_end_matches('Z');

0 commit comments

Comments
 (0)