mfgames-cli-table

News

Introduction

Generic list/table output formatting for clap-based CLI commands, built on tabled.

Usage

Add this package to the appropriate Cargo.toml:

mfgames-cli-table = { git = "https://src.mfgames.com/mfgames-rs/mfgames-cli-table.git" }

Command Arguments

This library contains a common set of options for clap that have no short form to avoid collisions:

  • --markdown formats the table in Markdown
  • --json formats the table in JSON
  • --format FORMAT formats the table in the given format:
  • none doesn't output the data at all
  • plain a plain format with minimal borders
  • markdown formats it as Markdown
  • json formats it as a single line JSON (format with jq if you need it pretty)

The three top-level arguments are mutually exclusive and all output always goes to stdout.

Printing

To format the data, take a Vec of row data that derived Tabled (for the table formatting) and Serialize (for JSON) and pass it into print_table:

#[derive(Tabled, Serialize)]
struct Row
{
    name: String,
}

fn run(command: &ListCommand)
{
    let rows = vec![Row { name: "example".into() }];
    print_table(rows, &command.table_options);
}

There is also a render_table which does the same but instead of printing it, it returns it as a string.

Metadata

Project

Forges

Categories