monocle-print 1.0.1 → 1.0.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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YWI2NjhiNjNlODdkN2NlMjcxZGNjNGVlNDI2OWViNTc3NmJjODBjZg==
4
+ ZTJhOTg2ZGRkZTUzMDYxOTAxMmNhMWI2ZTJjM2ViM2E0NTk4YzBmMQ==
5
5
  data.tar.gz: !binary |-
6
- ZDFkMWM1YTIyMTY2N2RhMmRlYjRkM2MzYjYzZjBlYzFkNjE3NWVjZA==
6
+ NmMyYWJmNzEyZDc1ZWZlODE5NDgwY2RiNjI1NDAzYjdjYzdhNWY1OA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YTRjOTJhMWE2N2FlNWRkMDZmNzI5MjcyYTI2ZDMxZWVjYTJhZmUxYzU1MjZm
10
- MTExMjk5OTllYjkyYWVkYTc3YTRjZGZlZGFiYjJmNTczMzNiNjFhNmNkMTg3
11
- ZGNiOWJiYWJjODUwZGRjNzVlMzk0ZTQ0OWZkYjJmOTdlYzQ3NmU=
9
+ NDE0ZDQwOTk5MTk1ZDVkYjM5NjJjZjg3OGFmZWVlMmRhMTFkOTcxMWNkNDgz
10
+ ODExMTQ5ODFmMzkxZTgxMTNlYmExODUzY2M1MWZlODE4MTlmNWY2NTViYzY5
11
+ ZmM3ZjZjNGZiZTU3YzI4ZmYwNTQzNDc0ODEwNGQ2ZDE5OTkyZjE=
12
12
  data.tar.gz: !binary |-
13
- MDhkN2ZmYTQxNTUxY2E2NmQ1ZmQ5ODYxNTUyODRkNmU4ZTZhY2Q1ZjY3ZWEz
14
- MjMwNTI0NGU0ZGQwMGQ2Yzk3MjdjZGE1YmNkOTdjYWYzMzkyMDRhNjUyMmFh
15
- MmIxOWZmZTg4YTU0YTk3YzdlZjA0ZjAxMTMwYThkODY3MDE3ZGY=
13
+ MGUyN2Q0NzkyN2I5ZWY0NjUzNzE2NWRlNDc3MTg4NGE2NDRiMDZjMjAzZDlm
14
+ YWZjM2Q2YzkyNWMzNWJiZDcyZmNjYzUyNTliMmJlY2U0ZmFiNzRjNDUwNjNk
15
+ YzRlNmJmMWQ0MGE5MjQ0NjM0ODk3MjRjODg0MzQ2ZWNkNzFmOGI=
@@ -40,6 +40,11 @@ class OutputDevice < DelegateClass( IO )
40
40
  block_given? ? yield( device ) : device
41
41
  end
42
42
 
43
+ def self.stderr( options = {} )
44
+ device = new( $stderr, options )
45
+ block_given? ? yield( device ) : device
46
+ end
47
+
43
48
  DEFAULT_SIZE = Pair.new( 80, 22 ).freeze
44
49
  IO_PRINT_METHODS = %w( puts print printf putc )
45
50
  SIZE_IOCTL = 0x5413
@@ -62,18 +67,20 @@ class OutputDevice < DelegateClass( IO )
62
67
  raise( ArgumentError, msg )
63
68
  end
64
69
  end
70
+
65
71
  super( @device )
66
72
 
67
- @cursor = Pair.new( 0, 0 )
68
73
  @background_stack = []
69
74
  @foreground_stack = []
70
- @background = @foreground = nil
71
- @tab_width = options.fetch( :tab_width, 8 )
75
+ @background = @foreground = nil
76
+ @use_color = options.fetch( :use_color, tty? )
72
77
 
73
- @newline = options.fetch( :newline, $/ )
74
- @use_color = options.fetch( :use_color, tty? )
78
+ @cursor = Pair.new( 0, 0 )
79
+ @tab_width = options.fetch( :tab_width, 8 )
80
+ @margin = Pair.new( 0, 0 )
81
+
82
+ @newline = options.fetch( :newline, $/ )
75
83
 
76
- @margin = Pair.new( 0, 0 )
77
84
  case margin = options.fetch( :margin, 0 )
78
85
  when Numeric then @margin.left = @margin.right = margin.to_i
79
86
  else
@@ -81,12 +88,12 @@ class OutputDevice < DelegateClass( IO )
81
88
  @margin.right = margin[ :right ].to_i
82
89
  end
83
90
 
84
- @tabs = {}
85
- @screen_size = nil
91
+ @tabs = {}
92
+ @screen_size = nil
86
93
  @forced_width = options[ :width ]
87
94
  @forced_height = options[ :height ]
88
- @alignment = options.fetch( :alignment, :left )
89
- @style = Style( options[ :style ] )
95
+ @alignment = options.fetch( :alignment, :left )
96
+ @style = Style( options[ :style ] )
90
97
  end
91
98
 
92
99
  def foreground( color = nil )
