GithubHelp home page GithubHelp logo

justjavac / term-table-rs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ryanbluth/term-table-rs

0.0 2.0 0.0 73 KB

Terminal table renderer for rust

Home Page: https://crates.io/crates/term-table

License: MIT License

Rust 100.00%

term-table-rs's Introduction

term-table

CLI Tables Made Easy

Example

let mut table = Table::new();
table.max_column_width = 40;

table.style = TableStyle::extended(); 

table.add_row(Row::new(vec![
    TableCell::new_with_alignment("This is some centered text", 2, Alignment::Center)
])); 

table.add_row(Row::new(vec![
    TableCell::new("This is left aligned text", 1),
    TableCell::new_with_alignment("This is right aligned text", 1, Alignment::Right)
]));

table.add_row(Row::new(vec![
    TableCell::new("This is left aligned text", 1),
    TableCell::new_with_alignment("This is right aligned text", 1, Alignment::Right)
]));

table.add_row(Row::new(vec![
    TableCell::new("This is some really really really really really really really really really that is going to wrap to the next line", 2),
]));   

println!("{}", table.render());

Here's the result

extended style

Using TableBuilder

let table = TableBuilder::new().style(TableStyle::extended()).rows(
        vec![
            Row::new(vec![
                TableCell::new_with_alignment("This is some centered text", 2, Alignment::Center)
            ]),
            Row::new(vec![
                TableCell::new("This is left aligned text"),
                TableCell::new_with_alignment("This is right aligned text", 1, Alignment::Right)
            ]),
            Row::new(vec![
                TableCell::new("This is left aligned text"),
                TableCell::new_with_alignment("This is right aligned text", 1, Alignment::Right)
            ]),
                Row::new(vec![
                TableCell::new_with_col_span("This is some really really really really really really really really really that is going to wrap to the next line", 2),
            ]),
        ]
    ).build();

println!("{}", table.render());

Table Styles

It is possible to define your own table styles by creating a new instance of TableStyle

This is what the extend table style implementation looks like. This is the default style in term-table-rs

pub fn extended() -> TableStyle {
    return TableStyle {
        top_left_corner: '╔',
        top_right_corner: '╗',
        bottom_left_corner: '╚',
        bottom_right_corner: '╝',
        outer_left_vertical: '╠',
        outer_right_vertical: '╣',
        outer_bottom_horizontal: '╩',
        outer_top_horizontal: '╦',
        intersection: '╬',
        vertical: '║',
        horizontal: '═',
    };
}

TableStyle also implements a simple() table style function and a blank() table style function

Those styles looks like this

Blank

blank style

Simple

simple style

Column Widths

It is possible to control the maximum width of table columns. The max_column_width property of Table can be set to restrict the width of all TableCells. The set_max_column_width function of Table can be used to set the max width of a specific column. The set_max_column_widths function provides the ability to set the width of multiple columns by passing in a Vec of tuples containing an index and width.

Disabling Row Separators

There are a few different options for disabling row separation.

Table has three flags for controlling row separation:

  1. separate_rows dictates whether rows are separated within the table

    separate_rows

  2. has_top_boarder dictates whether or not the table has a top border

    has_top_boarder

  3. has_bottom_boarder dictates whether or not the table has a bottom border

    has_bottom_boarder

Separators can also be controlled per row by setting the has_separator flag on Row

has_separator

term-table-rs's People

Contributors

fauxfaux avatar ivanceras avatar rivertam avatar ryanbluth avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.