PrettyComment 0.1.0

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 (2) hide show
  1. data/lib/PrettyComment.rb +154 -0
  2. metadata +62 -0
@@ -0,0 +1,154 @@
1
+ # encoding: utf-8
2
+ #======================================================================================================================#
3
+ require 'terminfo'
4
+
5
+ #======================================================================================================================#
6
+
7
+ module PrettyComment
8
+
9
+
10
+ #======================================================================================================================#
11
+
12
+ def self.terminal_width
13
+ TermInfo.screen_size[1]
14
+ end
15
+
16
+
17
+ #----------------------------------------------------------------------------------------------------------------------#
18
+
19
+ def self.separator(char='=')
20
+ '#' + (char * (terminal_width - 2)) + '#'
21
+ end
22
+
23
+
24
+ #----------------------------------------------------------------------------------------------------------------------#
25
+
26
+ def self.small_separator
27
+ separator ('-')
28
+ end
29
+
30
+
31
+ #----------------------------------------------------------------------------------------------------------------------#
32
+
33
+ def self.comment(comment)
34
+ pad_string("\# #{comment}", "#", terminal_width)
35
+ end
36
+
37
+
38
+ #----------------------------------------------------------------------------------------------------------------------#
39
+
40
+ def self.sub_heading(comment)
41
+ [comment(""), small_separator, comment(comment), small_separator].join("\n")
42
+ end
43
+
44
+
45
+ #----------------------------------------------------------------------------------------------------------------------#
46
+
47
+ def self.pad_string(a_string, a_suffix, a_width)
48
+ a_suffix.length ? a_string + " " * (a_width - a_string.length - a_suffix.length) + a_suffix : a_string.dup
49
+ end
50
+
51
+
52
+ #----------------------------------------------------------------------------------------------------------------------#
53
+
54
+ def self.format_line(text, prefix, only_first_line_prefix=false, suffix='', alternate_prefix='')
55
+ the_suffix = suffix.length ? " " + suffix : ""
56
+ text_width = terminal_width - 1 - the_suffix.length
57
+ title_array = text.split(" ")
58
+ result = []
59
+ current_line = prefix.dup
60
+ current_prefix =
61
+ only_first_line_prefix \
62
+ ? alternate_prefix + " " * (prefix.length - alternate_prefix.length) \
63
+ : prefix.dup
64
+
65
+ title_array.each do |w|
66
+ (current_line.length + w.length + 1 > text_width) \
67
+ && result << pad_string(current_line, suffix, terminal_width) \
68
+ && current_line = current_prefix.dup
69
+ current_line << " " << w
70
+ end
71
+
72
+ current_line.length > current_prefix.length && result << pad_string(current_line, suffix, terminal_width)
73
+ result.join("\n")
74
+ end
75
+
76
+ #----------------------------------------------------------------------------------------------------------------------#
77
+
78
+ def self.heading(a_text, a_delimiter = '=')
79
+ Box.new { self.top = self.bottom = a_delimiter; para a_text}.to_s
80
+ end
81
+
82
+
83
+ #----------------------------------------------------------------------------------------------------------------------#
84
+
85
+ def self.h1(a_text)
86
+ self.heading(a_text, '#')
87
+ end
88
+
89
+
90
+ #----------------------------------------------------------------------------------------------------------------------#
91
+
92
+ def self.h2(a_text)
93
+ self.heading(a_text, '=')
94
+ end
95
+
96
+
97
+ #----------------------------------------------------------------------------------------------------------------------#
98
+
99
+ def self.h3(a_text)
100
+ self.heading(a_text, '-')
101
+ end
102
+
103
+
104
+ #----------------------------------------------------------------------------------------------------------------------#
105
+
106
+ class Box
107
+ attr_accessor :left, :right, :top, :bottom
108
+ def initialize *a, &b
109
+ @left = @right = '#'
110
+ @top = @bottom = '='
111
+ @width = PrettyComment.terminal_width
112
+ @linebuffer = []
113
+ cloaker(&b).bind(self).call(*a) if b
114
+ end
115
+
116
+
117
+ #----------------------------------------------------------------------------------------------------------------------#
118
+
119
+ def para(a_string)
120
+ @linebuffer << PrettyComment::format_line(a_string, left, false, right)
121
+ end
122
+
123
+
124
+ #----------------------------------------------------------------------------------------------------------------------#
125
+
126
+ def hline(a_char = @top)
127
+ @linebuffer << PrettyComment.separator(a_char)
128
+ end
129
+
130
+
131
+ #----------------------------------------------------------------------------------------------------------------------#
132
+
133
+ def to_s
134
+ PrettyComment::separator(top) + @linebuffer.join("\n") + PrettyComment::separator(bottom)
135
+ end
136
+
137
+ #----------------------------------------------------------------------------------------------------------------------#
138
+
139
+ def cloaker &b
140
+ (class << self; self; end).class_eval do
141
+ define_method :cloaker_, &b
142
+ meth = instance_method :cloaker_
143
+ remove_method :cloaker_
144
+ meth
145
+ end
146
+ end
147
+ end
148
+
149
+
150
+ #----------------------------------------------------------------------------------------------------------------------#
151
+
152
+ end
153
+
154
+
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: PrettyComment
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Wolfgang Steiner
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ruby-terminfo
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.1.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.1.1
30
+ description: PrettyComment allows for nice formatting of status messages and comments
31
+ for terminal output.
32
+ email: wolfgang.steiner@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/PrettyComment.rb
38
+ homepage: http://rubygems.org/gems/PrettyComment
39
+ licenses: []
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.23
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Nice formatting of comments.
62
+ test_files: []