Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b81bd35
Showing
6 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.DS_Store | ||
.bundle | ||
config/*.yml | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
source "http://rubygems.org" | ||
|
||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require 'bundler' | ||
Bundler::GemHelper.install_tasks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module Blight | ||
VERSION = "0.0.1" | ||
end |