Skip to content

Commit

Permalink
Merge pull request #444 from Nukesor/comfy-table-update
Browse files Browse the repository at this point in the history
Comfy table update
  • Loading branch information
Nukesor authored Jun 6, 2023
2 parents 420d1ae + b4aed28 commit cc9c2af
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 44 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ on:
- "**/Cargo.toml"
- "**/Cargo.lock"
pull_request:
branches:
- main
- development
paths:
- ".github/**/*"
- "**.rs"
Expand All @@ -26,12 +29,9 @@ jobs:
matrix:
target:
- x86_64-unknown-linux-gnu
- x86_64-apple-darwin
include:
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
- os: macos-latest
target: x86_64-apple-darwin

steps:
- name: Checkout code
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ on:
- "**/Cargo.toml"
- "**/Cargo.lock"
pull_request:
branches:
- main
- development
paths:
- ".github/**/*"
- "**.rs"
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ on:
- "**/Cargo.toml"
- "**/Cargo.lock"
pull_request:
branches:
- main
- development
paths:
- ".github/**/*"
- "**.rs"
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pueue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ anyhow = "1.0"
chrono-english = "0.1"
clap = { version = "4.3", features = ["derive", "cargo", "help"] }
clap_complete = "4.3"
comfy-table = "6.2"
comfy-table = "7"
crossterm = { version = "0.26", default-features = false }
ctrlc = { version = "3", features = ["termination"] }
handlebars = "4.3"
Expand Down
2 changes: 1 addition & 1 deletion pueue/src/client/display/group.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use comfy_table::{Attribute, Color};
use crossterm::style::{Attribute, Color};

