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, OutputBuffer, 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



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

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
69
70
# File 'lib/story_teller/io.rb', line 66

def flush_io
  return if session

  $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



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
# File 'lib/story_teller/io.rb', line 130

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



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

def new_line
  out "\n"
end

#noopObject



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

def noop; end

#out(s) ⇒ Object



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

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

#output_stream(i, s) ⇒ Object



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

def output_stream(i, s); end

#preference_value(key) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/story_teller/io.rb', line 100

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



120
121
122
123
124
125
# File 'lib/story_teller/io.rb', line 120

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

  DefaultLineLength
end


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

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

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



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

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



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

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

#reset_promptObject



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

def reset_prompt
  @prompt = nil
end

#sessionObject



26
27
28
29
30
31
32
33
# 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).tap do |session|
    log.trace "self: #{self}, session: #{session.inspect}"
  end
end

#spaces(n) ⇒ Object



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

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