Class: MonoclePrint::SingleLine

Inherits:
String
  • Object
show all
Includes:
MonoclePrint
Defined in:
lib/monocle-print/atomic.rb

Constant Summary collapse

@@width =
{}
@@invisible_size =
{}

Constants included from MonoclePrint

COLOR_ESCAPE, FOUR_BYTES, MULTIBYTE_CHARACTER, ONE_BYTE, THREE_BYTES, TWO_BYTES, VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MonoclePrint

Line, Output, Rectangle, Style, Text, buffer, included, library_path, stderr, stdout, version

Class Method Details

.clear_cacheObject



28
29
30
31
32
# File 'lib/monocle-print/atomic.rb', line 28

def self.clear_cache
  @@width.clear
  @@invisible_size.clear
  return( self )
end

Instance Method Details

#align(alignment, width, fill = ' ') ⇒ Object



81
82
83
# File 'lib/monocle-print/atomic.rb', line 81

def align( alignment, width, fill = ' ' )
  dup.align!( alignment, width, fill )
end

#align!(alignment, width, fill = ' ') ⇒ Object



85
86
87
88
89
90
91
# File 'lib/monocle-print/atomic.rb', line 85

def align!( alignment, width, fill = ' ' )
  case alignment.to_sym
  when :left then left!( width, fill )
  when :center then center!( width, fill )
  when :right then right!( width, fill )
  end
end

#blank?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/monocle-print/atomic.rb', line 93

def blank?
  empty? or self =~ /^(\s|#{COLOR_ESCAPE})*$/
end

#bleachObject



97
98
99
# File 'lib/monocle-print/atomic.rb', line 97

def bleach
  gsub( COLOR_ESCAPE, '' )
end

#bleach!Object



101
102
103
# File 'lib/monocle-print/atomic.rb', line 101

def bleach!
  gsub!( COLOR_ESCAPE, '' )
end

#center!(w, fill = ' ') ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/monocle-print/atomic.rb', line 105

def center!( w, fill = ' ' )
  w > width or return( self )
  if fill.length == 1
    replace( center( w + invisible_size, fill ) )
  else fill = self.class.new( fill )
    even, odd = ( width - w ).divmod( 2 )
    insert( 0, fill.tile( even ) )
    self << fill.tile( even + odd )
  end
  self
end

#char_byte(n) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/monocle-print/atomic.rb', line 39

def char_byte( n )
  n.zero? and return( 0 )
  seen = byte = 0
  while c = self[ byte ] and seen < n
    step =
      case c
      when ONE_BYTE then seen += 1; 1
      when ?\e
        self[ byte, size ] =~ /^#{COLOR_ESCAPE}/ ? $~.end(0) : 1
      when TWO_BYTES then seen += 1; 2
      when THREE_BYTES then seen += 1; 3
      when FOUR_BYTES then seen += 1; 4
      else 1
      end
    byte += step
  end
  return( byte )
end

#divide_at(len) ⇒ Object



117
118
119
120
# File 'lib/monocle-print/atomic.rb', line 117

def divide_at( len )
  pos = char_byte( len )
  return( [ self[ 0, pos ], self[ pos, size ] ] )
end

#each_escapeObject



122
123
124
125
126
127
# File 'lib/monocle-print/atomic.rb', line 122

def each_escape
  block_given? or return( enum_for( :each_escape ) )
  scan( COLOR_ESCAPE ) do |esc|
    yield( esc )
  end
end

#escapesObject



129
130
131
132
133
# File 'lib/monocle-print/atomic.rb', line 129

def escapes
  each_escape.inject( self.class.new ) do | escs, esc |
    escs << esc
  end
end

#heightObject



211
212
213
# File 'lib/monocle-print/atomic.rb', line 211

def height
  1
end

#indent(n) ⇒ Object



135
136
137
# File 'lib/monocle-print/atomic.rb', line 135

def indent( n )
  dup.indent!( n )
end

#indent!(num_spaces) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'lib/monocle-print/atomic.rb', line 139

def indent!( num_spaces )
  if num_spaces < 0
    remaining_indent = Utils.at_least( level_of_indent + num_spaces, 0 )
    lstrip!
    indent!( remaining_indent )
  else
    insert( 0, ' ' * num_spaces )
  end
  return( self )
end

#invisible_sizeObject



150
151
152
# File 'lib/monocle-print/atomic.rb', line 150

def invisible_size
  @@invisible_size[ hash ] ||= size - width
end

#left!(w, fill = ' ') ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/monocle-print/atomic.rb', line 154

def left!( w, fill = ' ' )
  w > width or return( self )
  if fill.length == 1
    replace( ljust( w + invisible_size, fill ) )
  else fill = self.class.new( fill )
    insert( 0, fill.tile( width - w ) )
  end
  self
end

#level_of_indentObject



164
165
166
# File 'lib/monocle-print/atomic.rb', line 164

def level_of_indent
  self =~ /^(\s+)/ ? $1.length : 0
end

#pad!(left, right = left) ⇒ Object



168
169
170
171
# File 'lib/monocle-print/atomic.rb', line 168

def pad!( left, right = left )
  right!( width + left )
  left!( width + right )
end

#partial(len) ⇒ Object



173
174
175
# File 'lib/monocle-print/atomic.rb', line 173

def partial(len)
  self[ 0, char_byte( len ) ]
end

#right!(w, fill = ' ') ⇒ Object



177
178
179
180
181
182
183
184
185
# File 'lib/monocle-print/atomic.rb', line 177

def right!( w, fill = ' ' )
  w > width or return( self )
  if fill.length == 1
    replace( rjust( w + invisible_size, fill ) )
  else fill = self.class.new( fill )
    self.insert( 0, fill.tile( w - width ) )
  end
  self
end

#tile(size) ⇒ Object



187
188
189
190
191
# File 'lib/monocle-print/atomic.rb', line 187

def tile( size )
  width == 0 and return( )
  full, partial = size.divmod( width )
  self * full << partial( partial )
end

#truncate(w, tail = nil) ⇒ Object



193
194
195
# File 'lib/monocle-print/atomic.rb', line 193

def truncate( w, tail = nil )
  dup.truncate!( w, tail )
end

#truncate!(w, tail = nil) ⇒ Object



197
198
199
200
201
202
203
204
205
# File 'lib/monocle-print/atomic.rb', line 197

def truncate!( w, tail = nil )
  width > w or return( self )
  if tail then tail = Line( tail )
    return( partial( w - tail.width ) << tail )
  else
    return( partial( w ) )
  end
  return( self )
end

#widthObject



58
59
60
61
62
63
# File 'lib/monocle-print/atomic.rb', line 58

def width
  @@width[ hash ] ||= begin
    (temp = bleach).gsub!( MULTIBYTE_CHARACTER, ' ' )
    temp.size
  end
end

#wordsObject



207
208
209
# File 'lib/monocle-print/atomic.rb', line 207

def words
  strip.split(/\s+/)
end

#wrap(w) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/monocle-print/atomic.rb', line 215

def wrap( w )
  if width > w
    words = split( /\s+/ ).inject( [] ) do | words, word |
      while word.width > w
        frag, word = word.divide_at( w )
        words << frag
      end
      words << word
    end

    line = words.shift || self.class.new
    text = Text.new
    w -= 1
    while word = words.shift
      if line.width + word.width > w
        text << line
        line = word
      else
        line << ' ' << word
      end
    end
    text << line
    return( text )
  else
    return( Text( self.dup ) )
  end
end