@@ -137,22 +144,22 @@ class OutputDevice < DelegateClass( IO )
137
144
  end
138
145
 
139
146
  def use_color= v
140
- @use_color = ! ! v # 'not not x' casts x to either `true' or 'false'
147
+ @use_color = !!v
141
148
  end
142
149
 
143
150
  def margin= n
144
- left_margin = right_margin = Utils.at_least( n.to_i, 0 )
151
+ self.left_margin = self.right_margin = Utils.at_least( n.to_i, 0 )
145
152
  end
146
153
 
147
154
  def indent( n = 0, m = 0 )
148
155
  n, m = n.to_i, m.to_i
149
- self.left_margin += n
156
+ self.left_margin += n
150
157
  self.right_margin += m
151
158
  if block_given?
152
159
  begin
153
160
  yield
154
161
  ensure
155
- self.left_margin -= n
162
+ self.left_margin -= n
156
163
  self.right_margin -= m
157
164
  end
158
165
  else
@@ -297,7 +304,7 @@ class OutputDevice < DelegateClass( IO )
297
304
  @device.print( str )
298
305
  @cursor + str.width
299
306
  @device.print( clear_attr )
300
- fill( @screen_size.width - @cursor.column )
307
+ fill( screen_size.width - @cursor.column )
301
308
  self
302
309
  end
303
310
 
@@ -406,34 +413,35 @@ private
406
413
  end
407
414
 
408
415
  def screen_size
409
- @screen_size ||= begin
410
- data = SIZE_STRUCT.dup
411
- if @device.ioctl( SIZE_IOCTL, data ) >= 0
412
- height, width = data.unpack( "SS" )
413
- @forced_width and width = @forced_width
414
- @forced_height and height = @forced_height
416
+ @screen_size ||=
417
+ begin
418
+ if device.respond_to?( :winsize )
419
+ detected_height, detected_width = device.winsize
420
+ elsif data = SIZE_STRUCT.dup and device.ioctl( SIZE_IOCTL, data ) >= 0
421
+ detected_height, detected_width = data.unpack( "SS" )
422
+ else
423
+ size = default_size
424
+ detected_height, detected_width = size.height, size.width
425
+ end
415
426
  Pair.new(
416
- width > 0 ? width : default_width,
417
- height > 0 ? height : default_height
427
+ @forced_height || detected_height,
428
+ @forced_width || detected_width
418
429
  )
419
- else
430
+ rescue Exception
420
431
  default_size
421
432
  end
422
- rescue Exception
423
- default_size
424
- end
425
433
  end
426
434
 
427
435
  def default_size
428
- Pair.new( @forced_width || default_width, @forced_height || default_height )
436
+ Pair.new( default_width, default_height )
429
437
  end
430
438
 
431
439
  def default_height
432
- ( ENV[ 'LINES' ] || DEFAULT_SIZE.height ).to_i
440
+ ( @forced_height || ENV[ 'LINES' ] || DEFAULT_SIZE.height ).to_i
433
441
  end
434
442
 
435
443
  def default_width
436
- ( ENV[ 'COLUMNS' ] || DEFAULT_SIZE.width ).to_i
444
+ ( @forced_width || ENV[ 'COLUMNS' ] || DEFAULT_SIZE.width ).to_i
437
445
  end
438
446
  end
439
447
 
@@ -471,20 +479,23 @@ class Pager < OutputDevice
471
479
  private
472
480
 
473
481
  def screen_size
474
- @screen_size ||= begin
475
- data = SIZE_STRUCT.dup
476
- if STDOUT.ioctl( SIZE_IOCTL, data ) >= 0
477
- height, width = data.unpack( "SS" )
482
+ @screen_size ||=
483
+ begin
484
+ if STDOUT.respond_to?( :winsize )
485
+ detected_height, detected_width = STDOUT.winsize
486
+ elsif data = SIZE_STRUCT.dup and STDOUT.ioctl( SIZE_IOCTL, data ) >= 0
487
+ detected_height, detected_width = data.unpack( "SS" )
488
+ else
489
+ size = default_size
490
+ detected_height, detected_width = size.height, size.width
491
+ end
478
492
  Pair.new(
479
- width > 0 ? width : default_width,
480
- height > 0 ? height : default_height
493
+ @forced_height || detected_height,
494
+ @forced_width || detected_width
481
495
  )
482
- else
496
+ rescue Exception
483
497
  default_size
484
498
  end
485
- rescue Exception
486
- default_size
487
- end
488
499
  end
489
500
  end
490
501
 
@@ -5,7 +5,7 @@ module MonoclePrint
5
5
  module Presentation
6
6
  include MonoclePrint
7
7
  ALIGNMENTS = [ :left, :right, :center ]
8
-
8
+
9
9
  def self.included( klass )
10
10
  super
11
11
  klass.extend( ClassMethods )
@@ -13,7 +13,7 @@ module Presentation
13
13
 
14
14
  attr_accessor :owner
15
15
  protected :owner=
