Module: Inform::IO

Included in:
Object, Runtime, System::Object
Defined in:
lib/runtime/io.rb,
lib/runtime/session.rb

Overview

The IO module

Defined Under Namespace

Classes: Connection, Session

Constant Summary collapse

ItemPaddingString =

Adapted from the C code for ls.  :-)

'%<item>s%<padding>s'.freeze
MinimumLineLength =
30
MinimumCellPadding =
8
MinimumColumnPadding =
2

Instance Method Summary collapse

Instance Method Details

#connectionsObject



32
33
34
35
36
37
38
# File 'lib/runtime/io.rb', line 32

def connections
  subscribed_connections = explicit_subscribers.select { |a| a.is_a? Connection }
  if respond_to?(:inflib) && !inflib.nil? && subscribed_connections.empty?
    subscribed_connections = inflib.explicit_subscribers.select { |a| a.is_a? Connection }
  end
  subscribed_connections
end

#flush_ioObject



63
64
65
# File 'lib/runtime/io.rb', line 63

def flush_io
  $stdout.flush
end

#inform(s = '') ⇒ Object Also known as: update



52
53
54
55
56
57
58
59
60
# File 'lib/runtime/io.rb', line 52

def inform(s = '')
  s = invert_illeism(self, s) if respond_to?(:invert_illeism)
  new_line
  println s
  new_line if s.is_a?(Array) && !s.empty?
  prompt
  flush_io
  true
end

#many_per_line(items) ⇒ Object

rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/MethodLength rubocop: disable Metrics/PerceivedComplexity



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/runtime/io.rb', line 114

def many_per_line(items)
  max = 0

  lines = []
  prefs = @player.linkto :preferences
  line_length = prefs ? prefs.word_wrap.to_i : MinimumLineLength
  line_length = MinimumLineLength if line_length <= MinimumLineLength
  buffer = ''

  items.each do |item|
    max = [item.length, max].max
  end
  max += MinimumColumnPadding
  width = (line_length - MinimumCellPadding) / max
  height = (items.length / width).ceil - 1

  return if width == 0

  columns = []
  i = 0
  loop do
    # Build a list of columns with nil padding
    column = items[i, height]
    columns << column + Array.new(height - column.length)
    i += height
    break if (i + height) >= (items.length + height)
  end

  m = []
  loop do
    m << []
    for column in columns
      m.last << column.shift
    end
    break if columns.last.empty?
  end

  m.each do |row|
    buffer = "\e[51m"
    row.each do |item|
      next unless item
      n = [max - item.length, MinimumColumnPadding].max
      padding = " " * n unless item == row.last
      buffer += format(ItemPaddingString, item: item, padding: padding)
    end
    buffer += "\e[56m"
    lines << buffer.strip
    buffer = ''
  end

  lines
end

#new_lineObject



73
74
75
# File 'lib/runtime/io.rb', line 73

def new_line
  out "\n"
end

#noopObject



30
# File 'lib/runtime/io.rb', line 30

def noop; end

#out(s) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/runtime/io.rb', line 44

def out(s)
  if defined?(Curses)
    Curses.addstr s
  else
    $stdout.write(s)
  end
end

#output_stream(i, s) ⇒ Object



91
# File 'lib/runtime/io.rb', line 91

def output_stream(i, s); end


81
82
83
# File 'lib/runtime/io.rb', line 81

def print(s = '')
  out(s.to_s)
end

#println(s = '', isolate: false) ⇒ Object



85
86
87
88
89
# File 'lib/runtime/io.rb', line 85

def println(s = '', isolate: false)
  _isolated = (isolate == true) # TODO: Implement
  return print(s) if s.is_a?(Array)
  out(s.to_s + "\n")
end

#prompt(s = @prompt) ⇒ Object



97
98
99
# File 'lib/runtime/io.rb', line 97

def prompt(s = @prompt)
  out(@prompt = s || "\n>")
end

#readObject



40
41
42
# File 'lib/runtime/io.rb', line 40

def read
  $stdin.gets(chomp: true)
end

#reset_promptObject



93
94
95
# File 'lib/runtime/io.rb', line 93

def reset_prompt
  @prompt = nil
end

#sessionObject



25
26
27
28
# File 'lib/runtime/io.rb', line 25

def session
  return unless defined?(Inform::IO::Session) && Inform::IO::Session.respond_to?(:of)
  @session ||= Inform::IO::Session.of(self)
end

#spaces(n) ⇒ Object



77
78
79
# File 'lib/runtime/io.rb', line 77

def spaces(n)
  out(' ' * n)
end

#tidy_output(s, columns = 76) ⇒ Object



67
68
69
70
71
# File 'lib/runtime/io.rb', line 67

def tidy_output(s, columns = 76)
  s.gsub!(/^\n+/, "\n")
  return s if s.length < columns || columns == 0
  s.gsub(/(.{#{columns}}\S+)( +|$\n?)/, "\\1\n")
end