Class: Muby::OutputWindow

Inherits:
Object
  • Object
show all
Includes:
Configurable, Displayer, HelperMethods, Logger
Defined in:
lib/muby/outputwindow.rb

Overview

The class that controls what we see.

Constant Summary

Constants included from Styled

Styled::ALTCHARSET, Styled::BLACK, Styled::BLINK, Styled::BLUE, Styled::BOLD, Styled::CHARTEXT, Styled::CYAN, Styled::DIM, Styled::GREEN, Styled::INVIS, Styled::MAGENTA, Styled::NORMAL, Styled::PROTECT, Styled::RED, Styled::REVERSE, Styled::STANDOUT, Styled::UNDERLINE, Styled::WHITE, Styled::YELLOW

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Displayer

debug, error, exception, info, trace, warn

Methods included from HelperMethods

#do_execute, #execute, #execute_with_verbosity

Methods included from Configurable

#conf

Methods included from Logger

#log_input, #log_output

Constructor Details

#initializeOutputWindow

Returns a new instance of OutputWindow.



19
20
21
# File 'lib/muby/outputwindow.rb', line 19

def initialize
  @ready = false
end

Instance Attribute Details

#outputBufferObject (readonly)

Returns the value of attribute outputBuffer.



11
12
13
# File 'lib/muby/outputwindow.rb', line 11

def outputBuffer
  @outputBuffer
end

#scrollbackObject (readonly)

Returns the value of attribute scrollback.



11
12
13
# File 'lib/muby/outputwindow.rb', line 11

def scrollback
  @scrollback
end

#sizeObject (readonly)

Returns the value of attribute size.



11
12
13
# File 'lib/muby/outputwindow.rb', line 11

def size
  @size
end

Class Method Details

.get_instanceObject



15
16
17
# File 'lib/muby/outputwindow.rb', line 15

def self.get_instance
  @@instance ||= Muby::OutputWindow.new
end

Instance Method Details

#goObject

Start working!



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
# File 'lib/muby/outputwindow.rb', line 30

def go
  # Ensure our size is calculable
  begin
    height
    width
    top
    left
  rescue Exception => e
    Ncurses.endwin
    raise "Unable to calculate output window geometry: #{e}\n\nCheck your config file (#{conf.loaded_rc_file}) for errors in 'conf.output_window_geometry!\n\n"
  end

  @scrollback = 0
  @waitBuffer = []
  @lines = 0

  @outputBuffer = Ncurses.newpad(conf.output_buffer, width)
  @outputBuffer.wclear
  @outputBuffer.scrollok(true)
  @outputBuffer.idlok(true)
  defaultAttributes = Muby::Style.new(conf.default_attributes, conf.default_colors[0], conf.default_colors[1])
  defaultAttributes.affect(@outputBuffer)
  # Prepare the output window:
  @outputBuffer.mvprintw(conf.output_buffer - 1, 0, "\n")

  show_version
  refresh
  
  @ready = true
end

#heightObject



77
78
79
# File 'lib/muby/outputwindow.rb', line 77

def height
  do_execute(conf.output_window_geometry[:height])
end

#leftObject



65
66
67
# File 'lib/muby/outputwindow.rb', line 65

def left
  do_execute(conf.output_window_geometry[:left])
end

Print an info Array to the outputBuffer.

Will print text as is, and add any Styles encountered to the outputBuffer.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/muby/outputwindow.rb', line 169

def print(*info)
  if @scrollback == 0
    info.each do |e|
      if String === e
        rest = e
        while (match = rest.match(/^([^\r\n]*)([\r\n]*)(.*)$/m))
          to_print = match[1]
          linebreak = match[2]
          rest = match[3]
          
          break if to_print.empty? and linebreak.empty?

          @outputBuffer.printw("%s", timestamp(to_print) + linebreak)

          @recent_linebreak = !linebreak.empty?
          @lines += 1 unless linebreak.empty?
        end
      elsif Muby::Style === e
        e.affect(@outputBuffer)
      end
    end
  else
    @waitBuffer += info
  end
  refresh
  nil
end


156
157
158
159
160
161
162
# File 'lib/muby/outputwindow.rb', line 156

def print_on_newline(*info)
  if @recent_linebreak
    print(*info)
  else
    print(*(["\n"] + info))
  end
end

#ready?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/muby/outputwindow.rb', line 23

def ready?
  @ready
end

#refreshObject

Refresh the output buffer (if we have scrolled etc)



97
98
99
# File 'lib/muby/outputwindow.rb', line 97

def refresh
  @outputBuffer.prefresh(conf.output_buffer - (height * (@scrollback + 1)), 0, top, left, height - 1, width)
end

#resize!Object



61
62
63
# File 'lib/muby/outputwindow.rb', line 61

def resize!
  refresh
end

#scroll_down(inputWindow) ⇒ Object

Scroll down again.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/muby/outputwindow.rb', line 118

def scroll_down(inputWindow)
  if @scrollback > 0
    @scrollback -= 1
    if @scrollback == 0
      # BUG:
      # No
      inputWindow.set_message_line("")
      unless @waitBuffer.empty?
        print(*@waitBuffer)
        @waitBuffer = []
      end
    end
  end
  refresh
end

#scroll_up(inputWindow) ⇒ Object

Scroll up, and give the user a message about it.



104
105
106
107
108
109
110
111
112
113
# File 'lib/muby/outputwindow.rb', line 104

def scroll_up(inputWindow)
  if @scrollback * height < conf.output_buffer &&
      @scrollback * height < @lines
    @scrollback += 1
    # BUG: set_message_line is in userMethods.
    # No, its not a bug.
    inputWindow.set_message_line("SCROLLED - I/O PAUSED")
    refresh
  end
end

#show(level, message) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/muby/outputwindow.rb', line 142

def show(level, message)
  if conf.display?(level)
    to_print = [message, "\n"]
    if level_attributes = conf.send("#{level}_attributes")
      level_colors = conf.send("#{level}_colors")
      style = Muby::Style.new(level_attributes, level_colors[0], level_colors[1])
      oldStyle = Muby::Style.extract(@outputBuffer)
      to_print = [style] + to_print + [oldStyle]
    end

    print_on_newline(*to_print)
  end
end

#show_versionObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/muby/outputwindow.rb', line 81

def show_version
  print("
.--.--.--.     .--.
:        :--.--:  : .--.--.
:  :  :  :  :  :  '-:  :  :
:  :  :  :  :  :  : :  '  :
'--'--'--'-----'----'--.  :
The Ruby MUD Client  :  :
-------------------  '--'
   Version #{Muby::VERSION}
")
end

#timestamp(message) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/muby/outputwindow.rb', line 134

def timestamp(message)
  if conf.timestamp && @recent_linebreak
    Time.new.strftime(conf.timeformat) + "\t" + message
  else
    message
  end
end

#topObject



69
70
71
# File 'lib/muby/outputwindow.rb', line 69

def top
  do_execute(conf.output_window_geometry[:top])
end

#widthObject



73
74
75
# File 'lib/muby/outputwindow.rb', line 73

def width
  do_execute(conf.output_window_geometry[:width])
end