choosy 0.4.4 → 0.4.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +2 -2
- data/lib/VERSION.yml +1 -1
- data/lib/choosy/printing/base_printer.rb +3 -9
- data/lib/choosy/printing/help_printer.rb +1 -2
- data/lib/choosy/printing/manpage.rb +1 -1
- data/lib/choosy/terminal.rb +132 -0
- data/spec/choosy/{printing/terminal_spec.rb → terminal_spec.rb} +2 -2
- metadata +4 -4
- data/lib/choosy/printing/terminal.rb +0 -84
data/README.markdown
CHANGED
@@ -436,7 +436,7 @@ For those who want the nice, manpage experience, there's also the <code>:manpage
|
|
436
436
|
version FOO_VERSION # If you don't supply a version above, this will be used
|
437
437
|
end
|
438
438
|
|
439
|
-
Because the library is super-awesome, the manpage will even be in color when piped to <code>less -R
|
439
|
+
Because the library is super-awesome, the manpage will even be in color when piped to <code>less -R</code> (the default)! If you don't like the format of my manpage, feel free to implement your own using the <code>choosy/printing/manpage</code> class, a useful utility class for formatting manpage output correctly.
|
440
440
|
|
441
441
|
If you already have some templates that you'd like to use, there is also the <code>:erb</code> template that can be customized by writing a template of your choice:
|
442
442
|
|
@@ -448,7 +448,7 @@ If you already have some templates that you'd like to use, there is also the <co
|
|
448
448
|
|
449
449
|
The ERB printer also accepts the <code>:color</code> option. The color is exposed via a <code>color</code> property in the template; the command is exposed by the <code>command</code> property.
|
450
450
|
|
451
|
-
Finally, because I don't want to tell you how to print your help, I also give you the option of supplying your own printer. Just create a class with a <code>print!(command)</code> method on that class, and it will be passed in the command that it should print the help for. I have supplied some code you may find useful in <code>choosy/
|
451
|
+
Finally, because I don't want to tell you how to print your help, I also give you the option of supplying your own printer. Just create a class with a <code>print!(command)</code> method on that class, and it will be passed in the command that it should print the help for. I have supplied some code you may find useful in <code>choosy/terminal</code> that will help with things like finding commands and determining the column width of the terminal.
|
452
452
|
|
453
453
|
class CustomPrinter
|
454
454
|
def print!(command)
|
data/lib/VERSION.yml
CHANGED
@@ -1,15 +1,9 @@
|
|
1
1
|
require 'choosy/errors'
|
2
|
-
require 'choosy/
|
3
|
-
|
4
|
-
class String
|
5
|
-
def unformatted
|
6
|
-
gsub(/\e\[\d+m/, '').gsub(/\\f[IPB]/, '')
|
7
|
-
end
|
8
|
-
end
|
2
|
+
require 'choosy/terminal'
|
9
3
|
|
10
4
|
module Choosy::Printing
|
11
5
|
class BasePrinter
|
12
|
-
include Terminal
|
6
|
+
include Choosy::Terminal
|
13
7
|
|
14
8
|
attr_accessor :indent, :offset, :formatting_options, :heading_styles, :option_styles
|
15
9
|
|
@@ -215,7 +209,7 @@ module Choosy::Printing
|
|
215
209
|
case item
|
216
210
|
when Choosy::Option
|
217
211
|
opt = regular_option(item)
|
218
|
-
len = opt.
|
212
|
+
len = unformatted(opt).length
|
219
213
|
if len > optionlen
|
220
214
|
optionlen = len
|
221
215
|
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'choosy/errors'
|
2
|
-
require 'choosy/printing/terminal'
|
3
2
|
require 'choosy/printing/base_printer'
|
4
3
|
|
5
4
|
module Choosy::Printing
|
@@ -88,7 +87,7 @@ module Choosy::Printing
|
|
88
87
|
end
|
89
88
|
|
90
89
|
def write_prefix(prefix, after_indent)
|
91
|
-
len = after_indent.length - prefix.
|
90
|
+
len = after_indent.length - unformatted(prefix).length - indent.length
|
92
91
|
@buffer << indent
|
93
92
|
@buffer << prefix
|
94
93
|
@buffer << " " * len
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'choosy/errors'
|
2
|
+
require 'choosy/printing/color'
|
3
|
+
|
4
|
+
module Choosy
|
5
|
+
module Terminal
|
6
|
+
DEFAULT_LINE_COUNT = 25
|
7
|
+
DEFAULT_COLUMN_COUNT = 80
|
8
|
+
|
9
|
+
attr_writer :lines, :columns
|
10
|
+
|
11
|
+
def lines
|
12
|
+
@lines ||= find_terminal_size('LINES', 'lines', 0) || DEFAULT_LINE_COUNT
|
13
|
+
end
|
14
|
+
|
15
|
+
def columns
|
16
|
+
@columns ||= find_terminal_size('COLUMNS', 'cols', 1) || DEFAULT_COLUMN_COUNT
|
17
|
+
end
|
18
|
+
|
19
|
+
def color
|
20
|
+
@color ||= Choosy::Printing::Color.new
|
21
|
+
end
|
22
|
+
|
23
|
+
def pager?
|
24
|
+
!pager.empty?
|
25
|
+
end
|
26
|
+
|
27
|
+
def pager
|
28
|
+
@pager ||= nil
|
29
|
+
return @pager if @pager
|
30
|
+
|
31
|
+
@pager ||= ENV['PAGER'] ||
|
32
|
+
ENV['MANPAGER'] ||
|
33
|
+
if command_exists?('less')
|
34
|
+
'less -R'
|
35
|
+
elsif command_exists?('more')
|
36
|
+
'more'
|
37
|
+
else
|
38
|
+
''
|
39
|
+
end
|
40
|
+
if @pager !~ /less -R/
|
41
|
+
color.disable!
|
42
|
+
end
|
43
|
+
@pager
|
44
|
+
end
|
45
|
+
|
46
|
+
def page(contents, pipe_command=nil)
|
47
|
+
if pager?
|
48
|
+
if pipe_command
|
49
|
+
pipe_out("#{pipe_command} | #{pager}", contents)
|
50
|
+
else
|
51
|
+
pipe_out(pager, contents)
|
52
|
+
end
|
53
|
+
else
|
54
|
+
pipe_out(pipe_command, contents)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def pipe_out(command, contents = nil, &block)
|
59
|
+
puts contents if command.nil? && contents
|
60
|
+
|
61
|
+
IO.popen(command, 'w') do |f|
|
62
|
+
f.puts contents if contents
|
63
|
+
if block_given?
|
64
|
+
yield f
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def pipe_in(command = nil, &block)
|
70
|
+
raise ArgumentError.new("Requires a block") unless block_given?
|
71
|
+
|
72
|
+
if command
|
73
|
+
IO.popen(command, 'r') do |f|
|
74
|
+
f.each_line do |line|
|
75
|
+
yield line
|
76
|
+
end
|
77
|
+
end
|
78
|
+
$?
|
79
|
+
elsif stdin?
|
80
|
+
STDIN.each_line do |line|
|
81
|
+
yield line
|
82
|
+
end
|
83
|
+
0
|
84
|
+
else
|
85
|
+
1
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# directly from hirb
|
90
|
+
def command_exists?(command)
|
91
|
+
ENV['PATH'].split(File::PATH_SEPARATOR).any? {|d| File.exists? File.join(d, command) }
|
92
|
+
end
|
93
|
+
|
94
|
+
def stdin?
|
95
|
+
begin
|
96
|
+
require 'fcntl'
|
97
|
+
STDIN.fcntl(Fcntl::F_GETFL, 0) == 0 && !stdin.tty?
|
98
|
+
rescue
|
99
|
+
$stdin.stat.size != 0
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def unformatted(line='')
|
104
|
+
line = line.gsub(/\e\[\d+m/, '')
|
105
|
+
line.gsub!(/\\f[IPB]/, '')
|
106
|
+
line
|
107
|
+
end
|
108
|
+
|
109
|
+
def die(message)
|
110
|
+
raise Choosy::ConfigurationError.new(message)
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
# https://github.com/cldwalker/hirb
|
115
|
+
# modified from hirb
|
116
|
+
def find_terminal_size(env_name, tput_name, stty_index)
|
117
|
+
begin
|
118
|
+
if ENV[env_name] =~ /^\d$/
|
119
|
+
ENV[env_name].to_i
|
120
|
+
elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && command_exists?('tput')
|
121
|
+
`tput #{tput_name}`.to_i
|
122
|
+
elsif STDIN.tty? && command_exists?('stty')
|
123
|
+
`stty size`.scan(/\d+/).map { |s| s.to_i }[stty_index]
|
124
|
+
else
|
125
|
+
nil
|
126
|
+
end
|
127
|
+
rescue
|
128
|
+
nil
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 4
|
8
|
-
-
|
9
|
-
version: 0.4.
|
8
|
+
- 5
|
9
|
+
version: 0.4.5
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Gabe McArthur
|
@@ -98,6 +98,7 @@ files:
|
|
98
98
|
- lib/choosy/version.rb
|
99
99
|
- lib/choosy/option.rb
|
100
100
|
- lib/choosy/command.rb
|
101
|
+
- lib/choosy/terminal.rb
|
101
102
|
- lib/choosy/super_command.rb
|
102
103
|
- lib/choosy/dsl/argument_builder.rb
|
103
104
|
- lib/choosy/dsl/super_command_builder.rb
|
@@ -111,7 +112,6 @@ files:
|
|
111
112
|
- lib/choosy/printing/base_printer.rb
|
112
113
|
- lib/choosy/printing/formatting_element.rb
|
113
114
|
- lib/choosy/printing/manpage.rb
|
114
|
-
- lib/choosy/printing/terminal.rb
|
115
115
|
- lib/choosy/printing/color.rb
|
116
116
|
- lib/choosy/printing/erb_printer.rb
|
117
117
|
- lib/choosy/printing/help_printer.rb
|
@@ -128,6 +128,7 @@ files:
|
|
128
128
|
- spec/choosy/argument_spec.rb
|
129
129
|
- spec/choosy/base_command_spec.rb
|
130
130
|
- spec/choosy/version.yml
|
131
|
+
- spec/choosy/terminal_spec.rb
|
131
132
|
- spec/choosy/command_spec.rb
|
132
133
|
- spec/choosy/dsl/super_command_builder_spec.rb
|
133
134
|
- spec/choosy/dsl/option_builder_spec.rb
|
@@ -139,7 +140,6 @@ files:
|
|
139
140
|
- spec/choosy/super_parser_spec.rb
|
140
141
|
- spec/choosy/verifier_spec.rb
|
141
142
|
- spec/choosy/printing/help_printer_spec.rb
|
142
|
-
- spec/choosy/printing/terminal_spec.rb
|
143
143
|
- spec/choosy/printing/base_printer_spec.rb
|
144
144
|
- spec/choosy/printing/manpage_printer_spec.rb
|
145
145
|
- spec/choosy/printing/color_spec.rb
|
@@ -1,84 +0,0 @@
|
|
1
|
-
require 'choosy/errors'
|
2
|
-
require 'choosy/printing/color'
|
3
|
-
|
4
|
-
module Choosy::Printing
|
5
|
-
module Terminal
|
6
|
-
DEFAULT_LINE_COUNT = 25
|
7
|
-
DEFAULT_COLUMN_COUNT = 80
|
8
|
-
|
9
|
-
attr_writer :lines, :columns
|
10
|
-
|
11
|
-
def lines
|
12
|
-
@lines ||= find_terminal_size('LINES', 'lines', 0) || DEFAULT_LINE_COUNT
|
13
|
-
end
|
14
|
-
|
15
|
-
def columns
|
16
|
-
@columns ||= find_terminal_size('COLUMNS', 'cols', 1) || DEFAULT_COLUMN_COUNT
|
17
|
-
end
|
18
|
-
|
19
|
-
def color
|
20
|
-
@color ||= Color.new
|
21
|
-
end
|
22
|
-
|
23
|
-
def pager?
|
24
|
-
!pager.empty?
|
25
|
-
end
|
26
|
-
|
27
|
-
def pager
|
28
|
-
@pager ||= ENV['PAGER'] || ENV['MANPAGER']
|
29
|
-
return @pager if @pager
|
30
|
-
|
31
|
-
@pager = if command_exists?('less')
|
32
|
-
'less -R'
|
33
|
-
elsif command_exists?('more')
|
34
|
-
'more'
|
35
|
-
else
|
36
|
-
''
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def page(contents, pipe_command=nil)
|
41
|
-
if pager?
|
42
|
-
if pipe_command
|
43
|
-
pipe(contents, "#{pipe_command} | #{pager}")
|
44
|
-
else
|
45
|
-
pipe(contents, pager)
|
46
|
-
end
|
47
|
-
else
|
48
|
-
pipe(contents, pipe_command)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def pipe(contents, command)
|
53
|
-
puts contents if command.nil?
|
54
|
-
|
55
|
-
IO.popen(command, 'w') do |f|
|
56
|
-
f.puts contents
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
# directly from hirb
|
61
|
-
def command_exists?(command)
|
62
|
-
ENV['PATH'].split(File::PATH_SEPARATOR).any? {|d| File.exists? File.join(d, command) }
|
63
|
-
end
|
64
|
-
|
65
|
-
private
|
66
|
-
# https://github.com/cldwalker/hirb
|
67
|
-
# modified from hirb
|
68
|
-
def find_terminal_size(env_name, tput_name, stty_index)
|
69
|
-
begin
|
70
|
-
if ENV[env_name] =~ /^\d$/
|
71
|
-
ENV[env_name].to_i
|
72
|
-
elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && command_exists?('tput')
|
73
|
-
`tput #{tput_name}`.to_i
|
74
|
-
elsif STDIN.tty? && command_exists?('stty')
|
75
|
-
`stty size`.scan(/\d+/).map { |s| s.to_i }[stty_index]
|
76
|
-
else
|
77
|
-
nil
|
78
|
-
end
|
79
|
-
rescue
|
80
|
-
nil
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|