Skip to content

Instantly share code, notes, and snippets.

@reinh
Created February 3, 2013 07:31
Show Gist options
  • Save reinh/4700848 to your computer and use it in GitHub Desktop.
Save reinh/4700848 to your computer and use it in GitHub Desktop.
module Roguish
module UI
class CursesUI
class Window < Curses::Window
def paint(contents)
clear
contents = contents.split("\n") if contents.respond_to?(:split)
contents.each_with_index do |line, i|
setpos(i, 0)
self << line.to_s
end
refresh
end
class Bordered
def initialize(options)
layout = options[:layout]
@outer = Window.new(*layout)
@inner = Window.new(*layout.inner)
draw_border
refresh
end
def paint(contents)
@inner.paint(contents)
end
def draw_border
@outer.clear
@outer.box 0, 0
end
def refresh
@outer.refresh
@inner.refresh
end
def close
@outer.close
@inner.close
end
def getch
@outer.getch
end
end
class Titled < Bordered
def initialize(options)
title = options.delete(:title)
super
set_title title
refresh
end
private
def set_title(title)
draw_border
if title
@outer.setpos(0, 1)
@outer << " #{title} "
end
end
end
class Messages
def initialize(options)
layout = options[:layout]
@message_list = options[:message_list]
@max_messages = layout.inner.height
@window = TitledWindow.new title: 'Messages:', layout: layout
paint
end
def paint
@window.paint @message_list.take(@max_messages)
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment