Skip to content

Commit

Permalink
Merge pull request #101 from jordilin/display
Browse files Browse the repository at this point in the history
Move all print ops to display module
  • Loading branch information
jordilin committed Mar 8, 2024
2 parents b1792ba + deb3337 commit b0579ce
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 109 deletions.
14 changes: 7 additions & 7 deletions src/cicd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::api_traits::Cicd;
use crate::cli::PipelineOptions;
use crate::config::Config;
use crate::remote::{ListRemoteCliArgs, PipelineBodyArgs};
use crate::{remote, Result};
use crate::{display, remote, Result};
use std::io::Write;
use std::sync::Arc;

Expand Down Expand Up @@ -52,12 +52,12 @@ fn list_pipelines<W: Write>(
writer.write_all(b"No pipelines found.\n")?;
return Ok(());
}
if !cli_args.no_headers {
writer.write_all(b"URL | Branch | SHA | Created at | Status\n")?;
}
for pipeline in pipelines {
writer.write_all(format!("{}\n", pipeline).as_bytes())?;
}
display::print(
&mut writer,
pipelines,
cli_args.no_headers,
&cli_args.format,
)?;
Ok(())
}

Expand Down
38 changes: 38 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
display::Format,
docker::{DockerImageCliArgs, DockerListCliArgs},
merge_request::{MergeRequestCliArgs, MergeRequestListCliArgs},
remote::{ListRemoteCliArgs, ListSortMode, MergeRequestState},
Expand Down Expand Up @@ -65,6 +66,9 @@ struct DockerImageMetadata {
/// Do not print headers
#[clap(long)]
pub no_headers: bool,
/// Output format. pipe " | " or csv ","
#[clap(long, default_value_t=FormatCli::Pipe)]
format: FormatCli,
}

#[derive(Parser)]
Expand Down Expand Up @@ -108,6 +112,9 @@ struct ProjectInfo {
/// ID of the project
#[clap(long)]
pub id: Option<i64>,
/// Output format. pipe " | " or csv ","
#[clap(long, default_value_t=FormatCli::Pipe)]
format: FormatCli,
}

#[derive(Parser)]
Expand Down Expand Up @@ -263,6 +270,24 @@ struct ListArgs {
created_before: Option<String>,
#[clap(long, default_value_t=SortModeCli::Asc)]
sort: SortModeCli,
/// Output format. pipe " | " or csv ","
#[clap(long, default_value_t=FormatCli::Pipe)]
format: FormatCli,
}

#[derive(ValueEnum, Clone, Debug)]
enum FormatCli {
Csv,
Pipe,
}

impl Display for FormatCli {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
FormatCli::Csv => write!(f, "csv"),
FormatCli::Pipe => write!(f, "pipe"),
}
}
}

#[derive(ValueEnum, Clone, Debug)]
Expand Down Expand Up @@ -334,6 +359,7 @@ pub enum ProjectOperation {
pub struct ProjectOptions {
pub operation: ProjectOperation,
pub refresh_cache: bool,
pub format: Format,
}

pub enum MergeRequestOptions {
Expand Down Expand Up @@ -370,6 +396,7 @@ impl From<DockerImageMetadata> for DockerOptions {
.tag(options.tag)
.refresh_cache(options.refresh)
.no_headers(options.no_headers)
.format(options.format.into())
.build()
.unwrap(),
)
Expand Down Expand Up @@ -402,6 +429,7 @@ fn gen_list_args(list_args: ListArgs) -> ListRemoteCliArgs {
.created_after(list_args.created_after)
.created_before(list_args.created_before)
.sort(list_args.sort.into())
.format(list_args.format.into())
.build()
.unwrap();
list_args
Expand All @@ -426,6 +454,15 @@ impl From<CreateMergeRequest> for MergeRequestOptions {
}
}

impl From<FormatCli> for Format {
fn from(format: FormatCli) -> Self {
match format {
FormatCli::Csv => Format::CSV,
FormatCli::Pipe => Format::PIPE,
}
}
}

impl From<SortModeCli> for ListSortMode {
fn from(sort: SortModeCli) -> Self {
match sort {
Expand Down Expand Up @@ -518,6 +555,7 @@ impl From<ProjectCommand> for ProjectOptions {
id: options_info.id,
},
refresh_cache: options.refresh,
format: options_info.format.into(),
},
}
}
Expand Down
85 changes: 85 additions & 0 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use crate::Result;
use std::{fmt::Display, io::Write};

#[derive(Clone, Debug, Default)]
pub enum Format {
CSV,
#[default]
PIPE,
}

impl Display for Format {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Format::CSV => write!(f, ","),
Format::PIPE => write!(f, " | "),
}
}
}

impl From<&Format> for &str {
fn from(f: &Format) -> Self {
match f {
Format::CSV => ",",
Format::PIPE => " | ",
}
}
}

pub struct DisplayBody {
pub columns: Vec<Column>,
}

impl DisplayBody {
pub fn new(columns: Vec<Column>) -> Self {
Self { columns }
}
}

pub struct Column {
pub name: String,
pub value: String,
}

impl Column {
pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
Self {
name: name.into(),
value: value.into(),
}
}
}

pub fn print<W: Write, D: Into<DisplayBody> + Clone>(
w: &mut W,
data: Vec<D>,
no_headers: bool,
format: &Format,
) -> Result<()> {
if data.is_empty() {
return Ok(());
}
if !no_headers {
// Get the headers from the first row of columns
let headers = data[0]
.clone()
.into()
.columns
.iter()
.map(|c| c.name.clone())
.collect::<Vec<_>>();
writeln!(w, "{}", headers.join(format.into()))?;
}
for d in data {
let d = d.into();
let num_columns = d.columns.len();
for i in 0..num_columns {
write!(w, "{}", d.columns[i].value)?;
if i < num_columns - 1 {
write!(w, "{}", format)?;
}
}
writeln!(w)?;
}
Ok(())
}
Loading

0 comments on commit b0579ce

Please sign in to comment.