Git Product home page Git Product logo

picnic's Introduction

Picnic Tables

A Kotlin DSL and Java/Kotlin builder API for constructing HTML-like tables which can be rendered to text.

Features:

  • Borders (with multiple styles)
  • Padding (left/right/top/bottom)
  • Per-table, row, and cell styling
  • Header/footer sections
  • Row/column spans
  • Text alignment

DSL:

table {
  row("Hello", "World")
  row("Hola", "Mundo")
}
HelloWorld
Hola Mundo

Very underwhelming! Picnic tables start completely unstyled by design.

Jump to a real-world example or continue reading for individual features.

(Note: these examples do not render correctly on mobile)

Border

table {
  cellStyle {
    // These options affect the style of all cells contained within the table.
    border = true
  }
  row("Hello", "World")
  row("Hola", "Mundo")
}
┌─────┬─────┐
│Hello│World│
├─────┼─────┤
│Hola │Mundo│
└─────┴─────┘

Table Border Style

table {
  style {
    // Unlike cellStyle, these options affect the style of the table itself.
    borderStyle = Hidden
  }
  cellStyle {
    border = true
  }
  row("Hello", "World")
  row("Hola", "Mundo")
}
Hello│World
─────┼─────
Hola │Mundo

Padding

table {
  cellStyle {
    border = true
    paddingLeft = 1
    paddingRight = 1
  }
  row("Hello", "World")
  row("Hola", "Mundo")
}
┌───────┬───────┐
│ Hello │ World │
├───────┼───────┤
│ Hola  │ Mundo │
└───────┴───────┘

Table, Row, and Cell Style

table {
  cellStyle {
    border = true
    paddingLeft = 1
    paddingRight = 1
  }
  row {
    cellStyle {
      // These options affect only the cells contained within this row and override table options.
      paddingTop = 1
    }
    cell("Hello")
    cell("World")
  }
  row {
    cell("Hola")
    cell("Mundo") {
       // These options affect only this specific cell and override row and table options.
       border = false
     }
  }
}
┌───────┬───────┐
│       │       │
│ Hello │ World │
├───────┼───────┘
│ Hola  │ Mundo  
└───────┘        

Header and Footer

table {
  header {
    // Rows in a header always come first no matter when they're added.
    row("Hello", "Header")
  }
  footer {
    // Rows in a footer always come last no matter when they're added.
    row("Hello", "Footer")
  }
  row("Hello", "World")
  cellStyle {
    border = true
  }
}
┌─────┬──────┐
│Hello│Header│
├─────┼──────┤
│Hello│World │
├─────┼──────┤
│Hello│Footer│
└─────┴──────┘

Row and Column Span

table {
  cellStyle {
    border = true
  }
  row {
    cell("Hello") {
      rowSpan = 2
    }
    cell("World")
  }
  // This row has only one cell because "Hello" will carry over and push it to the right.
  row("Mars")

  // This row has only one cell because it spans two columns.
  row {
    cell("Hola Mundo") {
      columnSpan = 2
    }
  }
}
┌─────┬─────┐
│Hello│World│
│     ├─────┤
│     │Mars │
├─────┴─────┤
│Hola Mundo │
└───────────┘

Text Alignment

table {
  cellStyle {
    border = true
    alignment = TopCenter
  }
  row {
    cell("Hello") {
      rowSpan = 4
      alignment = MiddleLeft
    }
    cell("Mercury")
  }
  row("Venus")
  row("Earth")
  row("Mars")
  row {
    cell("Hola") {
      rowSpan = 4
      alignment = MiddleLeft
    }
    cell("Jupiter")
  }
  row("Saturn")
  row("Uranus")
  row("Neptune")
  row("Adios", "Pluto")
}
┌─────┬───────┐
│     │Mercury│
│     ├───────┤
│     │ Venus │
│Hello├───────┤
│     │ Earth │
│     ├───────┤
│     │ Mars  │
├─────┼───────┤
│     │Jupiter│
│     ├───────┤
│     │Saturn │
│Hola ├───────┤
│     │Uranus │
│     ├───────┤
│     │Neptune│
├─────┼───────┤
│Adios│ Pluto │
└─────┴───────┘

Real-world Example

Here is a more advanced, real-world example from the Diffuse tool. It features row and column spans, headers and footers, borders, table border style, padding, and text alignment.

