Module: StoryTeller::IO

Included in:
Inform::Ephemeral::Object
Defined in:
lib/story_teller/io.rb,
lib/story_teller/session.rb

Overview

module IO

Defined Under Namespace

Classes: Connection, Session

Constant Summary collapse

ItemPaddingString =

Adapted from the C code for ls.  :-)

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

Instance Method Summary collapse

Instance Method Details

#connectionsObject



35
36
37
38
39
40
41
# File 'lib/story_teller/io.rb', line 35

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



66
67
68
# File 'lib/story_teller/io.rb', line 66

def flush_io
  $stdout.flush
end

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



55
56
57
58
59
60
61
62
63
# File 'lib/story_teller/io.rb', line 55

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



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/story_teller/io.rb', line 187

def many_per_line(items)
  items = items.map(&:to_s)
  return [] if items.empty?

  line_length = preferred_line_length
  max_item_length = items.map(&:length).max || 0
  cell_width = max_item_length + MinimumColumnPadding

  column_count = (line_length - MinimumCellPadding) / cell_width
  column_count = 1 if column_count < 1
  column_count = items.length if column_count > items.length

  row_count = (items.length.to_f / column_count).ceil
  rows = []

  row_count.times do |row_index|
    row = []

    column_count.times do |column_index|
      item_index = row_index + (column_index * row_count)
      item = items[item_index]
      next if item.nil?

      row << item.ljust(cell_width)
    end

    rows << row.join.rstrip
  end

  rows
end

#new_lineObject



76
77
78
# File 'lib/story_teller/io.rb', line 76

def new_line
  out "\n"
end

#noopObject



33
# File 'lib/story_teller/io.rb', line 33

def noop; end

#out(s) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/story_teller/io.rb', line 47

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

#output_stream(i, s) ⇒ Object



94
# File 'lib/story_teller/io.rb', line 94

def output_stream(i, s); end

#preference_value(key) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/story_teller/io.rb', line 104

def preference_value(key)
  prefs = session.preferences

  return prefs.public_send(key) if prefs.respond_to?(key)
  return prefs[key] if prefs.respond_to?(:[]) && prefs.key?(key)
  return prefs[key.to_s] if prefs.respond_to?(:[]) && prefs.key?(key.to_s)

  nil
end

#preferred_line_lengthObject



124
125
126
127
128
129
# File 'lib/story_teller/io.rb', line 124

def preferred_line_length
  width = preference_value(:word_wrap).to_i
  return width if width > MinimumLineLength

  DefaultLineLength
end


84
85
86
# File 'lib/story_teller/io.rb', line 84

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

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



88
89
90
91
92
# File 'lib/story_teller/io.rb', line 88

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



100
101
102
# File 'lib/story_teller/io.rb', line 100

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

#readObject



43
44
45
# File 'lib/story_teller/io.rb', line 43

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

#reset_promptObject



96
97
98
# File 'lib/story_teller/io.rb', line 96

def reset_prompt
  @prompt = nil
end

#sessionObject



26
27
28
29
30
31
# File 'lib/story_teller/io.rb', line 26

def session
  return @session if defined?(@session) && !@session.nil?
  return nil unless defined?(StoryTeller::IO::Session)

  StoryTeller::IO::Session.of(self)
end

#spaces(n) ⇒ Object



80
81
82
# File 'lib/story_teller/io.rb', line 80

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

#tidy_output(s, columns = 76) ⇒ Object



70
71
72
73
74
# File 'lib/story_teller/io.rb', line 70

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