catsheet 1.1.0 → 1.1.2

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.
Files changed (3) hide show
  1. data/bin/catsheet +3 -0
  2. data/lib/catsheet.rb +83 -71
  3. metadata +6 -5
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'catsheet'
3
+ Catsheet::Sheetscript.main
@@ -3,85 +3,97 @@
3
3
  # Website: https://github.com/the-abe
4
4
  # Date: 2015-04-16
5
5
  # File: catsheet.rb
6
-
7
6
  require 'rubygems'
8
- # Require the spreadsheet gem and check for success.
9
- begin
10
- require 'spreadsheet'
11
- rescue LoadError
12
- puts "Could not load the spreadsheet gem. Please run \"gem install spreadsheet\"."
13
- exit 1
14
- end
7
+ require 'catsheet'
15
8
 
16
- # Return the length of the string, taking multibyte characters into account.
17
- def string_length(string)
18
- return string.scan(/./mu).size
19
- end
9
+ module Catsheet
10
+ class Sheetscript
11
+ # Require the spreadsheet gem and check for success.
12
+ begin
13
+ require 'spreadsheet'
14
+ rescue LoadError
15
+ puts "Could not load the spreadsheet gem. Please run \"gem install spreadsheet\"."
16
+ exit 1
17
+ end
20
18
 
21
- # Check if the name of a spreadsheet was supplied.
22
- if ARGV[0].nil?
23
- puts "You did not supply a spreadsheet to open."
24
- exit 1
25
- end
19
+ def self.main
20
+ # Check if the name of a spreadsheet was supplied.
21
+ if ARGV[0].nil?
22
+ puts "You did not supply a spreadsheet to open."
23
+ exit 1
24
+ end
26
25
 
27
- # Open the spreadsheet supplied in the arguments
28
- begin
29
- book = Spreadsheet.open(ARGV[0],'r')
30
- rescue Ole::Storage::FormatError
31
- puts "The file you wanted to open seems to be invalid."
32
- exit 1
33
- end
26
+ # Open the spreadsheet supplied in the arguments
27
+ begin
28
+ book = Spreadsheet.open(ARGV[0],'r')
29
+ rescue Ole::Storage::FormatError
30
+ puts "The file you wanted to open seems to be invalid."
31
+ exit 1
32
+ rescue Errno::ENOENT
33
+ puts "The file you wanted to open can't be opened."
34
+ exit 1
35
+ end
34
36
 
35
- # If a sheet number is supplied, use it.
36
- # The spreadsheet gem starts counting from 0, to we decrease the number by one.
37
- # If no number is supplied we assume the first one.
38
- sheet_number = (ARGV[1]||1).to_i - 1
39
- sheet = book.worksheet sheet_number
40
- if sheet.nil?
41
- puts "There doesn't seem to be a sheet #{sheet_number}."
42
- exit 1
43
- end
37
+ # If a sheet number is supplied, use it.
38
+ # The spreadsheet gem starts counting from 0, to we decrease the number by one.
39
+ # If no number is supplied we assume the first one.
40
+ sheet_number = (ARGV[1]||1).to_i - 1
41
+ sheet = book.worksheet sheet_number
42
+ if sheet.nil?
43
+ puts "There doesn't seem to be a sheet #{sheet_number}."
44
+ exit 1
45
+ end
44
46
 
45
- # Build and array of column widths based on the max width per column.
46
- width_array = []
47
- column_counter = 0
48
- sheet.each do |row|
49
- column_counter = row.length-1 if row.length-1 > column_counter
50
- row.each_with_index do |cell,index|
51
- row.format 2
52
- if width_array[index].nil? || width_array[index] < string_length(cell.to_s)
53
- cell = " " if cell.nil?
54
- width_array[index] = string_length(cell.to_s)
47
+ # Build and array of column widths based on the max width per column.
48
+ width_array = []
49
+ column_counter = 0
50
+ sheet.each do |row|
51
+ column_counter = row.length-1 if row.length-1 > column_counter
52
+ row.each_with_index do |cell,index|
53
+ row.format 2
54
+ if width_array[index].nil? || width_array[index] < string_length(cell.to_s)
55
+ cell = " " if cell.nil?
56
+ width_array[index] = string_length(cell.to_s)
57
+ end
58
+ end
59
+ end
60
+
61
+ # Now that we've got the maximum width per column, we can print a table!
62
+ sheet.each do |row|
63
+ # Assume we only need two decimals for numbers.
64
+ row.format 2
65
+ row_array = []
66
+ row.each_with_index do |cell,index|
67
+ # Put a space in empty cells so we don't break the layout of the table.
68
+ cell = " " if cell.nil?
69
+ # Get the maximum length from the array and add spaces to make the cell as
70
+ # long as the rest.
71
+ length = width_array[index]
72
+ filler = length - string_length(cell.to_s)
73
+ #Cells without digits are left aligned number cells are right aligned
74
+ if cell =~ /[^\d\.]+/
75
+ row_array << "#{cell}#{' '*filler}"
76
+ else
77
+ row_array << "#{' '*filler}#{cell}"
78
+ end
79
+ end
80
+ # Loop through it all and fill empty cells at the end of the row.
81
+ result_columns = (0..column_counter).map do |index|
82
+ row_array[index].nil? ? " "*width_array[index] : row_array[index]
83
+ end
84
+ result = "|"
85
+ result += result_columns.join("|")
86
+ result += "|"
87
+ puts result
88
+ end
55
89
  end
56
- end
57
- end
58
90
 
59
- # Now that we've got the maximum width per column, we can print a table!
60
- sheet.each do |row|
61
- # Assume we only need two decimals for numbers.
62
- row.format 2
63
- row_array = []
64
- row.each_with_index do |cell,index|
65
- # Put a space in empty cells so we don't break the layout of the table.
66
- cell = " " if cell.nil?
67
- # Get the maximum length from the array and add spaces to make the cell as
68
- # long as the rest.
69
- length = width_array[index]
70
- filler = length - string_length(cell.to_s)
71
- #Cells without digits are left aligned number cells are right aligned
72
- if cell =~ /[^\d\.]+/
73
- row_array << "#{cell}#{' '*filler}"
74
- else
75
- row_array << "#{' '*filler}#{cell}"
91
+ private
92
+
93
+ # Return the length of the string, taking multibyte characters into account.
94
+ def string_length(string)
95
+ return string.scan(/./mu).size
76
96
  end
97
+
77
98
  end
78
- # Loop through it all and fill empty cells at the end of the row.
79
- result_columns = (0..column_counter).map do |index|
80
- row_array[index].nil? ? " "*width_array[index] : row_array[index]
81
- end
82
- result = "|"
83
- result += result_columns.join("|")
84
- result += "|"
85
- puts result
86
99
  end
87
- exit 0
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: catsheet
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
- - 0
10
- version: 1.1.0
9
+ - 2
10
+ version: 1.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Abe van der Wielen
@@ -33,13 +33,14 @@ dependencies:
33
33
  version_requirements: *id001
34
34
  description: A simple gem that prints spreadsheets like cat prints text files.
35
35
  email: abevanderwielen@gmail.com
36
- executables: []
37
-
36
+ executables:
37
+ - catsheet
38
38
  extensions: []
39
39
 
40
40
  extra_rdoc_files: []
41
41
 
42
42
  files:
43
+ - bin/catsheet
43
44
  - lib/catsheet.rb
44
45
  homepage: https://github.com/the-abe/catsheet
45
46
  licenses: []