Giter Site home page Giter Site logo

autoaligns.jl's Introduction

AutoAligns

Project Status: WIP โ€“ Initial development is in progress, but there has not yet been a stable, usable release suitable for the public. Build Status Coverage Status

This Julia package helps align text for printing with a monospace font, by keeping track of the maximum width of each column. It is useful for printing matrices, tables, and analogous structures, and in particular for writing methods for Base.print.

Installation

This package is not yet registered on Julia's METADATA.jl. To install it, you must use the usual procedure for unregistered packages:

Pkg.clone("git://github.com/tpapp/AutoAligns.jl.git")

Note that the minimum required version of Julia is 0.6.

Usage

Create an AutoAlign object, then use print to save values into it, which are strings or converted to strings. The AutoAlign object keeps track of the current column (which you can query with position, but that is rarely necessary), and you can also provide a default alignment, or specify it before printing, using the methods

print(aa::AutoAlign, xs...)
println(aa::AutoAlign, xs...)

where the latter starts a new line after saving the values. Both accept a keyword argument align, for specifying alternate alignent for the preceding arguments.

Alignment can be specified as follows:

  1. an atom left, right, or center, or any other user extension which is a subtype of Alignment,

  2. a vector of alignment specifiers: for positions outside the length of the vector, the last element is used,

  3. a Dict of position => alignment pairs, where the position :default provides the alignment for items not in the Dict.

Example

julia> using AutoAligns

julia> aa = AutoAlign(align = Dict(1 => left, :default => right));

julia> for (i, r) in zip([1, 100, 10000], ["a", "bb", "ccc"])
           print(aa, r)
           for j in 1:5
               print(aa, "  ", i + j) # padding
           end
           println(aa)
       end

julia> print(STDOUT, aa)
a        2      3      4      5      6
bb     101    102    103    104    105
ccc  10001  10002  10003  10004  10005

Notes

Spaces or separators such as | are not treated in a special way, and therefore should be accounted for when specifying positions for alignment.

Printing an AutoAlign object to a stream pads the strings with spaces so as to achieve the desired alignment.

An AutoAlign object is not a subtype of IO, as it does not implement a write(::AutoAlign, x::UInt8) method. Unlike IOBuffer, printing does not delete the contents.

Custom alignment types can be defined as subtypes of Alignment, by implementing align_string.

autoaligns.jl's People

Contributors

tpapp avatar waldyrious avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.