table {
  style {
    borderStyle = Hidden
  }
  cellStyle {
    alignment = MiddleRight
    paddingLeft = 1
    paddingRight = 1
    borderLeft = true
    borderRight = true
  }
  header {
    cellStyle {
      border = true
      alignment = BottomLeft
    }
    row {
      cell("APK") {
        rowSpan = 2
      }
      cell("compressed") {
        alignment = BottomCenter
        columnSpan = 3
      }
      cell("uncompressed") {
        alignment = BottomCenter
        columnSpan = 3
      }
    }
    row("old", "new", "diff", "old", "new", "diff")
  }
  body {
    row("dex", "664.8 KiB", "664.8 Kib", "-25 B", "1.5 MiB", "1.5 MiB", "-112 B")
    // "arsc", "manifest", etc…
  }
  footer {
    cellStyle {
      border = true
    }
    row("total", "1.3 MiB", "1.3 MiB", "-39 B", "2.2 MiB", "2.2 MiB", "-112 B")
  }
}
          │          compressed           │          uncompressed
          ├───────────┬───────────┬───────┼───────────┬───────────┬────────
 APK      │ old       │ new       │ diff  │ old       │ new       │ diff
──────────┼───────────┼───────────┼───────┼───────────┼───────────┼────────
      dex │ 664.8 KiB │ 664.8 KiB │ -25 B │   1.5 MiB │   1.5 MiB │ -112 B
     arsc │ 201.7 KiB │ 201.7 KiB │   0 B │ 201.6 KiB │ 201.6 KiB │    0 B
 manifest │   1.4 KiB │   1.4 KiB │   0 B │   4.2 KiB │   4.2 KiB │    0 B
      res │ 418.2 KiB │ 418.2 KiB │ -14 B │ 488.3 KiB │ 488.3 KiB │    0 B
    asset │       0 B │       0 B │   0 B │       0 B │       0 B │    0 B
    other │  37.1 KiB │  37.1 KiB │   0 B │  36.3 KiB │  36.3 KiB │    0 B
──────────┼───────────┼───────────┼───────┼───────────┼───────────┼────────
    total │   1.3 MiB │   1.3 MiB │ -39 B │   2.2 MiB │   2.2 MiB │ -112 B

Download

repositories {
  mavenCentral()
}
dependencies {
  implementation 'com.jakewharton.picnic:picnic:0.7.0'
}
Snapshots of the development version are available in Sonatype's snapshots repository.

repositories {
  maven {
    url 'https://oss.sonatype.org/content/repositories/snapshots/'
  }
}
dependencies {
  implementation 'com.jakewharton.picnic:picnic:0.8.0-SNAPSHOT'
}

License

Copyright 2015 Jake Wharton

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

picnic's People

Contributors

jakewharton avatar renovate[bot] avatar sonofbowser avatar vanniktech avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

picnic's Issues

support horizontal rule

In the tables I'm generating, I have several sections. It would useful to be able to add a separator between these sections.

Legacy mode

When using picnic 0.6.0 I'm getting:

The dependency com.jakewharton.picnic:picnic:0.6.0 was published in the legacy mode. Support for such dependencies will be removed in the future. See https://kotl.in/0b5kn8 for details.

Seems like tooling needs to be udpated, a new release needs to be cut and then we're all good.

Feature request: CSV (or DSV) rendering

Thank's for the great library. I use it in some of my projects and very happy with results (especially combined with Mosaic).

In one of my projects I've faced with requirement to output data as CSV along with human-readable tables. I've came up with my
own solution (not a rocket science) and got thinking that there could be some CSV related API in Picnic.

  • CSV (or more general DSV) format has no different in meaning from ASCII tables
  • Such new rendering API could benefit from existing Mosaic API (rows, columns, cell, columnSpan)
  • On the other side new API could contradict the single responsibility principle of the library (only ASCII tables)

So my questions are:

  • what do you think about supporting CSV?
  • if it works will you add that API in some future?
  • may I help with this?

Thank you

Columns are messed up with ANSI color codes

Expected behavior

Tables to be correctly drawn with ANSI color codes

Current behavior

Tables are messed up with Chinese ANSI color codes


Version: 0.4.0
OS: MacOS BigSur - english language
IDE: IntelliJ Idea Ultimate 2020.2
Kotlin: 1.4.0
Project type: Multiplatform, JVM variant only for this module ( stdlib-jdk8 )
JDK: 14

image
image

TEXT EXAMPLE

