Code Monkey home page Code Monkey logo

formatador's Introduction

formatador

STDOUT text formatting

Build Status

Quick and dirty

You can call class methods to print out single lines like this:

Formatador.display_line('Hello World')

You use tags, similar to html, to set formatting options:

Formatador.display_line('[green]Hello World[/]')

[/] resets everything to normal, colors are supported and [_color_] sets the background color.

Standard options

  • format - and adds color codes if STDOUT.tty? is true
  • display - calls format on the input and prints it
  • display_line - calls display, but adds on a newline (\n)
  • redisplay - Displays text, prepended with \r which will overwrite the last existing line

Extensions

  • display_table: takes an array of hashes. Each hash is a row, with the keys being the headers and values being the data. An optional second argument can specify which headers/columns to include and in what order they should appear.
  • display_compact_table: Same as display_table, execpt that split lines are not drawn by default in the body of the table. If you need a split line, put a :split constant in the body array.
  • redisplay_progressbar: takes the current and total values as its first two arguments and redisplays a progressbar (until current = total and then it display_lines). An optional third argument represents the start time and will add an elapsed time counter.

Progress Bar examples

total    = 1000
progress = Formatador::ProgressBar.new(total)

1000.times do
  progress.increment
end

#=> 978/1000  |************************************************* |

# Change the color of the bar
total    = 1000
progress = Formatador::ProgressBar.new(total, :color => "light_blue")

1000.times do
  progress.increment
end

# Change the color of a completed progress bar
total    = 1000
progress = Formatador::ProgressBar.new(total) { |b| b.opts[:color] = "green" }

1000.times do
  progress.increment
end

Table examples

table_data = [
  { :name => "Joe",  :food => "Burger" },
  { :name => "Bill", :food => "French fries" }
]
Formatador.display_table(table_data)

#=> +------+--------------+
#   | name | food         |
#   +------+--------------+
#   | Joe  | Burger       |
#   +------+--------------+
#   | Bill | French fries |
#   +------+--------------+

table_data = [
  {
    :name => "Joe",
    :meal => {
      :main_dish => "Burger",
      :drink => "water"
    }
  },
  {
    :name => "Bill",
    :meal => {
      :main_dish => "Chicken",
      :drink => "soda"
    }
  }
]
Formatador.display_table(table_data, [:name, :"meal.drink"])

#=> +------+------------+
#   | name | meal.drink |
#   +------+------------+
#   | Joe  | water      |
#   +------+------------+
#   | Bill | soda       |
#   +------+------------+

Indentation

By initializing a formatador object you can keep track of indentation:

formatador = Formatador.new
formatador.display_line('one level of indentation')
formatador.indent {
  formatador.display_line('two levels of indentation')
}
formatador.display_line('one level of indentation')

Copyright

(The MIT License)

Copyright (c) 2022 geemus (Wesley Beary)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

formatador's People

Contributors

amatsuda avatar beanieboi avatar bkabrda avatar bmorrall avatar cdlm avatar dependabot[bot] avatar esse avatar gabetax avatar geemus avatar howech avatar lgn21st avatar lodestone avatar manther avatar michaelmior avatar mikoto2000 avatar nirvdrum avatar olleolleolle avatar sawanoboly avatar swamp09 avatar takanamito avatar temujin9 avatar ytkg 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

formatador's Issues

Allow display of a row index

I'm constructing a DataFrame gem for ruby called daru, and it involves row and column indexed data structures.

Currently I've rolled my own version of #inspect to display a DataFrame in the console (somewhat) pretty, but if support for single level and hierarchical row indexing could be added to Formatador in the future then that would be fantastic!

Allow to send color codes even if no tty

Sometimes we need to send the color codes to log files, or stdout is replaced by a logging process like daemontools. Formatador needs an option to force the color codes.

Thanks.

Better visualization on README

The idea is to use the syntax specification that Markdown allows for a better visualization of the code (and even results).

Here an example of how it would look like:

total    = 1000
progress = ProgressBar.new(total)
1000.times do
 progress.increment
end

#=> 978/1000  |************************************************* |

# Change the color of the bar
total    = 1000
progress = ProgressBar.new(total, :color => "light_blue")
1000.times do
 progress.increment
end

# Change the color of a completed progress bar
total    = 1000
progress = ProgressBar.new(total) { |b| b.opts[:color] = "green" }
1000.times do
 progress.increment
end

Tests fail when redirected to a file

