flexible_table 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/flexible_column.rb +18 -7
- data/lib/flexible_table.rb +69 -51
- data/lib/print_helper.rb +68 -31
- data/lib/terminal_utils.rb +89 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7820ebcd7f52ec82dc683231eb1a0d9bdd01a198
|
4
|
+
data.tar.gz: 03a373e6e7e954b5c68633800c1d2fffb6710c0d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da3febe12f4c2820748e6ef503cbd1c4a6459d8890ed4ddaf0ba4ad92c2187df0cca5688d74292481be93d4460bd71f158ef1573ddf1b271db178b9fedf7d5d2
|
7
|
+
data.tar.gz: d238dafe7c473efea8d96e7a70fd8e26cb8bdaa122fe80143926d15a6c5dd6b3bcb6d34072746bd768290e094aa7a2fb41ce605d916d68318f6a6451a9f832e8
|
data/lib/flexible_column.rb
CHANGED
@@ -1,9 +1,20 @@
|
|
1
|
+
##
|
2
|
+
# This class represents a column in the table. Instances of this class will get created by FlexibleTable#add_column and used inside an instance of a FlexibleTable.
|
3
|
+
#
|
4
|
+
# It's important to note that the width_percentages of all FlexibleColmn's inside an instance of FlexibleTable must add up to less than or equal to 100.
|
1
5
|
class FlexibleColumn
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
|
7
|
+
# === Attributes
|
8
|
+
#
|
9
|
+
# * +header+ - A String that will be displayed as the name of the column
|
10
|
+
# * +width_percentage+ - An Integer that will be the percentage of the terminal that the column should take up. The total columns width_percentages must add up to less than 100
|
11
|
+
# * +justify_header+ - Optional param that takes a symbol (:left or :right) that determines the justification of the column header
|
12
|
+
# * +justify_row+ - Optional param that takes a symbol (:left or :right) that determines the justification of the column cells
|
13
|
+
def initialize(header, width_percentage, justify_header: :left, justify_row: :left)
|
14
|
+
@header = header
|
15
|
+
@width_percentage = width_percentage
|
16
|
+
@justify_header = justify_header
|
17
|
+
@justify_row = justify_row
|
18
|
+
end
|
19
|
+
attr_reader :width_percentage, :header, :justify_row, :justify_header
|
9
20
|
end
|
data/lib/flexible_table.rb
CHANGED
@@ -1,57 +1,75 @@
|
|
1
1
|
require_relative './flexible_column'
|
2
2
|
require_relative './print_helper'
|
3
3
|
require 'io/console'
|
4
|
+
require_relative './terminal_utils'
|
4
5
|
|
6
|
+
##
|
7
|
+
# This is the main class for creating Tables. The user can create a series of columns, and the print those columns out.
|
8
|
+
# See the README for more example usages.
|
9
|
+
#
|
10
|
+
# === Examples
|
11
|
+
#
|
12
|
+
# table = FlexibleTable.new
|
13
|
+
# table.add_column("I'm the first column", 10)
|
14
|
+
# table.add_column("I'm the second column", 10)
|
15
|
+
# table.print_header
|
16
|
+
# table.print_row("I'll be in column 1", "I'll be in column 2")
|
17
|
+
# table.print_row("I'll be in column 1", "I'll be in column 2")
|
18
|
+
# table.print_row("I'll be in column 1", "I'll be in column 2")
|
5
19
|
class FlexibleTable
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
20
|
+
include PrintHelper
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
@total_screen_columns = IO.console.winsize[1]
|
24
|
+
@columns = []
|
25
|
+
end
|
26
|
+
|
27
|
+
def add_column(title, width_percentage, **args)
|
28
|
+
@columns << FlexibleColumn.new(title, width_percentage, args)
|
29
|
+
end
|
30
|
+
|
31
|
+
def print_header()
|
32
|
+
exit unless columns_fit_screen?(@columns)
|
33
|
+
|
34
|
+
@columns.each_with_index do |col, index|
|
35
|
+
abs_width = get_abs_column_width(@total_screen_columns, col.width_percentage)
|
36
|
+
color_prefix = col.header.instance_eval { @tu_color_prefix } || ''
|
37
|
+
color_suffix = col.header.instance_eval { @tu_color_suffix } || ''
|
38
|
+
output = get_printable_output(col.header, abs_width)
|
39
|
+
|
40
|
+
if(@columns[index].justify_header == :left)
|
41
|
+
printf("%s%-#{abs_width}s%s", color_prefix, output, color_suffix)
|
42
|
+
elsif(@columns[index].justify_header == :right)
|
43
|
+
printf("%s%#{abs_width}s%s", color_prefix, output, color_suffix)
|
44
|
+
else
|
45
|
+
printf("%s%-#{abs_width}s%s", color_prefix, output, color_suffix) # default to left justify
|
46
|
+
end
|
47
|
+
print_column_separator unless index == (@columns.length - 1)
|
48
|
+
end
|
49
|
+
|
50
|
+
puts "\n"
|
51
|
+
print_header_line(@total_screen_columns)
|
52
|
+
end
|
53
|
+
|
54
|
+
def print_row(*args)
|
55
|
+
exit unless columns_fit_screen?(@columns)
|
56
|
+
|
57
|
+
args.each_with_index do |element, index|
|
58
|
+
abs_width = get_abs_column_width(@total_screen_columns, @columns[index].width_percentage)
|
59
|
+
color_prefix = element.instance_eval { @tu_color_prefix } || ''
|
60
|
+
color_suffix = element.instance_eval { @tu_color_suffix } || ''
|
61
|
+
output = get_printable_output(element, abs_width)
|
62
|
+
|
63
|
+
if(@columns[index].justify_row == :left)
|
64
|
+
printf("%s%-#{abs_width}s%s", color_prefix, output, color_suffix)
|
65
|
+
elsif(@columns[index].justify_row == :right)
|
66
|
+
printf("%s%#{abs_width}s%s", color_prefix, output, color_suffix)
|
67
|
+
else
|
68
|
+
printf("%s%-#{abs_width}s%s", color_prefix, output, color_suffix) # default to left justify
|
69
|
+
end
|
70
|
+
|
71
|
+
print_column_separator unless index == (@columns.length - 1)
|
72
|
+
end
|
73
|
+
puts "\n"
|
74
|
+
end
|
57
75
|
end
|
data/lib/print_helper.rb
CHANGED
@@ -1,33 +1,70 @@
|
|
1
1
|
module PrintHelper
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
2
|
+
##
|
3
|
+
# Returns the width that a column should occupy based on screen size
|
4
|
+
#
|
5
|
+
# === Attributes
|
6
|
+
#
|
7
|
+
# +total_screen_columns+ - An Integer representing columns on the terminal window
|
8
|
+
# +width_percentage+ - An Integer representing the percentage of the total_screen_columns that the column should occupy
|
9
|
+
def get_column_width(total_screen_columns, width_percentage)
|
10
|
+
column_width = (Integer(((width_percentage.to_f / 100.to_f) * total_screen_columns.to_f).floor))
|
11
|
+
|
12
|
+
return column_width
|
13
|
+
end
|
14
|
+
|
15
|
+
##
|
16
|
+
# Returns a slightly truncated version of #get_column_width in order to compensate for different font sizes.
|
17
|
+
# This is the method that should be used when calculating what gets printed into a column
|
18
|
+
#
|
19
|
+
# === Attributes
|
20
|
+
#
|
21
|
+
# +total_screen_columns+ - An Integer representing columns on the terminal window
|
22
|
+
# +width_percentage+ - An Integer representing the percentage of the total_screen_columns that the column should occupy
|
23
|
+
def get_abs_column_width(total_screen_columns, width_percentage)
|
24
|
+
column_width = get_column_width(total_screen_columns, width_percentage) - 3
|
25
|
+
|
26
|
+
return column_width <= 0 ? 1 : column_width
|
27
|
+
end
|
28
|
+
|
29
|
+
def get_printable_output(str, abs_width)
|
30
|
+
return str.to_s[0..(abs_width - 1)]
|
31
|
+
end
|
32
|
+
|
33
|
+
def print_header_line(total_screen_columns)
|
34
|
+
line_header = ''
|
35
|
+
separator = tty_supports_unicode? ? "\u2500" : '-'
|
36
|
+
|
37
|
+
@columns.each do |col|
|
38
|
+
get_column_width(total_screen_columns, col.width_percentage).times do |t|
|
39
|
+
line_header << separator
|
40
|
+
end
|
41
|
+
end
|
42
|
+
puts "#{line_header}\n"
|
43
|
+
end
|
44
|
+
|
45
|
+
def columns_fit_screen?(columns)
|
46
|
+
total_percentage = (columns.map { |x| x.width_percentage }).reduce(:+)
|
47
|
+
if(total_percentage > 100)
|
48
|
+
puts "Your column percentages can't add up to more than 100\n"
|
49
|
+
columns.each { |col| puts "\t#{col.header}: \t#{col.width_percentage}" }
|
50
|
+
puts "\tTotal percetage: #{total_percentage}"
|
51
|
+
|
52
|
+
return false
|
53
|
+
else
|
54
|
+
return true
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def print_column_separator
|
59
|
+
separator = tty_supports_unicode? ? "\u2502" : '|'
|
60
|
+
|
61
|
+
printf(" #{separator} ")
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
def tty_supports_unicode?
|
66
|
+
@tty_is_unicode_enabled ||= ENV.values_at("LC_ALL","LC_CTYPE","LANG").compact.any? { |x| x.downcase.include?('utf-8') }
|
67
|
+
|
68
|
+
return @tty_is_unicode_enabled
|
69
|
+
end
|
33
70
|
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module TerminalUtils
|
2
|
+
##
|
3
|
+
#
|
4
|
+
# This is a refinement on String that adds some properties to the String that represent ANSI Escape Sequences
|
5
|
+
#
|
6
|
+
# === Examples
|
7
|
+
#
|
8
|
+
# # Set the String foreground color property to red
|
9
|
+
# 'hello'.fg_red.tu_color_prefix
|
10
|
+
# => "\e[31m"
|
11
|
+
#
|
12
|
+
# 'hello'.fg_red.tu_color_suffix
|
13
|
+
# => "\e[0m"
|
14
|
+
#
|
15
|
+
#
|
16
|
+
# # Set the background color property to red
|
17
|
+
# 'hello'.bg_red.tu_color_prefix
|
18
|
+
# => "\e[41m"
|
19
|
+
#
|
20
|
+
# 'hello'.bg_red.tu_color_suffix
|
21
|
+
# => "\e[0m"
|
22
|
+
#
|
23
|
+
#
|
24
|
+
# # Set the foreground to red and the background to black
|
25
|
+
# 'hello'.fg_red.bg_black.tu_color_prefix
|
26
|
+
# => "\e[31m\e[40m"
|
27
|
+
#
|
28
|
+
# 'hello'.fg_red.bg_black.tu_color_suffix
|
29
|
+
# => "\e[0m"
|
30
|
+
module Color
|
31
|
+
refine String do
|
32
|
+
def fg_default; return fg_color("\e[39m"); end
|
33
|
+
def fg_black; return fg_color("\e[30m"); end
|
34
|
+
def fg_red; return fg_color("\e[31m"); end
|
35
|
+
def fg_green; return fg_color("\e[32m"); end
|
36
|
+
def fg_yellow; return fg_color("\e[33m"); end
|
37
|
+
def fg_blue; return fg_color("\e[34m"); end
|
38
|
+
def fg_magenta; return fg_color("\e[35m"); end
|
39
|
+
def fg_cyan; return fg_color("\e[36m"); end
|
40
|
+
def fg_grey; return fg_color("\e[37m"); end
|
41
|
+
def fg_bright_black; return fg_color("\e[90m"); end
|
42
|
+
def fg_bright_red; return fg_color("\e[91m"); end
|
43
|
+
def fg_bright_green; return fg_color("\e[92m"); end
|
44
|
+
def fg_bright_yellow; return fg_color("\e[93m"); end
|
45
|
+
def fg_bright_blue; return fg_color("\e[94m"); end
|
46
|
+
def fg_bright_magenta; return fg_color("\e[95m"); end
|
47
|
+
def fg_bright_cyan; return fg_color("\e[96m"); end
|
48
|
+
def fg_bright_grey; return fg_color("\e[97m"); end
|
49
|
+
|
50
|
+
def bg_default; return fg_color("\e[49m"); end
|
51
|
+
def bg_black; return bg_color("\e[40m"); end
|
52
|
+
def bg_red; return bg_color("\e[41m"); end
|
53
|
+
def bg_green; return bg_color("\e[42m"); end
|
54
|
+
def bg_yellow; return bg_color("\e[43m"); end
|
55
|
+
def bg_blue; return bg_color("\e[44m"); end
|
56
|
+
def bg_magenta; return bg_color("\e[45m"); end
|
57
|
+
def bg_cyan; return bg_color("\e[46m"); end
|
58
|
+
def bg_grey; return bg_color("\e[47m"); end
|
59
|
+
def bg_bright_black; return bg_color("\e[100m"); end
|
60
|
+
def bg_bright_red; return bg_color("\e[101m"); end
|
61
|
+
def bg_bright_green; return bg_color("\e[102m"); end
|
62
|
+
def bg_bright_yellow; return bg_color("\e[103m"); end
|
63
|
+
def bg_bright_blue; return bg_color("\e[104m"); end
|
64
|
+
def bg_bright_magenta; return bg_color("\e[105m"); end
|
65
|
+
def bg_bright_cyan; return bg_color("\e[106m"); end
|
66
|
+
def bg_bright_grey; return bg_color("\e[107m"); end
|
67
|
+
|
68
|
+
private
|
69
|
+
def fg_color(ansi_code)
|
70
|
+
@tu_fg_color = ansi_code
|
71
|
+
|
72
|
+
return color_string
|
73
|
+
end
|
74
|
+
|
75
|
+
def bg_color(ansi_code)
|
76
|
+
@tu_bg_color = ansi_code
|
77
|
+
|
78
|
+
return color_string
|
79
|
+
end
|
80
|
+
|
81
|
+
def color_string
|
82
|
+
@tu_color_prefix = [@tu_fg_color, @tu_bg_color].compact.join('')
|
83
|
+
@tu_color_suffix = "\e[0m"
|
84
|
+
|
85
|
+
return self
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flexible_table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brett Sykes
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-10-
|
11
|
+
date: 2016-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Prints a table that can have different column widths that also respond
|
14
14
|
in size relative to the size of the terminal window
|
@@ -20,6 +20,7 @@ files:
|
|
20
20
|
- lib/flexible_column.rb
|
21
21
|
- lib/flexible_table.rb
|
22
22
|
- lib/print_helper.rb
|
23
|
+
- lib/terminal_utils.rb
|
23
24
|
homepage: https://github.com/bretts/flexible_table
|
24
25
|
licenses:
|
25
26
|
- MIT
|
@@ -40,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
41
|
version: '0'
|
41
42
|
requirements: []
|
42
43
|
rubyforge_project:
|
43
|
-
rubygems_version: 2.
|
44
|
+
rubygems_version: 2.6.7
|
44
45
|
signing_key:
|
45
46
|
specification_version: 4
|
46
47
|
summary: Prints a table that can have different column widths that also respond in
|