Git Product home page Git Product logo

pprint's Introduction

pprint.png

A lua library completes data beautification output, and has no external dependencies.

Require

Install

You can install the library with luarock, like this:

$ luarocks install pprint

Or the file should dropped into a project or a lua lib, then require by it:

pprint = require('pprint')

Usage

Also, using the library is very simple. It provides several functions:

  • pprint.pprint(obj, indent?, width?, depth?)

    Print the formatted representation of object with color to stream with a trailing newline.

    indent: indent of each level

    width: max col limit

    depth: level limit of recursive

    local test_struct = {
        ['name'] = 'Tom',
        age = 18,
        hobbys = {
            'game',
            ['ball'] = {'football', 'basketball'}
        },
        eat = function(food)
            print('I eat: ' .. food)
        end
    }
    pprint.pprint(test_struct)

    demo

  • pprint.pp(obj, args?)

    Print the formatted representation of object to stream with a trailing newline.

    args: The options of function. Will be transparently transmitted to PrettyPrinter.

  • pprint.pformat(obj, indent?, width?, depth?)

    Return the formatted representation of object as a string.

    indent: Number of spaces to indent for each level of nesting.

    width: Attempted maximum number of columns in the output.

    depth: Depth limit, exceeding the limit will be folded.

  • pprint.isrecursive(obj)

    Determines whether object requires recursive representation.

    pprint.isrecursive(1) -- false
    pprint.isrecursive({1, 2, 3}) -- true
  • pprint.isreadable(obj)

    Determines whether the formatted representation of object is "readable" that can be used to reconstruct the object's value via load().

    pprint.isreadable(1) -- true
    pprint.isreadable({1, 2, 3}) -- true
    pprint.isreadable({
              'tom',
              28,
              say = function ()
                  print('hello')
              end
            }) -- false

Source

The pprint library define a class: pprint.PrettyPrinter(args)

-- Create a `PrettyPrinter` instance.
local pp = pprint.PrettyPrinter({...})

The args support some arguments:

  • args.indent: integer, Number of spaces to indent for each level of nesting.
  • args.width: integer, Attempted maximum number of columns in the output.
  • args.depth: integer, Depth limit, exceeding the limit will be folded.
  • args:compact: boolean, If true, several items will be combined in one line.
  • args:sort_tables: boolean, If true, sort the table by key.
  • args.scientific_notation: boolean, If true, will display number with scientific notation.
  • args.color: boolean, If true, will format with color escape.

The function provided:

  • PrettyPrinter:pprint(obj)
  • PrettyPrinter:pformat(obj)
  • PrettyPrinter:isrecursive(obj)
  • PrettyPrinter:isreadable(obj)

Feature

  • Pure lua implementation, no external lib dependencies.
  • Support color output.
  • Work with lua >=5.2

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.