remedy 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 346c9d17734819cc9516ee578ae4b647e70c13c876c7a84e3ffe74e9cb956e27
4
- data.tar.gz: 4add78044a3161d6eae52452a6a0bd3f3ba46a18b238af7b533f996ac6599ac6
3
+ metadata.gz: b308f7cf135ba7a8f6ab69ef9c56ff7d640a189c9f44056e14f49b987948ec1f
4
+ data.tar.gz: 9f1969fea60358a6585974f948f572bdeeeb55afd0f176d6dd220bdd4b29c094
5
5
  SHA512:
6
- metadata.gz: e3a676f7b7df84dc9732aad2518a4bf2d53625736f0928ed3c2dea7f613d727d3fb6539b724939fa83c427cff098d39fcc7ee09412b195d44dca05d3e28dbbbd
7
- data.tar.gz: a6f5a5cc4a4523b6220b426b4016b6370c814f38a2effc993c729a1456e22f3dd9765c43d62bb1f5a8e0ee351a4e14530a87286b58e87409ba9b5c2968cf46ff
6
+ metadata.gz: 0d664d1b61638b139bd2aef66ed2facd0d25e4dbaa65a39014892bcb3f967895cbf78304a8b4235cce1a5210384e7cadaa6672fa41a2d2e102c22a80c0cd40ef
7
+ data.tar.gz: ea8a3d1aeaeb8a0e474bf2a69f6837ab300810dfaf3bd014200e284f6e45058875fa5363e73948228806e35e24957ded1d342d07a8622f089726e94da2e93a3b
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.3.7
1
+ 2.6.3
data/README.markdown CHANGED
@@ -1,10 +1,13 @@
1
1
  Remedy
2
2
  ======
3
3
 