use pueue_lib::{
network::message::GroupResponseMessage,
Expand Down
2 changes: 1 addition & 1 deletion pueue/src/client/display/log/local.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fs::File;
use std::io::{self, Stdout};

use comfy_table::*;
use crossterm::style::{Attribute, Color};

use pueue_lib::log::{get_log_file_handle, seek_to_last_lines};
use pueue_lib::settings::Settings;
Expand Down
19 changes: 12 additions & 7 deletions pueue/src/client/display/log/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::BTreeMap;

use comfy_table::*;
use comfy_table::{Attribute as ComfyAttribute, Cell, CellAlignment, Table};
use crossterm::style::Color;

use pueue_lib::network::message::TaskLogMessage;
use pueue_lib::settings::Settings;
Expand Down Expand Up @@ -129,7 +130,11 @@ fn print_log(
/// Print some information about a task, which is displayed on top of the task's log output.
fn print_task_info(task: &Task, style: &OutputStyle) {
// Print task id and exit code.
let task_cell = style.styled_cell(format!("Task {}: ", task.id), None, Some(Attribute::Bold));
let task_cell = style.styled_cell(
format!("Task {}: ", task.id),
None,
Some(ComfyAttribute::Bold),
);

let (exit_status, color) = match &task.status {
TaskStatus::Paused => ("paused".into(), Color::White),
Expand Down Expand Up @@ -167,30 +172,30 @@ fn print_task_info(task: &Task, style: &OutputStyle) {

// Command and path
table.add_row(vec![
style.styled_cell("Command:", None, Some(Attribute::Bold)),
style.styled_cell("Command:", None, Some(ComfyAttribute::Bold)),
Cell::new(&task.command),
]);
table.add_row(vec![
style.styled_cell("Path:", None, Some(Attribute::Bold)),
style.styled_cell("Path:", None, Some(ComfyAttribute::Bold)),
Cell::new(task.path.to_string_lossy()),
]);
if let Some(label) = &task.label {
table.add_row(vec![
style.styled_cell("Label:", None, Some(Attribute::Bold)),
style.styled_cell("Label:", None, Some(ComfyAttribute::Bold)),
Cell::new(label),
]);
}

// Start and end time
if let Some(start) = task.start {
table.add_row(vec![
style.styled_cell("Start:", None, Some(Attribute::Bold)),
style.styled_cell("Start:", None, Some(ComfyAttribute::Bold)),
Cell::new(start.to_rfc2822()),
]);
}
if let Some(end) = task.end {
table.add_row(vec![
style.styled_cell("End:", None, Some(Attribute::Bold)),
style.styled_cell("End:", None, Some(ComfyAttribute::Bold)),
Cell::new(end.to_rfc2822()),
]);
}
Expand Down
2 changes: 1 addition & 1 deletion pueue/src/client/display/log/remote.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io;

use anyhow::Result;
use comfy_table::*;
use crossterm::style::{Attribute, Color};
use snap::read::FrameDecoder;

use pueue_lib::network::message::TaskLogMessage;
Expand Down
63 changes: 36 additions & 27 deletions pueue/src/client/display/style.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,58 @@
use pueue_lib::settings::Settings;

use comfy_table::Cell;
use comfy_table::{Attribute as ComfyAttribute, Cell, Color as ComfyColor};
use crossterm::style::{style, Attribute, Color, Stylize};

/// OutputStyle wrapper for actual colors depending on settings
/// - Enables styles if color mode is 'always', or if color mode is 'auto' and output is a tty.
/// - Using dark colors if dark_mode is enabled
#[derive(Debug, Clone)]
pub struct OutputStyle {
/// whether or not ANSI styling is enabled
/// Whether or not ANSI styling is enabled
pub enabled: bool,
/// red color
red: Color,
/// green color
green: Color,
/// yellow color
yellow: Color,
/// Whether dark mode is enabled.
pub dark_mode: bool,
}

impl OutputStyle {
/// init color-scheme depending on settings
pub const fn new(settings: &Settings, enabled: bool) -> Self {
if settings.client.dark_mode {
Self {
enabled,
green: Color::DarkGreen,
red: Color::DarkRed,
yellow: Color::DarkYellow,
Self {
enabled,
dark_mode: settings.client.dark_mode,
}
}

/// Return the desired crossterm color depending on whether we're in dark mode or not.
fn map_color(&self, color: Color) -> Color {
if self.dark_mode {
match color {
Color::Green => Color::DarkGreen,
Color::Red => Color::DarkRed,
Color::Yellow => Color::DarkYellow,
_ => color,
}
} else {
Self {
enabled,
green: Color::Green,
red: Color::Red,
yellow: Color::Yellow,
}
color
}
}

fn map_color(&self, color: Color) -> Color {
/// Return the desired comfy_table color depending on whether we're in dark mode or not.
fn map_comfy_color(&self, color: Color) -> ComfyColor {
if self.dark_mode {
return match color {
Color::Green => ComfyColor::DarkGreen,
Color::Red => ComfyColor::DarkRed,
Color::Yellow => ComfyColor::DarkYellow,
_ => ComfyColor::White,
};
}

match color {
Color::Green => self.green,
Color::Red => self.red,
Color::Yellow => self.yellow,
_ => color,
Color::Green => ComfyColor::Green,
Color::Red => ComfyColor::Red,
Color::Yellow => ComfyColor::Yellow,
_ => ComfyColor::White,
}
}

Expand Down Expand Up @@ -80,7 +89,7 @@ impl OutputStyle {
&self,
text: T,
color: Option<Color>,
attribute: Option<Attribute>,
attribute: Option<ComfyAttribute>,
) -> Cell {
let mut cell = Cell::new(text.to_string());
// Styling disabled
Expand All @@ -89,7 +98,7 @@ impl OutputStyle {
}

if let Some(color) = color {
cell = cell.fg(self.map_color(color));
cell = cell.fg(self.map_comfy_color(color));
}
if let Some(attribute) = attribute {
cell = cell.add_attribute(attribute);
Expand Down
3 changes: 2 additions & 1 deletion pueue/src/client/display/table_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use chrono::Duration;
use comfy_table::presets::UTF8_HORIZONTAL_ONLY;
use comfy_table::*;
use comfy_table::{Cell, ContentArrangement, Row, Table};
use crossterm::style::Color;

use pueue_lib::settings::Settings;
use pueue_lib::task::{Task, TaskResult, TaskStatus};
Expand Down

0 comments on commit cc9c2af

Please sign in to comment.