Hi,
I just found out, that when you redirect the test output to a file, the 3 table tests fail, because you don't do the formatting. For example:

display_table([{:a => 1}, {:a => 2}]) - returns " +---+\n | \e[1ma\e[0m |\n +---+\n | 1 |\n +---+\n | 2 |\n +---+\n"

  expected => "    +---+\n    | \e[1ma\e[0m |\n    +---+\n    | 1 |\n    +---+\n    | 2 |\n    +---+\n"
  returned => "    +---+\n    | a |\n    +---+\n    | 1 |\n    +---+\n    | 2 |\n    +---+\n"

I believe that this is not an issue with formatador itself, but with the tests. I think they should react to the situation when redirected to a file.

Thanks,
Bohuslav Kabrda.

Allow renaming of headers in display_table

Would you consider a patch would allow a hash to be passed as the second argument to display_table so an alternate name could be given for columns? Currently I don't think this is possible without renaming all the keys in each hash.

Here's an example:

  table_data = [
    {:name => "Joe", :meal => {:main_dish => "Burger", :drink => "water"}}, 
    {:name => "Bill", :meal => {:main_dish => "Chicken", :drink => "soda"}}
  ]
  Formatador.display_table(table_data, {"name": "Name", "meal.drink": "Drink"})

  +------+------------+
  | Name | Drink      |
  +------+------------+
  | Joe  | water      |
  +------+------------+
  | Bill | soda       |
  +------+------------+

Empty lines in a simple table

Hi,

I don't know if it is a issue, but I can't fix a problem I got with my own code, please forgive me if it isn't an issue :s

formatador

The problem is fairly simple, it is a simple table, only containing simple hashes, as it doesn't got any linking, it is displayed on several lines. But I couldn't find any formatador option to fix it.

Formatador.display_compact_table(@clients_array)

Thanks you for your work and for having read.

Add License information to gemfile

This will make it show up on rubygems.org. I'm doing due diligence on our gems and need to find out the licenses for all the gems. Having it show up on rubygems.org cuts out the step of having to go to the github repo.

Is there a way to right-justify a numeric

I want to right-justify a numeric.

example:

table_data = [
  { :name => "Burger", :price => 200 },
  { :name => "French fries", :price => 120 }
]
Formatador.display_table(table_data)

#=>  +--------------+-------+
#    | name         | price |
#    +--------------+-------+
#    | Burger       |   200 |
#    +--------------+-------+
#    | French fries |   120 |
#    +--------------+-------+

Semantic Versioning

Are there plans on using Semantic Versioning for formatador? http://semver.org/

I was updating my version of the fog gem and having a 1.0 release of formatador would have helped make me more confident that upgrading wouldn't introduce any breaking changes

Decouple From Terminal + Add HTML / Spreadsheets Output?

Hi! Nice gem.

I'm on a project that us going to use some Presenters to render tabularized info to HTML and spreadsheets... a pretty common task.

But, instead of writing it as yet another small clump of template presentation code, we'd like to do it as a gem. Yours came up as an example of a tool that operates on a similar input /output mapping, though (currently) it is only for ANSI/stdout.

What would you think of a refactoring that separated it so yoy could conveniently swap rendering targets, and maybe threw in some other spiffups, like totals?

We could do most of the code+tests, but it would be nice for that work to have a proper home.

Thanks!

make a new release?

Hi,
I noticed that the last release on rubygems is quite old and has no license specified. This is already fixed in the master branch. Could you publish a new release please?

Rewrite headers to desired output

Hi,
I am having a small issue seeing how I can add a custom header of my own onto this.

The reason being is that I hit a third party API and instead of it return a JSON object with a key of "nickname", I would like it to be "username" instead. I could iterate over the response and create a new object from the response but I would like to avoid extra handling where possible.

Any help would be appreciated!

uninitialized constant StringIO while running tests

Hi,
I am not sure if I am running tests correctly, but I keep getting this error:

Formatador
#display_table([{:a => 1}, {:a => 2}]) - returns " +---+\n | \e[1ma\e[0m |\n +---+\n | 1 |\n +---+\n | 2 |\n +---+\n"
uninitialized constant StringIO (NameError)
./tests/tests_helper.rb:9:in `capture_stdout'
./tests/table_tests.rb:14
(...)

A simple fix is to add "requires 'stringio'" to tests_helper.rb. Am I missing something, or is this require needed for the tests to work properly?

Thank you very much,
Bohuslav Kabrda.

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.