Class: Naksh::BufferClass

Inherits:
Object
  • Object
show all
Defined in:
lib/naksh/buffer.rb

Overview

for internal buffers these should be replaced with a file named BufferClass for to prevent typing errors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inp = nil, out = nil, err = nil) ⇒ BufferClass

Returns a new instance of BufferClass.



30
31
32
33
34
35
36
37
# File 'lib/naksh/buffer.rb', line 30

def initialize(inp=nil,out=nil,err=nil)
  @in =inp
  @out=out
  @err=err
  @new=String.new
  @old=StringIO.new
  @silenced=false
end

Instance Attribute Details

#errObject

Returns the value of attribute err.



39
40
41
# File 'lib/naksh/buffer.rb', line 39

def err
  @err
end

#inObject

Returns the value of attribute in.



39
40
41
# File 'lib/naksh/buffer.rb', line 39

def in
  @in
end

#outObject

Returns the value of attribute out.



39
40
41
# File 'lib/naksh/buffer.rb', line 39

def out
  @out
end

Instance Method Details

#clearObject

wipe anything stored and call self.speak



105
106
107
108
109
# File 'lib/naksh/buffer.rb', line 105

def clear
  @old=StringIO.new
  @new=String.new
  speak
end

#fake_print(*a) ⇒ Object



65
66
67
68
# File 'lib/naksh/buffer.rb', line 65

def fake_print *a
  @new<<a.join('')
  nil
end

#fake_puts(*a) ⇒ Object Also known as: fake_pp



71
72
73
74
75
76
77
# File 'lib/naksh/buffer.rb', line 71

def fake_puts *a
  a.each do |i|
    @new<<i.to_s
    @new<<"\n" unless @new[-1]=="\n" or @new[-1].chr=="\n"
  end
  nil
end

#flushObject

this is useful if you don’t want incremental output, just a single final dump



122
123
124
# File 'lib/naksh/buffer.rb', line 122

def flush
  @out.print @old.read
end

#getsObject



127
128
129
# File 'lib/naksh/buffer.rb', line 127

def gets
  @old.gets
end

#nextObject

this tells the buffer you’re working on a new command, and anything you printed before should be getted now



138
139
140
141
# File 'lib/naksh/buffer.rb', line 138

def next
  @old=StringIO.new(@new)
  @new=String.new
end

#pp(*a) ⇒ Object Also known as: real_pp



41
42
43
44
45
46
# File 'lib/naksh/buffer.rb', line 41

def pp *a
  a.each do |i|
    @out.pp i.to_s
  end
  nil
end


48
49
50
51
52
53
# File 'lib/naksh/buffer.rb', line 48

def print *a
  a.each do |i|
    @out.print i.to_s
  end
  nil
end

#puts(*a) ⇒ Object Also known as: real_puts



56
57
58
59
60
61
62
# File 'lib/naksh/buffer.rb', line 56

def puts *a
  a.each do |i|
    @out.print i.to_s
    @out.print "\n" unless i[-1]=="\n" or i[-1].chr=="\n"
  end
  nil
end

#readlineObject

just like gets, but throws an error at end of file



132
133
134
# File 'lib/naksh/buffer.rb', line 132

def readline
  @old.readline
end

#silenceObject

see Buffer#silenced?



88
89
90
91
92
93
94
# File 'lib/naksh/buffer.rb', line 88

def silence
  @silenced=true
  self.class.class_eval('undef_method :print,:puts,:pp')
  self.class.class_eval('alias_method :pp,   :fake_pp')
  self.class.class_eval('alias_method :print,:fake_print')
  self.class.class_eval('alias_method :puts, :fake_puts')
end

#silenced?Boolean

if silenced? is true, Buffer#puts and Buffer#print are storing data for later use, and not sending it to Buffer#out

Returns:

  • (Boolean)


99
100
101
# File 'lib/naksh/buffer.rb', line 99

def silenced?
  @silenced
end

#speakObject



112
113
114
115
116
117
118
# File 'lib/naksh/buffer.rb', line 112

def speak
  @silenced=false
  self.class.class_eval('undef_method :print,:puts,:pp')
  self.class.class_eval('alias_method :print,:real_print')
  self.class.class_eval('alias_method :pp,   :real_pp')
  self.class.class_eval('alias_method :puts, :real_puts')
end