Class: Text

Inherits:
Object show all
Defined in:
lib/text.rb

Overview

Copyright

Copyright © 2005 Nicolas Pouillard. All rights reserved.

Author

Nicolas Pouillard <[email protected]>.

License

Gnu General Public License.

Revision

$Id: /w/fey/ruby_ex/trunk/lib/text.rb 7964 2005-09-16T10:05:22.743435Z ertai $

Constant Summary collapse

@@default_options =
{ :width => 78, :cut_if_needed => false }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, options = {}) ⇒ Text

options:

width:         the maximal line width (default 78)
cut_if_needed: see cropping


15
16
17
18
19
20
# File 'lib/text.rb', line 15

def initialize ( text, options={} )
  options = @@default_options.merge options
  @text = text
  @width = options[:width]
  @cut_if_needed = options[:cut_if_needed]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Redirect calls like ‘justify’ to ‘dup.justify!’



153
154
155
156
157
# File 'lib/text.rb', line 153

def method_missing ( meth, *args, &block )
  in_place_meth = :"#{meth}!"
  return super unless respond_to? in_place_meth
  dup.send(in_place_meth, *args, &block)
end

Instance Attribute Details

#cut_if_neededObject

Returns the value of attribute cut_if_needed.



8
9
10
# File 'lib/text.rb', line 8

def cut_if_needed
  @cut_if_needed
end

#textObject

Returns the value of attribute text.



8
9
10
# File 'lib/text.rb', line 8

def text
  @text
end

#widthObject

Returns the value of attribute width.



8
9
10
# File 'lib/text.rb', line 8

def width
  @width
end

Instance Method Details

#adder(*args) ⇒ Object



126
127
128
# File 'lib/text.rb', line 126

def adder ( *args )
  @text += args.join
end

#clearObject



137
138
139
# File 'lib/text.rb', line 137

def clear
  @text.clear
end

#clip!(head_size = 100, tail_size = 30, message = "\n[...clipped...]\n\n", &block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/text.rb', line 112

def clip! ( head_size=100, tail_size=30, message="\n[...clipped...]\n\n", &block )
  input = @text.to_a
  size = input.size
  return identity!(&block) if head_size + tail_size >= size
  head = (head_size - 1 < 0)? [] : input[0 .. head_size - 1]
  tail = input[[0, size - tail_size, head_size].max .. -1]
  @text, block = '', method(:adder) if block.nil?
  head.each(&block)
  message.each(&block)
  tail.each(&block)
  self
end

#crop!(width = @width, message = " ...\n", &block) ⇒ Object

Raises:

  • (ArgumentError)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/text.rb', line 95

def crop! ( width=@width, message=" ...\n", &block )
  input = @text
  @text, block = '', method(:adder) if block.nil?
  message = message.to_s
  width2 = width - 1 - message.chomp.width
  raise ArgumentError, "Bad width: too low" if width2 < 0
  input.each do |line|
    if line.width > width
      block[line[0..width2], message]
    else
      block[line]
    end
  end
  self
end

#identity!(&block) ⇒ Object



131
132
133
134
# File 'lib/text.rb', line 131

def identity! ( &block )
  @text.each(&block) unless block.nil?
  self
end

#justify!(&block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/text.rb', line 24

def justify! ( &block )
  input = @text.to_a
  @text, block = '', method(:adder) if block.nil?
  last_line = input.size - 1
  input.each_with_index do |line, i|
    line =~ /^(\s*)(.*)\s*?(\n?)$/
    indent, base, eol = $1, $2, $3
    words = base.split(/\s+/)
    base = words.join(' ')
    width = indent.width + base.width
    unless width > @width or base.empty? or i == last_line
      spaces = words.size - 1
      padding_size = @width - width
      if spaces > 0
        nb = ' ' * (padding_size / spaces + 1)
        nb2 = nb + ' '
        rest = padding_size % spaces + 1
        base.gsub!(/ /) do
          rest -= 1
          (rest >= 0) ? nb2 : nb
        end
      end
    end
    block[indent, base, eol]
  end
  self
end

#mask!Object



142
143
144
# File 'lib/text.rb', line 142

def mask!
  @text = '*****'.to_text
end

#split!(&block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/text.rb', line 54

def split! ( &block )
  last_new_line = (@text[-1] == ?\n)? "\n" : ''
  indent = @text[/\A([ \t]*)/, 1]
  words = @text.split(/\s+/)
  @text, block = '', method(:adder) if block.nil?

  line = ''
  while not words.empty?
    words.shift if words.first.empty?
    word = words.first
    if indent.width + line.width + word.width + 1 <= @width
      line += ' ' unless line.empty?
      line += word
      words.shift
    elsif line.empty?
      if @cut_if_needed
        line = word[0..width-1]
        words[0] = word[width..-1]
        block[indent, line, "\n"]
        line = ''
      else
        block[indent, word, "\n"]
        words.shift
      end
    else
      block[indent, line, "\n"]
      line = ''
    end
  end

  block[indent, line, last_new_line] unless line.empty?
  self
end

#split_and_justify!(&block) ⇒ Object



90
91
92
# File 'lib/text.rb', line 90

def split_and_justify! ( &block )
  split!.justify!(&block)
end

#to_sObject



147
148
149
# File 'lib/text.rb', line 147

def to_s
  @text.dup
end