rurses 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0eb3e03695e4454190b4a2d7803dae2adbb61e35
4
+ data.tar.gz: 80cdf3890c1ea5611f81112c1fc34f3378308a65
5
+ SHA512:
6
+ metadata.gz: 89635579ca42b61cb372a41db098f387eb1cf8f90fcc4e8935a59cae181289ac2c173e048eb1d16a42ca2d7763a0c758167c7230f8d8a867b07cb23fa8a65323
7
+ data.tar.gz: 8b9c05b5bfc8ca8cbea1d07db552f4c7e1e52b84eac9e213ba0b27daea64e3a4f910bbc6ea8157410f878235d1eb1b25b3d3da7f7b8b26a8fe57dc8b033c8f8d
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rurses.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 James Edward Gray II
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,51 @@
1
+ # Rurses
2
+
3
+ Rurses (pronounced like [Scooby-Doo](http://en.wikipedia.org/wiki/Scooby-Doo_%28character%29) saying "Curses") is meant to be a more Ruby-like interface to ncurses.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rurses'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rurses
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ Rurses.program(modes: %i[c_break no_echo keypad]) do |screen|
25
+ screen.draw_string("Enter a key: ")
26
+
27
+ key = Rurses.get_key
28
+
29
+ screen.move_cursor(x: 0, y: 1)
30
+ screen.style(:bold) do
31
+ screen.draw_string("You typed %p." % key)
32
+ end
33
+
34
+ screen.refresh_in_memory
35
+ Rurses.update_screen
36
+ end
37
+ ```
38
+
39
+ ## Development
40
+
41
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
42
+
43
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it ( https://github.com/[my-github-username]/rurses/fork )
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rurses"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,50 @@
1
+ require "ffi-ncurses"
2
+
3
+ require_relative "rurses/version"
4
+ require_relative "rurses/panel_stack"
5
+ require_relative "rurses/window"
6
+
7
+ module Rurses
8
+ SPECIAL_KEYS = Hash[
9
+ FFI::NCurses::KeyDefs
10
+ .constants
11
+ .select { |name| name.to_s.start_with?("KEY_") }
12
+ .map { |name|
13
+ [ FFI::NCurses::KeyDefs.const_get(name),
14
+ name.to_s.sub(/\AKEY_/, "").to_sym ]
15
+ }
16
+ ]
17
+
18
+ module_function
19
+
20
+ def curses
21
+ FFI::NCurses
22
+ end
23
+
24
+ def program(modes: [ ])
25
+ @stdscr = Window.new(curses_ref: curses.initscr, standard_screen: true)
26
+ @stdscr.change_modes(modes)
27
+ yield(@stdscr)
28
+ ensure
29
+ curses.endwin
30
+ end
31
+
32
+ def stdscr
33
+ @stdscr
34
+ end
35
+
36
+ def get_key
37
+ case (char = curses.getch)
38
+ when curses::KeyDefs::KEY_CODE_YES..curses::KeyDefs::KEY_MAX
39
+ SPECIAL_KEYS[char]
40
+ when curses::ERR
41
+ nil
42
+ else
43
+ char.chr
44
+ end
45
+ end
46
+
47
+ def update_screen
48
+ curses.doupdate
49
+ end
50
+ end
@@ -0,0 +1,35 @@
1
+ module Rurses
2
+ class PanelStack
3
+ def initialize
4
+ @window_to_panel_map = { }
5
+ end
6
+
7
+ attr_reader :window_to_panel_map
8
+ private :window_to_panel_map
9
+
10
+ def add(window, add_subwindows: true)
11
+ window_to_panel_map[window] = Rurses.curses.new_panel(window.curses_ref)
12
+ if add_subwindows
13
+ window.subwindows.each_value do |subwindow|
14
+ add(subwindow, add_subwindows: add_subwindows)
15
+ end
16
+ end
17
+ end
18
+ alias_method :<<, :add
19
+
20
+ def remove(window, remove_subwindows: true)
21
+ if remove_subwindows
22
+ window.subwindows.each_value do |subwindow|
23
+ remove(subwindow, remove_subwindows: remove_subwindows)
24
+ end
25
+ end
26
+ window.clear
27
+ Rurses.curses.del_panel(window_to_panel_map[window])
28
+ Rurses.curses.delwin(window.curses_ref)
29
+ end
30
+
31
+ def refresh_in_memory
32
+ Rurses.curses.update_panels
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module Rurses
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,148 @@
1
+ module Rurses
2
+ class Window
3
+ MODE_NAMES = {
4
+ c_break: :cbreak,
5
+ no_echo: :noecho,
6
+ keypad: [:keypad, :window, true],
7
+ hide_cursor: [:curs_set, 0],
8
+ non_blocking: [:timeout, 0]
9
+ }
10
+ ATTRIBUTES = Hash.new { |all, name|
11
+ all[name] = Rurses.curses.const_get("A_#{name.upcase}")
12
+ }
13
+
14
+ def initialize(**details)
15
+ @curses_ref = details.fetch(:curses_ref) {
16
+ Rurses.curses.newwin(
17
+ details.fetch(:lines),
18
+ details.fetch(:columns),
19
+ details.fetch(:y),
20
+ details.fetch(:x)
21
+ )
22
+ }
23
+ @standard_screen = details.fetch(:standard_screen) { false }
24
+ @subwindows = { }
25
+ end
26
+
27
+ attr_reader :curses_ref, :subwindows
28
+
29
+ def standard_screen?
30
+ @standard_screen
31
+ end
32
+
33
+ def cursor_x
34
+ Rurses.curses.getcurx(curses_ref)
35
+ end
36
+
37
+ def cursor_y
38
+ Rurses.curses.getcury(curses_ref)
39
+ end
40
+
41
+ def cursor_xy
42
+ y, x = Rurses.curses.getyx(curses_ref)
43
+ {x: x, y: y}
44
+ end
45
+
46
+ def columns
47
+ Rurses.curses.getmaxx(curses_ref)
48
+ end
49
+
50
+ def lines
51
+ Rurses.curses.getmaxy(curses_ref)
52
+ end
53
+
54
+ def size
55
+ {columns: columns, lines: lines}
56
+ end
57
+
58
+ def resize(lines: , columns: )
59
+ Rurses.curses.resizeterm(lines, columns) if standard_screen?
60
+ Rurses.curses.wresize(curses_ref, lines, columns)
61
+ end
62
+
63
+ def change_modes(modes)
64
+ modes.each do |name|
65
+ mode = Array(MODE_NAMES[name] || name)
66
+ Rurses.curses.send(*mode.map { |arg| arg == :window ? curses_ref : arg })
67
+ end
68
+ end
69
+
70
+ def draw_border( left: 0, right: 0, top: 0, bottom: 0,
71
+ top_left: 0, top_right: 0, bottom_left: 0, bottom_right: 0 )
72
+ args = [
73
+ left, right, top, bottom,
74
+ top_left, top_right, bottom_left, bottom_right
75
+ ].map { |c| c.is_a?(String) ? c.encode("UTF-8").codepoints.first : c }
76
+ if args.any? { |c| c >= 128 }
77
+ Rurses.curses.wborder_set(
78
+ curses_ref,
79
+ *args.map { |c|
80
+ char = Rurses.curses::WinStruct::CCharT.new
81
+ char[:chars][0] = c
82
+ }
83
+ )
84
+ else
85
+ Rurses.curses.wborder(curses_ref, *args)
86
+ end
87
+ end
88
+
89
+ def move_cursor(x: , y: )
90
+ Rurses.curses.wmove(curses_ref, y, x)
91
+ end
92
+
93
+ def draw_string(content)
94
+ Rurses.curses.waddstr(curses_ref, content)
95
+ end
96
+
97
+ def draw_string_on_a_line(content)
98
+ old_y = cursor_y
99
+ draw_string(content)
100
+ new_y = cursor_y
101
+ move_cursor(x: 0, y: new_y + 1) if new_y == old_y
102
+ end
103
+
104
+ def skip_line
105
+ move_cursor(x: 0, y: cursor_y + 1)
106
+ end
107
+
108
+ def style(*attributes)
109
+ attributes.each do |attribute|
110
+ Rurses.curses.wattron(curses_ref, ATTRIBUTES[attribute])
111
+ end
112
+ yield
113
+ ensure
114
+ attributes.each do |attribute|
115
+ Rurses.curses.wattroff(curses_ref, ATTRIBUTES[attribute])
116
+ end
117
+ end
118
+
119
+ def clear(reset_cursor: true)
120
+ Rurses.curses.wclear(curses_ref)
121
+ move_cursor(x: 0, y: 0) if reset_cursor
122
+ end
123
+
124
+ def refresh_in_memory
125
+ Rurses.curses.wnoutrefresh(curses_ref)
126
+ end
127
+
128
+ def create_subwindow( name: , top_padding: 0, left_padding: 0,
129
+ right_padding: 0, bottom_padding: 0 )
130
+ s = size
131
+ xy = cursor_xy
132
+ subwindows[name] =
133
+ self.class.new(
134
+ curses_ref: Rurses.curses.derwin(
135
+ curses_ref,
136
+ s[:lines] - (top_padding + bottom_padding),
137
+ s[:columns] - (left_padding + right_padding),
138
+ xy[:y] + top_padding,
139
+ xy[:x] + left_padding
140
+ )
141
+ )
142
+ end
143
+
144
+ def subwindow(name)
145
+ subwindows[name]
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rurses/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rurses"
8
+ spec.version = Rurses::VERSION
9
+ spec.authors = ["James Edward Gray II"]
10
+ spec.email = ["james@graysoftinc.com"]
11
+
12
+ spec.summary = %q{A more Ruby style wrapper over ncurses.}
13
+ spec.description = %q{This library aims to make working with ncurses code feel more natural to Rubyists. Full Ruby objects wrap C pointers, blocks are used where they make sense, and the library has some intelligent default behavior built in.}
14
+ spec.homepage = "https://github.com/JEG2/rurses"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "ffi-ncurses", "~> 0.4.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.8"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rurses
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Edward Gray II
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi-ncurses
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.4.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.4.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: This library aims to make working with ncurses code feel more natural
56
+ to Rubyists. Full Ruby objects wrap C pointers, blocks are used where they make
57
+ sense, and the library has some intelligent default behavior built in.
58
+ email:
59
+ - james@graysoftinc.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/rurses.rb
75
+ - lib/rurses/panel_stack.rb
76
+ - lib/rurses/version.rb
77
+ - lib/rurses/window.rb
78
+ - rurses.gemspec
79
+ homepage: https://github.com/JEG2/rurses
80
+ licenses: []
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.2.2
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: A more Ruby style wrapper over ncurses.
102
+ test_files: []