shellout 0.2 → 0.3

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.
data/CHANGES ADDED
@@ -0,0 +1,11 @@
1
+ 2011-10-31: version 0.3
2
+
3
+ - Added a class for printing calendars
4
+
5
+ 2011-10-21: version 0.2
6
+
7
+ - Fixed bug that caused wrong justification of numeric columns in tables.
8
+
9
+ 2011-10-21: version 0.1
10
+
11
+ - Initial release
data/README.md CHANGED
@@ -22,9 +22,9 @@ Usage
22
22
  Shellout::Shadowbox.new("Hello world").print
23
23
 
24
24
  # ┌──────────────────────────────────────────┐
25
- # │ Hello world │▒
26
- # └──────────────────────────────────────────┘▒
27
- # ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
25
+ # │ Hello world │▒
26
+ # └──────────────────────────────────────────┘▒
27
+ # ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
28
28
 
29
29
 
30
30
  ### Tables
@@ -35,8 +35,7 @@ Usage
35
35
  rows: [['Man City', 22],
36
36
  ['Man Utd', 20],
37
37
  ['Chelsea', 19],
38
- ['Newcastle', 16]]
39
- ).print
38
+ ['Newcastle', 16]]).print
40
39
 
41
40
  # ┌───────────┬────────┐
42
41
  # │ Team │ Points │
@@ -48,6 +47,49 @@ Usage
48
47
  # └───────────┴────────┘
49
48
 
50
49
 
50
+ ### Calendars
51
+
52
+ require 'date'
53
+ require 'shellout/calendar'
54
+
55
+ today = Date.today # => "2011-10-31"
56
+
57
+ Shellout::Calendar.new(today).print
58
+
59
+ # October 2011
60
+ # Mo Tu We Th Fr Sa Su
61
+ # 1 2
62
+ # 3 4 5 6 7 8 9
63
+ # 10 11 12 13 14 15 16
64
+ # 17 18 19 20 21 22 23
65
+ # 24 25 26 27 28 29 30
66
+ # 31
67
+
68
+ Shellout::Calendar.new(today.prev_month,
69
+ today,
70
+ today.next_month).print
71
+
72
+ # September 2011 October 2011 November 2011
73
+ # Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
74
+ # 1 2 3 4 1 2 1 2 3 4 5 6
75
+ # 5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13
76
+ # 12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20
77
+ # 19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27
78
+ # 26 27 28 29 30 24 25 26 27 28 29 30 28 29 30
79
+ # 31
80
+
81
+ Bugs
82
+ ----
83
+
84
+ Report bugs to <http://github.com/kjellm/shellout/issues>
85
+
86
+
87
+ Author
88
+ ------
89
+
90
+ Kjell-Magne Øierud &lt;kjellm AT acm DOT org&gt;
91
+
92
+
51
93
  License
52
94
  -------
53
95
 