16
-
16
+
17
17
  for attr in %w( margin padding border )
18
18
  class_eval( <<-END, __FILE__, __LINE__ + 1 )
19
19
  def #{ attr }( value = nil )
@@ -21,33 +21,33 @@ module Presentation
21
21
  @#{ attr } ||= default_#{ attr }
22
22
  block_given? ? yield( @#{ attr } ) : @#{ attr }
23
23
  end
24
-
24
+
25
25
  def #{ attr }= value
26
26
  @#{ attr } = Rectangle( value )
27
27
  end
28
28
  END
29
29
  end
30
-
30
+
31
31
  def alignment( value = nil )
32
32
  value and self.alignment = value
33
33
  @alignment or @owner ? @owner.alignment : :left
34
34
  end
35
-
35
+
36
36
  def alignment= value
37
37
  ALIGNMENTS.member?( value = value.to_sym ) or
38
38
  raise( ArgumentError, "unkown alignment: %p" % value )
39
39
  @alignment = value
40
40
  end
41
-
41
+
42
42
  def style( value = nil )
43
43
  value and self.style = value
44
44
  @style or @owner ? @owner.style : Graphics::NAMED_STYLES[ 'ascii' ]
45
45
  end
46
-
46
+
47
47
  def style= value
48
48
  @style = Style( value )
49
49
  end
50
-
50
+
51
51
  def render( output = @output )
52
52
  if output
53
53
  render_content( output )
@@ -58,66 +58,66 @@ module Presentation
58
58
  end
59
59
  end
60
60
  end
61
-
61
+
62
62
  def to_s
63
63
  OutputDevice.buffer do | out |
64
64
  render_content( out )
65
65
  end
66
66
  end
67
-
67
+
68
68
  def height
69
69
  @height or calculate_height
70
70
  end
71
-
71
+
72
72
  def width
73
73
  @width or calculate_width
74
74
  end
75
-
75
+
76
76
  attr_writer :max_width
77
-
77
+
78
78
  def max_width
79
79
  @max_width or @owner && @owner.max_width or output.width
80
80
  end
81
-
81
+
82
82
  def output
83
83
  @output ||= ( @owner and @owner.output or OutputDevice.stdout )
84
84
  end
85
-
85
+
86
86
  def output=( io )
87
87
  @output = io.nil? ? io : Output( io )
88
88
  end
89
-
89
+
90
90
  private
91
-
91
+
92
92
  def initialize_view( options = nil, owner = nil )
93
93
  @max_width = @width = @height = nil
94
94
  @margin = @padding = @alignment = @style = nil
95
95
  @output = @foreground = @background = nil
96
-
96
+
97
97
  if options
98
- val = options[ :width ] and self.width = val
98
+ val = options[ :width ] and self.width = val
99
99
  val = options[ :align ] and self.alignment = val
100
- val = options[ :style ] and self.style = val
101
- val = options[ :padding ] and self.padding = val
102
- val = options[ :margin ] and self.margin = val
103
- val = options[ :output ] and self.output = val
100
+ val = options[ :style ] and self.style = val
101
+ val = options[ :padding ] and self.padding = val
102
+ val = options[ :margin ] and self.margin = val
103
+ val = options[ :output ] and self.output = val
104
104
  end
105
-
105
+
106
106
  @owner = owner
107
107
  end
108
-
108
+
109
109
  def default_margin
110
110
  Rectangle.new( 0, 0, 0, 0 )
111
111
  end
112
-
112
+
113
113
  def default_padding
114
114
  Rectangle.new( 0, 0, 0, 0 )
115
115
  end
116
-
116
+
117
117
  def default_border
118
118
  Rectangle.new( false, false, false, false )
119
119
  end
120
-
120
+
121
121
  end
122
122
 
123
123
  module Presentation::ClassMethods
data/lib/monocle-print.rb CHANGED
@@ -1,8 +1,14 @@
1
1
  #!/usr/bin/ruby
2
2
  # encoding: utf-8
3
3
 
4
+ begin
5
+ require 'io/console'
6
+ rescue LoadError
7
+ # ignore
8
+ end
9
+
4
10
  module MonoclePrint
5
- VERSION = '1.0.1'
11
+ VERSION = '1.0.2'
6
12
 
7
13
  def self.version
8
14
  VERSION
@@ -17,8 +23,16 @@ module MonoclePrint
17
23
  File.join( File.dirname( __FILE__ ), *args )
18
24
  end
19
25
 
20
- def self.stdout
21
- Output( $stdout )
26
+ def self.stdout( options = {}, &block )
27
+ OutputDevice.stdout( options, &block )
28
+ end
29
+
30
+ def self.stderr( options = {}, &block )
31
+ OutputDevice.stderr( options, &block )
32
+ end
33
+
34
+ def self.buffer( options = {}, &block )
35
+ OutputDevice.buffer( options, &block )
22
36
  end
23
37
 
24
38
  module_function
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monocle-print
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - monocle-print
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-21 00:00:00.000000000 Z
11
+ date: 2013-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rdoc