Giter Club home page Giter Club logo

java_text_tables's Introduction

java_text_tables

Text Table Library in Java

About

The Java Text Tables library is the next generation of my original Java Text Table Formatter project.

This new project hopes to offer more extensibility and composability, while still providing the ease-of-use of the original project.

Examples

SimpleTable Example

SimpleTable is Optimized for quick table construction without having to know the final table dimensions beforehand. It Allows you to build a table one row/cell at a time.

// NOTE: Apply vertical alignment FIRST !
//
SimpleTable s = SimpleTable.of()
    .nextRow()
        .nextCell()
            .addLine("Left")
            .addLine("Top")
            .applyToCell(TOP_ALIGN .withHeight(height))
            .applyToCell(LEFT_ALIGN.withWidth (width ).withChar('^'))
        .nextCell()
            .addLine("Center")
            .addLine("Top")
            .applyToCell(TOP_ALIGN        .withHeight(height))
            .applyToCell(HORIZONTAL_CENTER.withWidth (width ))
        .nextCell()
            .addLine("Right")
            .addLine("Top")
            .applyToCell(TOP_ALIGN  .withHeight(height))
            .applyToCell(RIGHT_ALIGN.withWidth (width ))
    .nextRow()
        .nextCell()
            .addLine("Left")
            .addLine("Center")
            .applyToCell(VERTICAL_CENTER.withHeight(height))
            .applyToCell(LEFT_ALIGN     .withWidth (width ))
        .nextCell()
            .addLine("Center")
            .addLine("Center")
            .applyToCell(VERTICAL_CENTER  .withHeight(height))
            .applyToCell(HORIZONTAL_CENTER.withWidth (width ).withChar('.'))
        .nextCell()
            .addLine("Right")
            .addLine("Center")
            .applyToCell(VERTICAL_CENTER.withHeight(height))
            .applyToCell(RIGHT_ALIGN    .withWidth (width ))
    .nextRow()
        .nextCell()
            .addLine("Left")
            .addLine("Bottom")
            .applyToCell(BOTTOM_ALIGN.withHeight(height))
            .applyToCell(LEFT_ALIGN  .withWidth (width ))
        .nextCell()
            .addLine("Center")
            .addLine("Bottom")
            .applyToCell(BOTTOM_ALIGN     .withHeight(height))
            .applyToCell(HORIZONTAL_CENTER.withWidth (width ))
        .nextCell()
            .addLine("Right")
            .addLine("Bottom")
            .applyToCell(BOTTOM_ALIGN.withHeight(height))
            .applyToCell(RIGHT_ALIGN .withWidth (width ).withChar('_'))
    ;

//
// NOTE: SimpleTable makes creating the table easy, but you will need to convert it
// into a GridTable in order to perform further operations
// (like adding a border or printing)
//

// Convert to grid
//
GridTable g = s.toGrid();

// Add simple border
//
g = Border.of(Border.Chars.of('+', '-', '|')).apply(g);


// Print the table to System.out
//
Util.print(g);

The above code should result in the following output:

+----------+----------+----------+
|Left^^^^^^|  Center  |     Right|
|Top^^^^^^^|   Top    |       Top|
|^^^^^^^^^^|          |          |
|^^^^^^^^^^|          |          |
|^^^^^^^^^^|          |          |
|^^^^^^^^^^|          |          |
+----------+----------+----------+
|          |..........|          |
|          |..........|          |
|Left      |..Center..|     Right|
|Center    |..Center..|    Center|
|          |..........|          |
|          |..........|          |
+----------+----------+----------+
|          |          |__________|
|          |          |__________|
|          |          |__________|
|          |          |__________|
|Left      |  Center  |_____Right|
|Bottom    |  Bottom  |____Bottom|
+----------+----------+----------+

GridTable Example

GridTable Offers a more powerful table builder (compared to SimpleTable), but requires knowledge of the final table dimensions at construction, along with specifying coordinates (row, col) when manipulating cells.

GridTable g = GridTable.of(3, 3)
    .put(0, 0, Cell.of("Left"  , "Top"   ))
    .put(0, 1, Cell.of("Center", "Top"   ))
    .put(0, 2, Cell.of("Right" , "Top"   ))

    .put(1, 0, Cell.of("Left"  , "Center"))
    .put(1, 1, Cell.of("Center", "Center"))
    .put(1, 2, Cell.of("Right" , "Center"))

    .put(2, 0, Cell.of("Left"  , "Bottom"))
    .put(2, 1, Cell.of("Center", "Bottom"))
    .put(2, 2, Cell.of("Right" , "Bottom"))

    .applyToRow(0, TOP_ALIGN      .withHeight(height))
    .applyToRow(1, VERTICAL_CENTER.withHeight(height))
    .applyToRow(2, BOTTOM_ALIGN   .withHeight(height))

    .apply(0, 0, LEFT_ALIGN.withWidth(width).withChar('^'))
    .apply(1, 0, LEFT_ALIGN)
    .apply(2, 0, LEFT_ALIGN)

    .apply(0, 1, HORIZONTAL_CENTER.withWidth(width))
    .apply(1, 1, HORIZONTAL_CENTER.withChar('.'))
    .apply(2, 1, HORIZONTAL_CENTER)

    .apply(0, 2, RIGHT_ALIGN.withWidth(width))
    .apply(1, 2, RIGHT_ALIGN)
    .apply(2, 2, RIGHT_ALIGN.withChar('_'))
    ;

// Add fancy border
//
g = Border.DOUBLE_LINE.apply(g);

// Print the table to System.out
//
Util.print(g);

The above code should result in the following output:

╔══════════╦══════════╦══════════╗
║Left^^^^^^║  Center  ║     Right║
║Top^^^^^^^║   Top    ║       Top║
║^^^^^^^^^^║          ║          ║
║^^^^^^^^^^║          ║          ║
║^^^^^^^^^^║          ║          ║
║^^^^^^^^^^║          ║          ║
╠══════════╬══════════╬══════════╣
║          ║..........║          ║
║          ║..........║          ║
║Left      ║..Center..║     Right║
║Center    ║..Center..║    Center║
║          ║..........║          ║
║          ║..........║          ║
╠══════════╬══════════╬══════════╣
║          ║          ║__________║
║          ║          ║__________║
║          ║          ║__________║
║          ║          ║__________║
║Left      ║  Center  ║_____Right║
║Bottom    ║  Bottom  ║____Bottom║
╚══════════╩══════════╩══════════╝

NOTE Spaces between Border Character are due to HTML styling and would not appear in a terminal

Importing Into Your Project

Snapshot

<repositories>
  <repository>
    <id>oss-sonatype</id>
    <name>oss-sonatype</name>
    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
    <snapshots>
      <enabled>true</enabled>
    </snapshots>
  </repository>
</repositories>

<dependencies>
  <dependency>
    <groupId>com.github.inamik.text.tables</groupId>
    <artifactId>inamik-text-tables</artifactId>
    <version>1.0-SNAPSHOT</version>
  </dependency>
</dependencies>

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Credits

License

Licensed under The MIT License (MIT), see LICENSE.txt

java_text_tables's People

Contributors

davidpfarrell avatar dfarrell-lendup avatar gastaldi avatar jlleitschuh avatar dependabot[bot] 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.