@@ -0,0 +1,94 @@
1
+ module Shellout
2
+ class Calendar
3
+
4
+ def initialize(*dates)
5
+ @dates = dates
6
+ @list_of_days = "Mo Tu We Th Fr Sa Su"
7
+ reset
8
+ end
9
+
10
+ def print(out=$stdout)
11
+ @out=out
12
+ @dates.each do |d|
13
+ print_calendar(d)
14
+ unless d == @dates.last # FIXME
15
+ @col += 24
16
+ @out.print ansi_cursor_up(@lines) + ansi_cursor_right(@col)
17
+ @lines = 1
18
+ end
19
+ end
20
+ reset
21
+ #puts
22
+ end
23
+
24
+ private
25
+
26
+ def reset
27
+ @lines = 1
28
+ @col = 0
29
+ end
30
+
31
+ def print_calendar(for_date)
32
+ print_heading(for_date)
33
+ print_dates(for_date)
34
+ end
35
+
36
+ def print_heading(for_date)
37
+ print_line("#{Date::MONTHNAMES[for_date.month]} #{for_date.year}".center(@list_of_days.length))
38
+ print_line(@list_of_days)
39
+ end
40
+
41
+ def print_dates(for_date)
42
+ week = []
43
+ days(for_date).each_with_index do |d, i|
44
+ d = is_current_month?(for_date) && d == Date.today.day \
45
+ ? ansi_reverse_color(d) : d
46
+ week << "%2s" % d
47
+ if end_of_week?(i)
48
+ print_week(week)
49
+ week = []
50
+ end
51
+ end
52
+ print_week(week) unless week.empty?
53
+ @out.puts
54
+ end
55
+
56
+ def print_week(week)
57
+ print_line week.join(' ')
58
+ end
59
+
60
+ def end_of_week?(i)
61
+ (i+1) % 7 == 0
62
+ end
63
+
64
+ def is_current_month?(date)
65
+ Date.today.month == date.month
66
+ end
67
+
68
+ def print_line(s="")
69
+ @lines += 1
70
+ @out.print s
71
+ @out.print @col == 0 ? "\n" : ansi_cursor_down(1) + ansi_cursor_left(@list_of_days.length)
72
+ end
73
+
74
+ def days(for_date)
75
+ first_day_of_month = Date.new(for_date.year, for_date.month)
76
+ days = (1..last_day_of_month(first_day_of_month).day).to_a
77
+ wday = (first_day_of_month.wday - 1) % 7
78
+ days.unshift(*Array.new(wday, ""))
79
+ days
80
+ end
81
+
82
+ def last_day_of_month(first_day_of_month)
83
+ first_day_of_month.next_month-1
84
+ end
85
+
86
+ def ansi_reverse_color(s); "\e[7m#{s}\e[0m"; end
87
+
88
+ def ansi_cursor_up(n); "\e[#{n}A"; end
89
+ def ansi_cursor_down(n); "\e[#{n}B"; end
90
+ def ansi_cursor_left(n); "\e[#{n}D"; end
91
+ def ansi_cursor_right(n); "\e[#{n}C"; end
92
+
93
+ end
94
+ end
@@ -0,0 +1,3 @@
1
+ module Shellout
2
+ VERSION = '0.3'
3
+ end
data/lib/shellout.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
 
2
3
  # Unicode box drawing characters and block elements
3
4
  #
@@ -18,7 +19,8 @@
18
19
  # U+259x ▐ ░ ▒ ▓ ▔ ▕ ▖ ▗ ▘ ▙ ▚ ▛ ▜ ▝ ▞ ▟
19
20
 
20
21
  module Shellout
21
-
22
+
23
+ autoload :Calendar, 'shellout/calendar'
22
24
  autoload :Shadowbox, 'shellout/shadowbox'
23
25
  autoload :Table, 'shellout/table'
24
26
 
data/shellout.gemspec CHANGED
@@ -1,8 +1,10 @@
1
1
  # encoding: utf-8
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require 'shellout/version'
2
4
 
3
5
  Gem::Specification.new do |s|
4
6
  s.name = "shellout"
5
- s.version = "0.2"
7
+ s.version = Shellout::VERSION
6
8
  s.authors = ["Kjell-Magne Øierud"]
7
9
  s.email = ["kjellm@acm.org"]
8
10
  s.homepage = "https://github.com/kjellm/shellout"