"\u001B[46mWelcome to My App!\u001B[0m"

TEXT RESULT

┌───────────────────────────────────────────────────────────────────┐
│                                                                   │
│                    Welcome to My App!                    │
│                                                                   │
├──────────────────────────────────┬────────────────────────────────┤
│                                  │            commands            │
├──────────────────────────────────┼───────┬────────────────┬───────┤
│   Search a Movie by title        │   1   │   search       │   s   │
├──────────────────────────────────┼───────┼────────────────┼───────┤
│   Rate a Movie by id             │   2   │   rate         │   r   │
├──────────────────────────────────┼───────┼────────────────┼───────┤
│   Get suggested Movies for you   │   3   │   suggestion   │   g   │
└──────────────────────────────────┴───────┴────────────────┴───────┘

Feature request: render an empty footer

Imagine this table

        table {
          style {
            borderStyle = Solid
          }
          cellStyle {
            borderLeft = true
            borderRight = true
          }
          header {
            cellStyle {
              border = true
              alignment = BottomLeft
            }
            row("Header 1", "Header 2")
          }
          body {
            repeat(3) {
              row("row $it", "row $it")
            }
          }
        }

It would render:

┌────────┬────────┐
│Header 1│Header 2│
├────────┼────────┤
│row 0   │row 0   │
│row 1   │row 1   │
│row 2   │row 2   │

Notice that the border at the bottom is missing. The fix is could be to render a different row in the body

            row {
              cell("last row") {
                borderBottom = true
              }
              cell("last row") {
                borderBottom = true
              }
            }

Or another fix could be to render a footer:

          footer {
            cellStyle {
              borderBottom = true
            }
            row("Footer 1", "Footer 2")
          }

But this is annoying, because the last row is a special case. I'd rather would like to specify a footer like this:

          footer {
            cellStyle {
              border = true
            }
          }

But this doesn't render anything since there is no row.

Notice year wrong?

Might be nitpicking here, but is the copyright year in README.md really 2015?

calling `renderText` from Java

Hi, thanks for this useful library! I'm using it in a Java project and I wanted to try out the ROUNDED and ASCII styles, but I cannot seem to access Table.renderText (or Table.render). Is this feature meant to be available from Java?

Support for table with external borders but no inner borders for top/bottom

Picnic could be able to do build

┌───────────┬──────────────┐
│ columnA   │ columnB      │
├───────────┼──────────────┤
│ itemA1    │ B            │
│ itemA2    │ C            │
│ itemA3    │ D            │
└───────────┴──────────────┘

But right now I can only build

┌───────────┬──────────────┐
│ columnA   │ columnB      │
├───────────┼──────────────┤
│ itemA1    │ B            │
│ itemA2    │ C            │
│ itemA3    │ D            │

Using header (border=true) + body(borderLeft=true, borderRight=true)

Characters don't work on Windows

It appears that the unicode characters are not generated or recognized correctly on Windows.

I've opened a PR to add a GitHub action to build on Windows, Linux and Mac; the Windows build always fails, though, because the tables look like this:

    ?          compressed           ?          uncompressed          
        ?????????????????????????????????????????????????????????????????
APK      ? old       ? new       ? diff  ? old       ? new       ? diff   
???????????????????????????????????????????????????????????????????????????
    dex ? 664.8 KiB ? 664.8 KiB ? -25 B ?   1.5 MiB ?   1.5 MiB ? -112 B 
    arsc ? 201.7 KiB ? 201.7 KiB ?   0 B ? 201.6 KiB ? 201.6 KiB ?    0 B 
manifest ?   1.4 KiB ?   1.4 KiB ?   0 B ?   4.2 KiB ?   4.2 KiB ?    0 B 
    res ? 418.2 KiB ? 418.2 KiB ? -14 B ? 488.3 KiB ? 488.3 KiB ?    0 B 
    asset ?       0 B ?       0 B ?   0 B ?       0 B ?       0 B ?    0 B 
    other ?  37.1 KiB ?  37.1 KiB ?   0 B ?  36.3 KiB ?  36.3 KiB ?    0 B 
???????????????????????????????????????????????????????????????????????????

See build results here.

Markdown links break the table

I have a picnic table where I am trying to render links using markdown (through mordant 2.0 markdown support). The cell breaks the table though because the width is calculated with the entire string and not just the visible part. Is there any way to work this around?

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/build.yaml
  • actions/checkout v4
  • actions/setup-java v4
  • gradle/actions v3
  • ffurrer2/extract-release-notes v2
  • softprops/action-gh-release v2
