remedy 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2efe0ed0180c60202b73b0cf9f0fce46b51bcfeefe2a83171f4915c9d2b022ad
4
- data.tar.gz: 2b11a6544dec2796c6bdf5ce96691be0c7b232f132174d0b79496098252fe5ab
3
+ metadata.gz: af17b629f35cdae5c8a15c47a6e23f5aa51bce006afa7f7e129a8640c9d191b1
4
+ data.tar.gz: 36568e55906c286e97666183f9e6eb704535b6155830bc93acda7c59299a2ab6
5
5
  SHA512:
6
- metadata.gz: c76c67debc47dd95b9b614a6681923cb5e47f53c4135335c39170243bcb9a007630af5bc364894f33489cde79c5692e9a28d97ed0f5caf0bfbe83e229ad394eb
7
- data.tar.gz: a001aa18bd36640ebe267c8d6ae4bf68e7e73d60df0017e31875b222ff30b87169c4c36d06130c27096def9e4ccfa26c50ad350ea1d2cf2adbb42228af5dcf8d
6
+ metadata.gz: 447afaf7d518eb6b8ddeb80dc9d7b4cae579a978ec6af952749dafe52b2d95b930ef6e6bbc24378ebab81ec48807102453d07e0f8196b251d62ffd84662f80a9
7
+ data.tar.gz: 7d800899e64c41e9acd31215df9b66fd1dd96f9da52a735e3393c855bf91f8d75f1fc224dc559f9560fedaa89a4c88468cce94b9713bd3bb1ef3e2b428d52e61
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "remedy", path: "../.."
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "remedy", path: "../.."
@@ -11,11 +11,6 @@ class Menu
11
11
 
12
12
  # will do basic setup and then loop over user input
13
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
-
19
14
  # if the user resizes the screen we redraw it to fit the new dimensions
20
15
  Console.set_console_resized_hook! do |size|
21
16
  draw
@@ -24,10 +19,10 @@ class Menu
24
19
  # create an interaction object to handle user input
25
20
  interaction = Interaction.new
26
21
 
27
- # call draw here because interaction blocks until it gets input
22
+ # call draw here because the interaction loop blocks until it gets input
28
23
  draw
29
24
 
30
- # loop over user input (individual keypresses)
25
+ # loop over individual keypresses or ANSI codes from the terminal
31
26
  interaction.loop do |key|
32
27
  @last_key = key
33
28
  if key == "q" then
@@ -39,7 +34,7 @@ class Menu
39
34
 
40
35
  # this tells the Viewport to draw to the screen
41
36
  def draw
42
- @viewport.draw content, Size([0,0]), header, footer
37
+ @viewport.draw content, Size.zero, header, footer
43
38
  end
44
39
 
45
40
  # this is the body of our menu, it will be squished if the terminal is too small
@@ -16,6 +16,7 @@ module Remedy
16
16
  reset_width!
17
17
  line = "#{line}" # opportunistically convert any object into a string
18
18
  @lines += clean line unless line.nil? || line.empty?
19
+ self
19
20
  end
20
21
 
21
22
  def first
@@ -26,16 +27,17 @@ module Remedy
26
27
  lines.last
27
28
  end
28
29
 
29
- def length
30
+ def height
30
31
  lines.length
31
32
  end
33
+ alias_method :length, :height
32
34
 
33
35
  def width
34
- @width ||= lines.max{|line| line.length }.length
36
+ lines.map{|line| line.length }.max
35
37
  end
36
38
 
37
39
  def size
38
- Size.new length, width
40
+ Size.new height, width
39
41
  end
40
42
 
41
43
  def to_a
data/lib/remedy/size.rb CHANGED
@@ -11,6 +11,10 @@ module Remedy
11
11
  end
12
12
  attr_accessor :dimensions
13
13
 
14
+ def self.zero
15
+ self.new([0,0])
16
+ end
17
+
14
18
  def - other_size
15
19
  if other_size.respond_to? :length then
16
20
  self.class.new subtract(other_size)
@@ -41,11 +45,13 @@ module Remedy
41
45
  def rows
42
46
  dimensions[0]
43
47
  end
48
+ alias_method :height, :rows
44
49
 
45
50
  def cols
46
51
  dimensions[1]
47
52
  end
48
- alias :columns :cols
53
+ alias_method :columns, :cols
54
+ alias_method :width, :cols
49
55
 
