Class: Bbiff::Executable::LineIndicator

Inherits:
Object
  • Object
show all
Defined in:
lib/bbiff/executable.rb

Instance Method Summary collapse

Constructor Details

#initialize(out = STDOUT) ⇒ LineIndicator

Returns a new instance of LineIndicator.



10
11
12
13
14
# File 'lib/bbiff/executable.rb', line 10

def initialize(out = STDOUT)
  @width = 0
  @out = out
  @closed = false
end

Instance Method Details

#clearObject



40
41
42
43
44
45
# File 'lib/bbiff/executable.rb', line 40

def clear
  raise_if_closed!

  @out.print "\r#{' ' * @width}\r"
  @width = 0
end

#closeObject



60
61
62
63
64
65
66
67
# File 'lib/bbiff/executable.rb', line 60

def close
  if @closed
    raise 'already closed LineIndicator'
  else
    @out.puts if @width > 0
    @closed = true
  end
end

#newlineObject



33
34
35
36
37
38
# File 'lib/bbiff/executable.rb', line 33

def newline
  raise_if_closed!

  @out.print "\n"
  @width = 0
end

#puts(str) ⇒ Object



47
48
49
50
51
52
# File 'lib/bbiff/executable.rb', line 47

def puts(str)
  raise_if_closed!

  set_line(str)
  newline
end

#raise_if_closed!Object



54
55
56
57
58
# File 'lib/bbiff/executable.rb', line 54

def raise_if_closed!
  if @closed
    raise 'Closed LineIndicator'
  end
end

#set_line(str) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bbiff/executable.rb', line 16

def set_line(str)
  raise_if_closed!

  clear
  if str[-1] == "\n"
    if str.rindex("\n") != str.size-1 || str.index("\n") < str.rindex("\n")
      raise 'multiline' 
    end

    @out.print str
    @width = 0
  else
    @out.print str
    @width = mbswidth(str)
  end
end