Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
searls committed Jun 24, 2012
0 parents commit b81bd35
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
.DS_Store
.bundle
config/*.yml
.idea
3 changes: 3 additions & 0 deletions Gemfile
@@ -0,0 +1,3 @@
source "http://rubygems.org"

gemspec
2 changes: 2 additions & 0 deletions Rakefile
@@ -0,0 +1,2 @@
require 'bundler'
Bundler::GemHelper.install_tasks
22 changes: 22 additions & 0 deletions blight.gemspec
@@ -0,0 +1,22 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "blight/version"

Gem::Specification.new do |s|
s.name = 'blight'
s.version = Blight::VERSION
s.platform = Gem::Platform::RUBY
s.summary = "ncurses with less cursing"
s.description = "Blight provides a sane interface to the very procedural ncurses API"
s.authors = ["Justin Searls"]
s.email = 'searls@gmail.com'
s.homepage = 'https://github.com/searls/blight'

s.add_dependency "ncurses-ruby"


s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
end
119 changes: 119 additions & 0 deletions lib/blight.rb
@@ -0,0 +1,119 @@
require 'ncurses'

module Blight

class RunsLoop
def run(options = {})
config = default_options.merge(options)

begin
Ncurses.initscr
Ncurses.raw
Ncurses.start_color if config[:color]
config[:echo] ? Ncurses.echo : Ncurses.noecho

yield window = Window.new

while window.open?
key = Ncurses.stdscr.getch
config[:listeners].each { |l| l.listen({value: key.chr}, window) }
end

ensure
Ncurses.endwin
end
end

private

def default_options
{
color: true,
listeners: [ListensForQuitters.new],
echo: false
}
end

end

class Window

def initialize
@window = Ncurses.stdscr
end

def x
Ncurses.getcurx(@window)
end

def y
Ncurses.getcury(@window)
end

def width
Ncurses.getmaxx(@window)
end

def height
Ncurses.getmaxy(@window)
end

def print(string, x = x, y = y)
Ncurses.mvwprintw(@window, y, x, string)
refresh
end

def open?
!@closed
end

def close
@closed = true
end

def refresh
Ncurses.refresh
end

end

class RendersCenteredText
def initialize(window)
@window = window
end

def render(text)
center_x = @window.x + (@window.width - text.length) / 2
center_y = @window.height / 2
@window.print(text, center_x, center_y)
end
end

class UsesColors
def initialize(window)
@window = window
end

def use(foreground, background)
Ncurses.init_pair(1, find(foreground), find(background))
Ncurses.attron(Ncurses.COLOR_PAIR(1))
yield
Ncurses.attroff(Ncurses.COLOR_PAIR(1))
end

private

def find(color)
Ncurses.const_get("COLOR_#{color.to_s.upcase}")
end

end

class ListensForQuitters
def listen(event, window)
window.close if event[:value] == 'q'
end
end

end

3 changes: 3 additions & 0 deletions lib/blight/version.rb
@@ -0,0 +1,3 @@
module Blight
VERSION = "0.0.1"
end

0 comments on commit b81bd35

Please sign in to comment.