curses_menu 0.0.6 → 0.1.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: 74becd64c41df7dc7628e8c2b2c416c56f430c53d9be7e3e68698bb6bc1633aa
4
- data.tar.gz: 02bf486f61ae412243d536912202a36eb03d72d6c3c0e4ff034808221193d3f5
3
+ metadata.gz: 8dc4a9d14a6f315f6e2fba464d40d58d7a17922ee9afe1aabc1c045a4731a561
4
+ data.tar.gz: fde73b88bf3972e2b4353911dea6278cd606a2a8e673feeab8dff41a87a0f3e8
5
5
  SHA512:
6
- metadata.gz: d7014fece6ecf47e9f0dba36ee8b835841b8176809a09f52bf16e87892719075da8847427a62b2558068bb15abd6cb12cab0f49b4456693c417740c0d306f77d
7
- data.tar.gz: c407a1d42c57f39fa7c32488191097186c574fca00bd043e664b7cce45f0fba946d227712f9754025b0c9c72fac342cd721e3429cd2d4427b3d2bc4bfdc6dac6
6
+ metadata.gz: 6f9e782b2d1264e4b8b2e3b5f721574ba62abb20935a6d61fe6a70647d73386da9fc6c429689f35de3463f2d6aa6ce5d799fe8d17793b05a5621a27128bb3456
7
+ data.tar.gz: c39295fce31482e5f3dd91f2a0de36658912c4fd0d6da3d7a18d0ae55671b74dd5d13e6b74037c96b57993716776697f3b5cb01e5da484b660dc459cd2fe9d1a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # [v0.1.0](https://github.com/Muriel-Salvan/curses_menu/compare/v0.0.6...v0.1.0) (2022-10-12 15:58:25)
2
+
3
+ ### Features
4
+
5
+ * [[#5] [feature] Add possibility to render menu item titles in a lazy way](https://github.com/Muriel-Salvan/curses_menu/commit/6997c846f800d37441ea40bce109345e725ff12c)
6
+
1
7
  # [v0.0.6](https://github.com/Muriel-Salvan/curses_menu/compare/v0.0.5...v0.0.6) (2022-10-12 15:48:18)
2
8
 
3
9
  ### Patches
@@ -0,0 +1,14 @@
1
+ require 'curses_menu'
2
+
3
+ nbr_visible_items = Curses.stdscr.maxy - 5
4
+ CursesMenu.new 'Menu items using lazy rendering' do |menu|
5
+ (nbr_visible_items * 2).times do
6
+ menu.item "I am a normal item, rendered at #{Time.now}"
7
+ menu.item(proc do
8
+ "I am a lazy item, rendered at #{Time.now}"
9
+ end)
10
+ end
11
+ menu.item 'Refresh menu' do
12
+ :menu_refresh
13
+ end
14
+ end
@@ -1,5 +1,5 @@
1
1
  class CursesMenu
2
2
 
3
- VERSION = '0.0.6'
3
+ VERSION = '0.1.0'
4
4
 
5
5
  end
data/lib/curses_menu.rb CHANGED
@@ -55,9 +55,11 @@ class CursesMenu
55
55
  # Display the menu
56
56
  current_items[display_first_idx..display_first_idx + max_displayed_items - 1].each.with_index do |item_info, idx|
57
57
  selected = display_first_idx + idx == selected_idx
58
+ # Keep a cache of titles as they can be loaded in a lazy way for performance
59
+ item_info[:title_cached] = item_info[:title].is_a?(Proc) ? item_info[:title].call : item_info[:title] unless item_info.key?(:title_cached)
58
60
  print(
59
61
  window,
60
- item_info[:title],
62
+ item_info[:title_cached],
61
63
  from: display_first_char_idx,
62
64
  default_color_pair: item_info.key?(:actions) ? COLORS_MENU_ITEM : COLORS_LINE,
63
65
  force_color_pair: selected ? COLORS_MENU_ITEM_SELECTED : nil,
@@ -159,7 +161,7 @@ class CursesMenu
159
161
  # This method is meant to be called from a choose_from call.
160
162
  #
161
163
  # Parameters::
162
- # * *title* (String or CursesRow): Text to be displayed for this item
164
+ # * *title* (String, CursesRow or Proc): Text to be displayed for this item, or Proc returning this text when needed (lazy loading)
163
165
  # * *actions* (Hash<Object, Hash<Symbol,Object> >): Associated actions to this item, per shortcut [default: {}]
164
166
  # * *name* (String): Name of this action (displayed at the bottom of the menu)
165
167
  # * *execute* (Proc): Code called when this action is selected
@@ -0,0 +1,127 @@
1
+ describe CursesMenu do
2
+
3
+ it 'displays a menu with 1 item with lazy rendering' do
4
+ render_called = false
5
+ test_menu(title: 'Menu title') do |menu|
6
+ menu.item(proc do
7
+ render_called = true
8
+ 'Menu item lazy'
9
+ end)
10
+ end
11
+ expect(render_called).to be true
12
+ assert_line 3, 'Menu item lazy'
13
+ end
14
+
15
+ it 'displays a menu with 1 item in a CursesRow with lazy rendering' do
16
+ render_called = false
17
+ test_menu(title: 'Menu title') do |menu|
18
+ menu.item(proc do
19
+ render_called = true
20
+ CursesMenu::CursesRow.new({ cell: { text: 'Menu item lazy' } })
21
+ end)
22
+ end
23
+ expect(render_called).to be true
24
+ assert_line 3, 'Menu item lazy'
25
+ end
26
+
27
+ it 'doesn\'t lazy render when the item is not displayed' do
28
+ nbr_visible_items = Curses.stdscr.maxy - 5
29
+ render_called = false
30
+ test_menu(title: 'Menu title') do |menu|
31
+ (nbr_visible_items * 2).times do |idx|
32
+ menu.item "Menu item #{idx}"
33
+ end
34
+ menu.item(proc do
35
+ render_called = true
36
+ 'Menu item lazy'
37
+ end)
38
+ end
39
+ expect(render_called).to be false
40
+ end
41
+
42
+ it 'keeps lazy rendered titles in a cache while navigating' do
43
+ nbr_renders = 0
44
+ test_menu(keys: [Curses::KEY_DOWN, Curses::KEY_DOWN, Curses::KEY_DOWN]) do |menu|
45
+ menu.item 'Menu item 1'
46
+ menu.item(proc do
47
+ nbr_renders += 1
48
+ 'Menu item 2 Lazy'
49
+ end)
50
+ menu.item 'Menu item 3'
51
+ menu.item 'Menu item 4', actions: { 'a' => { name: 'Special action', execute: proc {} } }
52
+ end
53
+ expect(nbr_renders).to eq 1
54
+ assert_line(-1, '= Arrows/Home/End: Navigate | Esc: Exit | a: Special action')
55
+ end
56
+
57
+ it 'keeps lazy rendered titles in a cache while navigating across pages' do
58
+ nbr_renders = 0
59
+ nbr_visible_items = Curses.stdscr.maxy - 5
60
+ test_menu(keys: [Curses::KEY_DOWN, Curses::KEY_DOWN, Curses::KEY_END, Curses::KEY_HOME, Curses::KEY_END, Curses::KEY_HOME, Curses::KEY_END]) do |menu|
61
+ (nbr_visible_items * 2).times do |idx|
62
+ menu.item "Menu item #{idx}"
63
+ end
64
+ menu.item(proc do
65
+ nbr_renders += 1
66
+ 'Menu item Lazy'
67
+ end)
68
+ end
69
+ expect(nbr_renders).to eq 1
70
+ assert_line(-3, 'Menu item Lazy')
71
+ end
72
+
73
+ it 'refreshes lazy rendered titles between menu refreshes' do
74
+ nbr_renders = 0
75
+ test_menu(keys: [Curses::KEY_DOWN, Curses::KEY_UP, CursesMenu::KEY_ENTER, Curses::KEY_DOWN, Curses::KEY_UP]) do |menu|
76
+ menu.item 'Menu item Refresh' do
77
+ :menu_refresh
78
+ end
79
+ menu.item(proc do
80
+ nbr_renders += 1
81
+ 'Menu item Lazy'
82
+ end)
83
+ end
84
+ expect(nbr_renders).to eq 2
85
+ end
86
+
87
+ it 'does not refresh lazy rendered titles when executing actions' do
88
+ nbr_renders = 0
89
+ test_menu(keys: [Curses::KEY_DOWN, Curses::KEY_UP, CursesMenu::KEY_ENTER, Curses::KEY_DOWN, Curses::KEY_UP]) do |menu|
90
+ menu.item 'Menu item' do
91
+ # Do nothing
92
+ end
93
+ menu.item(proc do
94
+ nbr_renders += 1
95
+ 'Menu item Lazy'
96
+ end)
97
+ end
98
+ expect(nbr_renders).to eq 1
99
+ end
100
+
101
+ it 'does not refresh lazy rendered titles when getting into sub-menus' do
102
+ nbr_renders = 0
103
+ test_menu(
104
+ keys: [
105
+ # Enter sub-menu
106
+ CursesMenu::KEY_ENTER,
107
+ Curses::KEY_DOWN,
108
+ # Back to first menu
109
+ CursesMenu::KEY_ESCAPE,
110
+ Curses::KEY_DOWN
111
+ ]
112
+ ) do |menu, key_presses|
113
+ menu.item 'Sub-menu' do
114
+ described_class.new('Sub-menu title', key_presses: key_presses) do |sub_menu|
115
+ sub_menu.item 'Sub-menu item 1'
116
+ sub_menu.item 'Sub-menu item 2'
117
+ end
118
+ end
119
+ menu.item(proc do
120
+ nbr_renders += 1
121
+ 'Menu item Lazy'
122
+ end)
123
+ end
124
+ expect(nbr_renders).to eq 1
125
+ end
126
+
127
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: curses_menu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Muriel Salvan
@@ -93,6 +93,7 @@ extra_rdoc_files:
93
93
  - examples/automatic_key_presses.rb
94
94
  - examples/formatting.rb
95
95
  - examples/hello.rb
96
+ - examples/lazy_menu_items.rb
96
97
  - examples/refresh.rb
97
98
  - examples/scrolling.rb
98
99
  - examples/several_items.rb
@@ -106,6 +107,7 @@ files:
106
107
  - examples/automatic_key_presses.rb
107
108
  - examples/formatting.rb
108
109
  - examples/hello.rb
110
+ - examples/lazy_menu_items.rb
109
111
  - examples/refresh.rb
110
112
  - examples/scrolling.rb
111
113
  - examples/several_items.rb
@@ -117,6 +119,7 @@ files:
117
119
  - spec/curses_menu_test.rb
118
120
  - spec/curses_menu_test/actions_spec.rb
119
121
  - spec/curses_menu_test/formatting_spec.rb
122
+ - spec/curses_menu_test/lazy_spec.rb
120
123
  - spec/curses_menu_test/rubocop_spec.rb
121
124
  - spec/curses_menu_test/scrolling_spec.rb
122
125
  - spec/curses_menu_test/simple_navigation_spec.rb