flexible_table 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 87a39f425b93bfe9764411addd45abb6a172ba35
4
+ data.tar.gz: 2863f2030c00a87bc9e7962b4f1ff188394c3d65
5
+ SHA512:
6
+ metadata.gz: e7789777d44279338c617989f360e2c784dbf5d775bc03e63546422fc37d8c6f00b985dc4ff8963385cbfcc7eff9e05c587070739103f693d582766aa9895126
7
+ data.tar.gz: 3ecc8474a3ad64277977659a13e849880da070d1c5b393ad970aa18251aaac9d75ea20c58cff75c14a6cd3e238de491de901916f85a627435219b416d24025f0
@@ -0,0 +1,7 @@
1
+ class FlexibleColumn
2
+ def initialize(header, width_percentage)
3
+ @header = header
4
+ @width_percentage = width_percentage
5
+ end
6
+ attr_reader :width_percentage, :header
7
+ end
@@ -0,0 +1,44 @@
1
+ require_relative './flexible_column'
2
+ require_relative './print_helper'
3
+ require 'io/console'
4
+
5
+ class FlexibleTable
6
+ include PrintHelper
7
+
8
+ def initialize
9
+ @screen_width = IO.console.winsize[1]
10
+ @columns = []
11
+ end
12
+
13
+ def add_column(title, width_percentage)
14
+ @columns << FlexibleColumn.new(title, width_percentage)
15
+ end
16
+
17
+ def print_header()
18
+ validate_column_width_percentages
19
+
20
+ @columns.each_with_index do |col, index|
21
+ abs_width = get_abs_width(col.width_percentage, 3)
22
+ output = get_printable_output(col.header, abs_width)
23
+
24
+ printf("%-#{abs_width}s", output)
25
+ printf(' | ') unless index == (@columns.length - 1)
26
+ end
27
+
28
+ puts "\n"
29
+ print_header_line
30
+ end
31
+
32
+ def print_row(*args)
33
+ validate_column_width_percentages
34
+
35
+ args.each_with_index do |element, index|
36
+ abs_width = get_abs_width(@columns[index].width_percentage, 3)
37
+ output = get_printable_output(element, abs_width)
38
+
39
+ printf("%-#{abs_width}s", output)
40
+ printf(' | ') unless index == (@columns.length - 1)
41
+ end
42
+ puts "\n"
43
+ end
44
+ end
@@ -0,0 +1,33 @@
1
+ module PrintHelper
2
+ private
3
+ def get_abs_width(width_percentage, offset=0)
4
+ abs_width = (Integer(((width_percentage.to_f / 100.to_f) * @screen_width.to_f).floor)) - offset
5
+
6
+ return abs_width <= 0 ? 1 : abs_width
7
+ end
8
+
9
+ def get_printable_output(str, abs_width)
10
+ return str[0..(abs_width - 1)]
11
+ end
12
+
13
+ def print_header_line
14
+ line_header = ''
15
+
16
+ @columns.each do |col|
17
+ get_abs_width(col.width_percentage).times do |t|
18
+ line_header << '-'
19
+ end
20
+ end
21
+ puts "#{line_header}\n"
22
+ end
23
+
24
+ def validate_column_width_percentages
25
+ total_percentage = (@columns.map { |x| x.width_percentage }).reduce(:+)
26
+ if(total_percentage > 100)
27
+ puts "Your column percentages can't add up to more than 100%\n"
28
+ @columns.each { |col| puts "\t#{col.header}: \t#{col.width_percentage}" }
29
+ puts "\tTotal percetage: #{total_percentage}"
30
+ exit
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flexible_table
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brett Sykes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Prints a table that can have different column widths that also respond
14
+ in size relative to the size of the terminal window
15
+ email: brettcsykes@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/flexible_column.rb
21
+ - lib/flexible_table.rb
22
+ - lib/print_helper.rb
23
+ homepage: https://github.com/bretts/flexible_table
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.4.5
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Prints a table that can have different column widths that also respond in
47
+ size relative to the size of the terminal window
48
+ test_files: []