gradle
gradle.properties
settings.gradle
build.gradle
  • org.jetbrains.kotlin:kotlin-gradle-plugin 1.9.10
  • org.jetbrains.dokka:dokka-gradle-plugin 1.9.20
  • com.diffplug.spotless:spotless-plugin-gradle 6.25.0
  • ru.vyarus:gradle-animalsniffer-plugin 1.7.1
  • com.vanniktech:gradle-maven-publish-plugin 0.27.0
  • org.codehaus.mojo.signature:java18 1.0
  • net.sf.androidscents.signature:android-api-level-21 5.0.1_r2
picnic/gradle.properties
picnic/build.gradle
  • com.jakewharton.crossword:crossword 0.3.0
sample/build.gradle
  • com.github.ajalt:mordant 1.2.1
gradle-wrapper
gradle/wrapper/gradle-wrapper.properties
  • gradle 8.7

  • Check this box to trigger a request for Renovate to run again on this repository

Passing a PrintStream to renderText

It would be great if we could pass a PrintStream to the renderText call so that large tables can be streamed out immediately and to reduce the memory footprint.

How to use picnic in Java

I wonder if there is a simple quick start example for the usage of picnic in Java? A minimal introduction to Java usage would be great.

0.3.0 introduced an undocumented dependency on java8 - String.

0.3.0 specifically commit: 2ef687d
introduced a dependency on java8 String

I was using the lib inside an android application (mainly running on android6 devices).
project is setup:

  • with Java source/target compatibility 8
  • kotlin jvm target 8

Project compile without any issue or warning but the app ultimately crash when invoking the codePoints method.

E java.lang.NoSuchMethodError: No virtual method codePoints()Ljava/util/stream/IntStream; in class Ljava/lang/String; or its super classes (declaration of 'java.lang.String' appears in /system/framework/core-libart.jar) E at com.jakewharton.picnic.SimpleLayout.draw(textLayout.kt:62) E at com.jakewharton.picnic.TextRendering.render(textRender.kt:314) E at com.jakewharton.picnic.TextRendering.render$default(textRender.kt:16)

Columns are messed up with unicode chars

Expected behavior

Tables to be correctly drawn with unicode characters

Current behavior

Tables are messed up with unicode characters


Version: 0.4.0
OS: MacOS BigSur - english language
IDE: IntelliJ Idea Ultimate 2020.2
Kotlin: 1.4.0
Project type: Multiplatform, JVM variant only for this module ( stdlib-jdk8 )
JDK: 14

Chinese chars

image

TEXT:

┌──────────────────────────────────────────────────────────────────────────────────┬─────────────┬───────────────────┐
│  呀〈KIBA〉 ~暗黒騎士鎧伝~                                                                │  Year 2011  │  Tmdb id: 257171  │
├──────────────────────────────────────────────────────────────────────────────────┴─────────────┴───────────────────┤
│  Actors: Masaki Kyômoto, Mika Hijii, Leah Dizon, Mikoto Inoue, Raima Hiramatsu, Kazuhiko Inoue, Miz...             │
├────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│  Genres: Fantasy, Action, Adventure                                                                                │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

ANSI color code

image
image

TEXT EXAMPLE

"\u001B[46mWelcome to My App!\u001B[0m"

TEXT RESULT

┌───────────────────────────────────────────────────────────────────┐
│                                                                   │
│                    Welcome to My App!                    │
│                                                                   │
├──────────────────────────────────┬────────────────────────────────┤
│                                  │            commands            │
├──────────────────────────────────┼───────┬────────────────┬───────┤
│   Search a Movie by title        │   1   │   search       │   s   │
├──────────────────────────────────┼───────┼────────────────┼───────┤
│   Rate a Movie by id             │   2   │   rate         │   r   │
├──────────────────────────────────┼───────┼────────────────┼───────┤
│   Get suggested Movies for you   │   3   │   suggestion   │   g   │
└──────────────────────────────────┴───────┴────────────────┴───────┘

Support hyperlinks

I'm using picnic to print out tables for a command line tool. On some tables, I'd like to have hyperlink support. Do you have any ideas / suggestion how to have them clickable by iTerm?

iTerms and other consoles have support for links:

printf '\e]8;;http://example.com\e\\This is a link\e]8;;\e\\\n'

Source

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.