Class: InOut

Inherits:
Object
  • Object
show all
Defined in:
lib/rust/helpers/io.rb

Overview

Created by me on 2011-12-13.

Copyright (c) 2011. All pwnage reserved.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdin = STDIN, stdout = STDOUT, stderr = STDERR, keys = {}) ⇒ InOut

Returns a new instance of InOut.



9
10
11
12
13
14
# File 'lib/rust/helpers/io.rb', line 9

def initialize(stdin=STDIN, stdout=STDOUT, stderr=STDERR, keys={})
  @in = stdin
  @out = stdout
  @err = stderr
  @keys = keys
end

Instance Attribute Details

#backspace_limitObject

Returns the value of attribute backspace_limit.



7
8
9
# File 'lib/rust/helpers/io.rb', line 7

def backspace_limit
  @backspace_limit
end

#lineObject

Returns the value of attribute line.



7
8
9
# File 'lib/rust/helpers/io.rb', line 7

def line
  @line
end

Instance Method Details

#ask(something) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rust/helpers/io.rb', line 28

def ask(something)
  @line = ''
  @backspace_limit = 0
  tell something

  while character = get_character()
    # check for special keystrokes
    (@keys[character].call; next) if @keys[character]
    # honor backspace and delete
    if character == 127 or character == 8
      @line.slice!(-1, 1)
      @backspace_limit -= 1
      
      # Respect tab
#      elsif character == 9
      
    else
      @line << character.chr
      @backspace_limit = @line.size
    end
    # looking for carriage return (decimal 13) or
    # newline (decimal 10) in raw input
    break if character == 13 or character == 10
    if character == 127 or character == 8 
      # only backspace if we have characters on the line to
      # eliminate, otherwise we'll tromp over the prompt
      if @backspace_limit >= 0 then
        @out.print("\b#{ERASE_CHAR}")
      else 
        # do nothing
      end
    else
      @out.print(character.chr)
    end
    @out.flush
  end
  @out.print "\n"
  return @line
end

#err(txt, exception = nil) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/rust/helpers/io.rb', line 68

def err(txt, exception=nil)
  if exception.nil?
    @out.puts("Unexpected error - #{txt}")
  else
    @out.puts("#{txt}\n#{exception.class}: #{exception.message}\n#{exception.backtrace.map {|l| "  #{l}\n"}}")
  end
  @out.flush
end

#say(text) ⇒ Object

Stolen from HighLine (by James Edward Grey II and Greg Brown)



23
24
25
26
# File 'lib/rust/helpers/io.rb', line 23

def say(text)
  @out.puts(text)
  @out.flush
end

#silenceObject

Silence him!



79
80
81
82
83
# File 'lib/rust/helpers/io.rb', line 79

def silence
  @old_in, @old_out, @old_err = @in, @out, @err
  @in, @out, @err = StringIO.new, StringIO.new, StringIO.new
  @silenced = true
end

#silenced?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/rust/helpers/io.rb', line 85

def silenced?()
  @silenced
end

#speakObject

It’s cool. He can talk now and we’ll set the old stuff to DEAD



92
93
94
95
96
# File 'lib/rust/helpers/io.rb', line 92

def speak
  @silenced = false 
  @in, @out, @err = @old_in, @old_out, @old_err
  [@old_in, @old_out, @old_err].each {|var| }
end

#tell(text) ⇒ Object



16
17
18
19
# File 'lib/rust/helpers/io.rb', line 16

def tell(text)
  @out.print(text)
  @out.flush
end