Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit

  • Loading branch information...
commit b81bd357682a651fbf50f48ee37391967c3891eb 0 parents
@searls authored
4 .gitignore
@@ -0,0 +1,4 @@
+.DS_Store
+.bundle
+config/*.yml
+.idea
3  Gemfile
@@ -0,0 +1,3 @@
+source "http://rubygems.org"
+
+gemspec
2  Rakefile
@@ -0,0 +1,2 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
22 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 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  lib/blight/version.rb
@@ -0,0 +1,3 @@
+module Blight
+ VERSION = "0.0.1"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.