@@ -0,0 +1,60 @@
1
+ require 'date'
2
+ require 'stringio'
3
+ require 'shellout/calendar'
4
+
5
+ class Date
6
+ def self.today
7
+ Date.new(2011, 10, 30)
8
+ end
9
+ end
10
+
11
+ describe Shellout::Calendar do
12
+
13
+ before(:each) do
14
+ @out = StringIO.new
15
+ end
16
+
17
+ describe 'when one date given' do
18
+ it 'should print a calendar for the given month' do
19
+ Shellout::Calendar.new(Date.new(2011, 10)).print(@out)
20
+ @out.string.should == <<EOC
21
+ October 2011
22
+ Mo Tu We Th Fr Sa Su
23
+ 1 2
24
+ 3 4 5 6 7 8 9
25
+ 10 11 12 13 14 15 16
26
+ 17 18 19 20 21 22 23
27
+ 24 25 26 27 28 29 \e[7m30\e[0m
28
+ 31
29
+
30
+ EOC
31
+ end
32
+ end
33
+
34
+ describe 'when three dates are given' do
35
+ it 'should print all three calendars on one row' do
36
+ Shellout::Calendar.new(Date.new(2011, 7), Date.new(2011, 8), Date.new(2011, 9)).print(@out)
37
+ exp = <<EOC
38
+ July 2011
39
+ Mo Tu We Th Fr Sa Su
40
+ 1 2 3
41
+ 4 5 6 7 8 9 10
42
+ 11 12 13 14 15 16 17
43
+ 18 19 20 21 22 23 24
44
+ 25 26 27 28 29 30 31
45
+
46
+ \e[8A\e[24C August 2011 \e[1B\e[20DMo Tu We Th Fr Sa Su\e[1B\e[20D 1 2 3 4 5 6 7\e[1B\e[20D 8 9 10 11 12 13 14\e[1B\e[20D15 16 17 18 19 20 21\e[1B\e[20D22 23 24 25 26 27 28\e[1B\e[20D29 30 31\e[1B\e[20D
47
+ \e[8A\e[48C September 2011 \e[1B\e[20DMo Tu We Th Fr Sa Su\e[1B\e[20D 1 2 3 4\e[1B\e[20D 5 6 7 8 9 10 11\e[1B\e[20D12 13 14 15 16 17 18\e[1B\e[20D19 20 21 22 23 24 25\e[1B\e[20D26 27 28 29 30\e[1B\e[20D
48
+ EOC
49
+ # July 2011 August 2011 September 2011
50
+ # Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
51
+ # 1 2 3 1 2 3 4 5 6 7 1 2 3 4
52
+ # 4 5 6 7 8 9 10 8 9 10 11 12 13 14 5 6 7 8 9 10 11
53
+ # 11 12 13 14 15 16 17 15 16 17 18 19 20 21 12 13 14 15 16 17 18
54
+ # 18 19 20 21 22 23 24 22 23 24 25 26 27 28 19 20 21 22 23 24 25
55
+ # 25 26 27 28 29 30 31 29 30 31 26 27 28 29 30
56
+ #
57
+ @out.string.inspect.should == exp.inspect # using inspect to help debugging
58
+ end
59
+ end
60
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shellout
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-21 00:00:00.000000000Z
12
+ date: 2011-10-31 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70359984070900 !ruby/object:Gem::Requirement
16
+ requirement: &9770540 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70359984070900
24
+ version_requirements: *9770540
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: guard
27
- requirement: &70359984070420 !ruby/object:Gem::Requirement
27
+ requirement: &9770040 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70359984070420
35
+ version_requirements: *9770040
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: guard-rspec
38
- requirement: &70359984069960 !ruby/object:Gem::Requirement
38
+ requirement: &9769360 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,29 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70359984069960
47
- - !ruby/object:Gem::Dependency
48
- name: rb-fsevent
49
- requirement: &70359984069380 !ruby/object:Gem::Requirement
50
- none: false
51
- requirements:
52
- - - ! '>='
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- type: :development
56
- prerelease: false
57
- version_requirements: *70359984069380
58
- - !ruby/object:Gem::Dependency
59
- name: growl_notify
60
- requirement: &70359984068860 !ruby/object:Gem::Requirement
61
- none: false
62
- requirements:
63
- - - ! '>='
64
- - !ruby/object:Gem::Version
65
- version: '0'
66
- type: :development
67
- prerelease: false
68
- version_requirements: *70359984068860
46
+ version_requirements: *9769360
69
47
  description: Contains classes for printing tables and boxes
70
48
  email:
71
49
  - kjellm@acm.org
@@ -74,14 +52,18 @@ extensions: []
74
52
  extra_rdoc_files: []
75
53
  files:
76
54
  - .gitignore
55
+ - CHANGES
77
56
  - Gemfile
78
57
  - Guardfile
79
58
  - README.md
80
59
  - Rakefile
81
60
  - lib/shellout.rb
61
+ - lib/shellout/calendar.rb
82
62
  - lib/shellout/shadowbox.rb
83
63
  - lib/shellout/table.rb
64
+ - lib/shellout/version.rb
84
65
  - shellout.gemspec
66
+ - spec/shellout/calendar_spec.rb
85
67
  - spec/shellout/shadowbox_spec.rb
86
68
  - spec/shellout/table_spec.rb
87
69
  - spec/shellout_spec.rb
@@ -111,6 +93,8 @@ signing_key:
111
93
  specification_version: 3
112
94
  summary: Tools for writing terminal interfaces
113
95
  test_files:
96
+ - spec/shellout/calendar_spec.rb
114
97
  - spec/shellout/shadowbox_spec.rb
115
98
  - spec/shellout/table_spec.rb
116
99
  - spec/shellout_spec.rb
100
+ has_rdoc: