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

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.default_outputObject

Returns the value of attribute default_output.



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

def default_output
  @default_output
end

Instance Method Details

#connectionsObject



41
42
43
44
45
46
47
# File 'lib/story_teller/io.rb', line 41

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



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

def flush_io
  return if session

  $stdout.flush
end

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



61
62
63
64
65
66
67
68
69
# File 'lib/story_teller/io.rb', line 61

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



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

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



78
79
80
# File 'lib/story_teller/io.rb', line 78

def new_line
  out "\n"
end

#noopObject



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

def noop; end

#out(s) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/story_teller/io.rb', line 49

def out(s)
  current_session = session

  if current_session
    current_session.write_output(s)
  elsif StoryTeller::IO.default_output
    StoryTeller::IO.default_output.write(s)
  else
    $stdout.write(s)
  end
end

#output_stream(i, s) ⇒ Object



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

def output_stream(i, s); end

#preference_value(key) ⇒ Object



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

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



126
127
128
129
130
131
# File 'lib/story_teller/io.rb', line 126

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

  DefaultLineLength
end


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

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

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



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

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



102
103
104
# File 'lib/story_teller/io.rb', line 102

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

#reset_promptObject



98
99
100
# File 'lib/story_teller/io.rb', line 98

def reset_prompt
  @prompt = nil
end

#sessionObject



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

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



82
83
84
# File 'lib/story_teller/io.rb', line 82

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