50
56
  def [] index
51
57
  dimensions[index]
@@ -1,3 +1,3 @@
1
1
  module Remedy
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -5,14 +5,10 @@ require 'remedy/ansi'
5
5
 
6
6
  module Remedy
7
7
  class Viewport
8
- def draw content, center = Size.new(0,0), header = [], footer = []
9
- range = range_find content, center, content_size(header,footer)
8
+ def draw content, scroll = Size.zero, header = Partial.new, footer = Partial.new
9
+ range = range_find content, scroll, available_space(header,footer)
10
10
 
11
- if content.size.fits_into? range then
12
- viewable_content = content
13
- else
14
- viewable_content = content.excerpt *range
15
- end
11
+ viewable_content = content.excerpt *range
16
12
 
17
13
  view = View.new viewable_content, header, footer
18
14
 
@@ -20,19 +16,22 @@ module Remedy
20
16
  Console.output << view
21
17
  end
22
18
 
23
- def range_find partial, center, heightwidth
24
- row_size, col_size = heightwidth
25
- row_limit, col_limit = partial.size
19
+ def range_find partial, scroll, available_heightwidth
20
+ avail_height, avail_width = available_heightwidth
21
+ partial_height, partial_width = partial.size
22
+
23
+ center_row, center_col = scroll
26
24
 
27
- center_row, center_col = center
25
+ row_range = get_range center_row, partial_height, avail_height
26
+ col_range = get_range center_col, partial_width, avail_width
28
27
 
29
- row_range = center_range center_row, row_size, row_limit
30
- col_range = center_range center_col, col_size, col_limit
31
28
  [row_range, col_range]
32
29
  end
33
30
 
34
- def content_size header, footer
35
- trim = Size [header.length + footer.length, 0]
31
+ # This determines the maximum amount of room left available for Content
32
+ # after taking into consideration the height of the Header and Footer
33
+ def available_space header, footer
34
+ trim = Size [header.height + footer.height, 0]
36
35
  size - trim
37
36
  end
38
37
 
@@ -40,20 +39,26 @@ module Remedy
40
39
  Size Console.size
41
40
  end
42
41
 
43
- def center_range center, width, limit
44
- range_start = center - (width / 2)
42
+ def get_range offset, actual, available
43
+ # if the actual content can fit into the available space, then we're done
44
+ return (0...actual) if actual <= available
45
45
 
46
- if range_start + width > limit then
47
- range_start = limit - width
48
- end
46
+ # otherwise start looking at the scrolling offset, if any
49
47
 
50
- if range_start < 0 then
48
+ # clamp the offset within the possible range of the actual content
49
+ if offset < 0 then
51
50
  range_start = 0
51
+ elsif offset > actual then
52
+ range_start = actual
53
+ else
54
+ range_start = offset
52
55
  end
53
56
 
54
- range_end = range_start + width
57
+ # determine the subset of content that can be displayed
58
+ range_end = range_start + (available - offset)
55
59
 
56
60
  (range_start...range_end)
57
61
  end
62
+
58
63
  end
59
64
  end
@@ -3,11 +3,17 @@ require 'remedy/viewport'
3
3
 
4
4
  describe Remedy::Viewport do
5
5
  it 'should be able to execute the example code from the readme' do
6
+ @stdout = $stdout
6
7
  joke = ::Remedy::Partial.new
7
8
  joke << "Q: What's the difference between a duck?"
8
9
  joke << "A: Purple, because ice cream has no bones!"
9
10
 
10
11
  screen = ::Remedy::Viewport.new
12
+ sio = StringIO.new
13
+ $stdout = sio
11
14
  screen.draw joke unless ENV['CI']
15
+ expect(sio.string).to include("ice cream")
16
+ ensure
17
+ $stdout = @stdout
12
18
  end
13
19
  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.3.1
4
+ version: 0.4.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: 2023-10-04 00:00:00.000000000 Z
11
+ date: 2023-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -42,7 +42,9 @@ files:
42
42
  - LICENSE.txt
43
43
  - README.markdown
44
44
  - Rakefile
45
+ - examples/from_readme/Gemfile
45
46
  - examples/from_readme/readme.rb
47
+ - examples/menu/Gemfile
46
48
  - examples/menu/menu.rb
47
49
  - lib/remedy.rb
48
50
  - lib/remedy/ansi.rb