4
- Remedy is a console interaction framework along the lines of Curses written in pure Ruby with an Object-Oriented approach and well-seperated concerns making it easy to use what you need and ignore the rest.
4
+ Remedy is a console interaction framework along the lines of Curses written in pure Ruby. It is modular, making it easy to use what you need and ignore the rest.
5
+
6
+ [![Gem Version](https://img.shields.io/gem/v/remedy.svg?style=for-the-badge)](https://rubygems.org/gems/remedy)
7
+ [![Gem Downloads](https://img.shields.io/gem/dt/remedy.svg?style=for-the-badge)](https://rubygems.org/gems/remedy)
8
+ [![Build Status](https://img.shields.io/travis/acook/remedy.svg?style=for-the-badge)](https://travis-ci.org/acook/remedy)
9
+ [![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/acook/remedy?style=for-the-badge)](https://codeclimate.com/github/acook/remedy/maintainability)
5
10
 
6
- [![TravisCI](http://img.shields.io/travis/acook/remedy.svg?style=flat)](https://travis-ci.org/acook/remedy)
7
- [![Code Climate](https://codeclimate.com/github/acook/remedy/badges/gpa.svg)](https://codeclimate.com/github/acook/remedy)
8
11
 
9
12
  If you have any suggestions or find any bugs, drop them in GitHub/issues so I can keep track of them. Thanks!
10
13
 
@@ -42,6 +45,8 @@ There are objects for input as well as output, including low level console keyst
42
45
 
43
46
  The `Interaction` object wraps raw keyboard reads and streamlines some aspects of accepting keyboard input.
44
47
 
48
+ For instance to get a keypress from the terminal and display it:
49
+
45
50
  ```ruby
46
51
  include Remedy
47
52
  user_input = Interaction.new
@@ -67,7 +72,7 @@ The `Interaction` object wraps raw keyboard reads and streamlines some aspects o
67
72
 
68
73
  `Remedy::Partial` has the subclasses `Header`, `Footer`, and `Content`.
69
74
 
70
- You can use the above classes to divide your Views into 3 seperate pieces. Content will be truncated as needed to accomodate the header and footer and the dimensions of the console. You can also specify the cursor/scroll position of the content being drawn, and when specifying headers or footers, you must.
75
+ You can use the above classes to divide your Views into 3 separate pieces. Content will be truncated as needed to accommodate the header and footer and the dimensions of the console. You can also specify the cursor/scroll position of the content being drawn, and when specifying headers or footers, you must.
71
76
 
72
77
  ```ruby
73
78
  include Remedy
@@ -95,14 +100,31 @@ The most interesting function in my opinion is the callback that gets triggered
95
100
  include Remedy
96
101
 
97
102
  screen = Viewport.new
98
- notice = Content.new
99
- notice << "You just resized your screen!\n\nBrilliant!"
100
103
 
101
- Console.set_console_resized_hook! do
104
+ Console.set_console_resized_hook! do |size|
105
+ notice = Content.new
106
+ notice << "You just resized your screen!\n\nNew size:"
107
+ notice << size
102
108
  screen.draw notice
103
109
  end
104
110
  ```
105
111
 
112
+ Remedy in the Wild
113
+ ------------------
114
+
115
+ Remedy was originally written for my own console-based game which was sort of like Dwarf Fortress. Most of the project files were lost, but since Remedy was extracted from it and open-sourced it has lived on.
116
+
117
+ Here are a couple of projects that use many of Remedy's features:
118
+ - A multiplayer Yahtzee for web and console: [YahtzeeGame](https://github.com/ProgrammingPractice/YahtzeeGame)
119
+ - Twitter/RSS/Facebook reader: [noizee](https://github.com/acook/noizee)
120
+
121
+ Check them out!
122
+
123
+ Examples
124
+ --------
125
+
126
+ The `examples` directory has a couple of running implementations to get you started!
127
+
106
128
  Contributing
107
129
  ------------
108
130
 
@@ -1,13 +1,16 @@
1
+ require 'bundler'
2
+ Bundler.require
1
3
  require 'remedy'
2
4
 
3
5
  include Remedy
4
6
 
5
7
  screen = Viewport.new
6
8
 
7
- notice = Content.new
8
- notice << "You just resized your screen!\n\nBrilliant!"
9
+ Console.set_console_resized_hook! do |size|
10
+ notice = Content.new
11
+ notice << "You just resized your screen!\n\nNew size:"
12
+ notice << size
9
13
 
10
- Console.set_console_resized_hook! do
11
14
  screen.draw notice
12
15
  end
13
16
 
@@ -36,11 +39,12 @@ screen.draw jokes, Size.new(0,0), title, disclaimer
36
39
  user_input.get_key
37
40
 
38
41
  ANSI.cursor.next_line!
39
- loop_demo = Interaction.new "press q to exit, or any other key to display it\n"
42
+ keys = Content.new
43
+ loop_demo = Interaction.new "press q to exit, or any other key to display that key's name\n"
40
44
  loop_demo.loop do |key|
41
- ANSI.cursor.beginning_of_line!
42
- ANSI.command.clear_line!
43
- puts key
45
+ keys << key
46
+
47
+ screen.draw keys
44
48
  break if key == ?q
45
49
  end
46
50
 
@@ -1,12 +1,23 @@
1
+ require 'bundler'
2
+ Bundler.require
1
3
  require 'remedy'
2
4
 
3
5
  class Menu
4
6
  include Remedy
5
7
 
8
+ def initialize
9
+ @viewport = Viewport.new
10
+ end
11
+
6
12
  # will do basic setup and then loop over user input
7
13
  def listen
14
+ # get the screen in a reliable state and clear it
15
+ ANSI.screen.safe_reset!
16
+ ANSI.cursor.home!
17
+ ANSI.command.clear_screen!
18
+
8
19
  # if the user resizes the screen we redraw it to fit the new dimensions
9
- Console.set_console_resized_hook! do
20
+ Console.set_console_resized_hook! do |size|
10
21
  draw
11
22
  end
12
23
 
@@ -28,12 +39,15 @@ class Menu
28
39
 
29
40
  # this tells the Viewport to draw to the screen
30
41
  def draw
31
- Viewport.new.draw content, Size([0,0]), header, footer
42
+ @viewport.draw content, Size([0,0]), header, footer
32
43
  end
33
44
 
34
45
  # this is the body of our menu, it will be squished if the terminal is too small
35
46
  def content
36
- c = Partial.new
47
+ # this creates a new content every time we draw
48
+ # you may want to only create a new content/header/footer when they change
49
+ # or create your own Partial subclass
50
+ c = Content.new
37
51
  c << <<-CONTENT
38
52
 
39
53
  1. Do the thing
@@ -52,7 +66,7 @@ class Menu
52
66
 
53
67
  # footers are displayed the bottom of the viewport
54
68
  def footer
55
- Footer.new << "You pressed: #{@last_key}"
69
+ Footer.new << "Screen size: #{Console.size} You pressed: #{@last_key}"
56
70
  end
57
71
  end
58
72
 
@@ -42,19 +42,19 @@ module Remedy
42
42
  end
43
43
 
44
44
  def columns
45
- size.last
45
+ size.cols
46
46
  end
47
47
  alias_method :width, :columns
48
48
 
49
49
  def rows
50
- size.first
50
+ size.rows
51
51
  end
52
52
  alias_method :height, :rows
53
53
 
54
54
  def size
55
55
  str = [0, 0, 0, 0].pack('SSSS')
56
56
  if input.ioctl(TIOCGWINSZ, str) >= 0 then
57
- str.unpack('SSSS').first 2
57
+ Size.new str.unpack('SSSS').first 2
58
58
  else
59
59
  raise UnknownConsoleSize, "Unable to get console size"
60
60
  end
@@ -65,8 +65,8 @@ module Remedy
65
65
  end
66
66
 
67
67
  def set_console_resized_hook!
68
- Console::Resize.set_console_resized_hook! do
69
- yield
68
+ Console::Resize.set_console_resized_hook! do |*args|
69
+ yield *args
70
70
  end
71
71
  end
72
72
 
@@ -24,18 +24,21 @@ module Remedy; module Console; module Resize
24
24
  def set_console_resized_hook!
25
25
  @resize_count = 0
26
26
 
27
- command = lambda { |x|
27
+ Signal.trap 'SIGWINCH' do
28
28
  resizing!
29
- sleep 0.25
30
29
 
31
30
  if resized? then
32
- yield
31
+ begin
32
+ yield Console.size
33
+ rescue Exception => ex
34
+ # Ruby will eat *any* errors inside a trap,
35
+ # so we need to expose them for debuggability
36
+ p ex
37
+ end
33
38
  end
34
39
 
35
40
  resized!
36
- }
37
-
38
- Signal.trap 'SIGWINCH', command
41
+ end
39
42
  end
40
43
 
41
44
  def default_console_resized_hook!
data/lib/remedy/key.rb CHANGED
@@ -15,6 +15,10 @@ module Remedy
15
15
  seq
16
16
  end
17
17
 
18
+ def enc
19
+ seq.dump[1..-2]
20
+ end
21
+
18
22
  def name
19
23
  @name ||= Characters[seq] || :unknown
20
24
  end
@@ -51,6 +55,10 @@ module Remedy
51
55
  @recognized ||= name != :unknown
52
56
  end
53
57
 
58
+ def known?
59
+ !!Characters[seq]
60
+ end
61
+
54
62
  def single?
55
63
  @single ||= raw.length == 1
56
64
  end
@@ -60,7 +68,7 @@ module Remedy
60
68
  end
61
69
 
62
70
  def to_s
63
- @to_s ||= name.to_s
71
+ @to_s ||= known? ? name.to_s : enc
64
72
  end
65
73
 
66
74
  def value
@@ -14,6 +14,7 @@ module Remedy
14
14
 
15
15
  def << line
16
16
  reset_width!
17
+ line = "#{line}" # opportunistically convert any object into a string
17
18
  @lines += clean line unless line.nil? || line.empty?
18
19
  end
19
20
 
@@ -64,7 +65,7 @@ module Remedy
64
65
  end
65
66
 
66
67
  def split line
67
- line.split /\r\n|\n\r|\n|\r/
68
+ line.split(/\r\n|\n\r|\n|\r/)
68
69
  end
69
70
  end
70
71
  end
data/lib/remedy/size.rb CHANGED
@@ -39,12 +39,13 @@ module Remedy
39
39
  end
40
40
 
41
41
  def rows
42
- dimensions.first
42
+ dimensions[0]
43
43
  end
44
44
 
45
45
  def cols
46
46
  dimensions[1]
47
47
  end
48
+ alias :columns :cols
48
49
 
49
50
  def [] index
50
51
  dimensions[index]
@@ -1,3 +1,3 @@
1
1
  module Remedy
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remedy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony M. Cook
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-16 00:00:00.000000000 Z
11
+ date: 2021-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -83,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  - !ruby/object:Gem::Version
84
84
  version: '0'
85
85
  requirements: []
86
- rubygems_version: 3.0.4
86
+ rubygems_version: 3.1.2
87
87
  signing_key:
88
88
  specification_version: 4
89
89
  summary: Pure Ruby Console Interaction Library