Question

The output of my Json file is hash. I'm iterating over each line and using the tp gem to format it. My question is how do I format the width so that all my data is displayed neatly in the terminal.

I assume that it isn't formatting neatly because of the array within the hash.

 File.open("output.json", 'a+') do |file|
      1       data = { time_format: time_format, purchase: @purchase, quantity: @quantity, retail_price: @retail_price, purchasing_price: @purchasing_price }
      0       file.puts data.to_json                                                                                                                                         
      1     end  
transaction = File.open('output.json', 'a+').each do |line| 
           @transaction = JSON.parse(line)  
           if @transaction["time_format"].include?(review_date) 

           end     
         end       
         tp report



TIME_FORMAT      | PURCHASE | QUANTITY | RETAIL_PRICE | PURCHASING_PRICE
-----------------|----------|----------|--------------|-----------------
11/23/2013 08:07 | ["lig... | [1]      | [5.0]        | [3.0]           
11/23/2013 08:08 | ["lig... | [1]      | [5.0]        | [3.0]           
11/23/2013 08:08 | ["lig... | [1, 1... | [5.0, 7.5... | [3.0, 4.0, 5....
11/23/2013 08:12 | ["lig... | [1]      | [5.0]        | [3.0]           
11/23/2013 08:13 | ["lig... | [1]      | [5.0]        | [3.0]           
11/23/2013 08:18 | ["lig... | [1]      | [5.0]        | [3.0]           

Edit: The gem is called Table Print

Was it helpful?

Solution

There are a couple ways you can change the column widths.

To change all columns at once:

tp.set :max_width, 60

To change an individual column, print as usual but give some column options:

tp report, :include => [:purchase => {:width => 60}]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top