curses-extension 2.1 → 2.3

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: 0a272233095a365552a978ea26953df75a91f985ca30a9765f7191338ffc5468
4
- data.tar.gz: 6b807771cb90a3bc5bdbec098f01ec31414f55bfdb982cdf88cf3b0b62f33ca2
3
+ metadata.gz: 42000c1ac48e5ea98e193df0c18c8874146f26e53f671cf84f6338a7dda7cce6
4
+ data.tar.gz: ec6dad6abda95da23ecbbabc23be1019860f9da2929f6a0f3962baaede26df7c
5
5
  SHA512:
6
- metadata.gz: 5fa593f92f9cdbb0774350fb4fa2c956f9be7435021c52559770fe2c3c35bb7753d25f35b69e7b192818f3ae795be7705374c3c02e1714f14573d75c1b079ae9
7
- data.tar.gz: 445fcf7a916887d489d16835044513c8ad76664bfa9ed7163663a9edb12ca307806b671b7e76f4d847da53613f27e1563fdda69ff62e25d772ea2cde5436463f
6
+ metadata.gz: 2ab8dbfc99e8d9a93c030b5c1a24390abb8b747911cd3bed0a4495ec4fe67d91ee79d8a0415b08ca42f09a624d92fd2644c8bb6cc8fcd67e075ff3ae5807a92a
7
+ data.tar.gz: ae787aa46b4e57d114714577ae000992bf7a55c40f90eb6a6ca9982c937a05a7294a9f7f99081f5f4617ce576c0ce473ce9a6c448e8bac1cb5685f3809d88b6e
data/lib/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Ruby-Curses-Class-Extension
2
2
  Extending the Ruby Curses module with some obvious needed functionality.
3
3
 
4
+ No more fiddling with color pairs. No more manual filling lines or framing.
5
+
4
6
  With this extension, you don't need to initiate any color pairs or add text to
5
7
  windows with odd syntax. Use any foreground and backround color you see fit
6
8
  and let this extension take care of the weird need to pair colors and initiate
@@ -13,8 +15,18 @@ default foreground, background and attribute for a window (e.g. `win.fg =
13
15
  to a window with `win.p("Hello World")` where the defaults will be applied.
14
16
  You can override the defaults with e.g. `win.p(124, "Hello World")` to write
15
17
  the text in red or `win.p(76, 240, Curses::A_BOLD, "Hello World")` to write
16
- the text in bold green on a gray background.
18
+ the text in bold green on a gray background.
19
+
20
+ There is also the handy method `nl` that fills the rest of the line with
21
+ spaces and (optional) background color, making it an effective newline.
17
22
 
23
+ And then there is the convenient `frame`method that adds a... frame to the
24
+ window.
25
+
26
+ The `curses-template.rb` contains code that helps you understand how you can
27
+ easily create curses applications in Ruby. It is a fully runnable app that
28
+ doesn't do much - but you can fiddle around with the code and test
29
+ functionality for yourself.
18
30
 
19
31
  ## Attributes
20
32
  Attribute | Description
@@ -26,18 +38,22 @@ update | Whether to update the window on the next refresh
26
38
  index | Used to track an index for each window (used to display lists such as the content of a directory, etc.)
27
39
 
28
40
  ## Functions
41
+ Parameters in square are optional.
42
+
29
43
  Function | Description
30
44
  ------------------------------------|--------------------------------------------------------
31
- clr | Clears window without flicker (win.clear flickers)
32
- clr_to_cur_line | Clears the window up to the current line
33
- clr_from_cur_line | Clears the rest of the window after the current line
34
- fill | Fill window with color as set by :color ( or :bg if not :color is set)
35
- fill_to_cur_pos | Fill the window up to the current line
36
- fill_from_cur_pos | Fill the rest of the window after the current line
37
- p(fg, bg, attr, text) | Write text to window with fg/bg and attributes (will handle the exceptions if no colors are set).
38
- You can use `puts` instead of `p` (it's an alias)
39
- pclr(text) | As `p(text)` but also clears the rest of the window
40
- format(text) | Format text so that it linebreaks neatly inside window
45
+ clr | Clears window without flicker (win.clear flickers)
46
+ clr_to_cur_line | Clears the window up to the current line
47
+ clr_from_cur_line | Clears the rest of the window after the current line
48
+ fill | Fill window with color as set by :color ( or :bg if not :color is set)
49
+ fill_to_cur_pos | Fill the window up to the current line
50
+ fill_from_cur_pos | Fill the rest of the window after the current line
51
+ p([fg], [bg], [attr], text) | Write text to window with fg/bg and attributes (will handle the exceptions if no colors are set)
52
+ You can use `puts` instead of `p` (it's an alias). `fg`, `bg` and `attr` are optional parameters
53
+ pclr(text) | As `p(text)` but also clears the rest of the window
54
+ nl([bg]) | Newline. Puts spaces with (optional) background colors until the end of the line
55
+ frame([fg],[bg]) | Frame the window
56
+ format(text) | Format text so that it linebreaks neatly inside window
41
57
 
42
58
  ## Curses template
43
59
  The `curses_template.rb` includes the class extension and serves as the basis for my curses applications. It is a runnable curses application that does nothing.
@@ -75,10 +75,21 @@ class Curses::Window # CLASS EXTENSION
75
75
  self.attron(color_pair(cp) | attr) { self << text }
76
76
  self.refresh
77
77
  end
78
+ def nl(bg = self.bg)
79
+ bg = 232 if bg == nil
80
+ f = " " * (self.maxx - self.curx)
81
+ self.p(self.fg, bg, self.attr, f)
82
+ end
78
83
  def pclr(fg = self.fg, bg = self.bg, attr = self.attr, text) # Puts text to window with full set of attributes and clears rest of window
79
84
  self.p(fg, bg, attr, text)
80
85
  self.clr_from_cur_line
81
86
  end
87
+ def frame(fg = self.fg, bg = self.bg)
88
+ self.setpos(0,0)
89
+ self.p("┌" + "─"*(self.maxx-2) + "┐")
90
+ (self.maxy-2).times {self.p("│" + " "*(self.maxx-2) + "│")}
91
+ self.p("└" + "─"*(self.maxx-2) + "┘")
92
+ end
82
93
  def format(text) # Format text so that it linebreaks neatly inside window
83
94
  return "\n" + text.gsub(/(.{1,#{self.maxx}})( +|$\n?)|(.{1,#{self.maxx}})/, "\\1\\3\n")
84
95
  end
@@ -103,20 +103,32 @@ class Curses::Window # CLASS EXTENSION
103
103
  fg = 255 if fg == nil
104
104
  bg = 0 if bg == nil
105
105
  attr = 0 if attr == nil
106
- cp = Curses::Window.pair(fg, bg)
106
+ cp = Curses::Window.pair(fg, bg)
107
107
  self.attron(color_pair(cp) | attr) { self << text }
108
108
  self.refresh
109
109
  end
110
+ def nl(bg = self.bg)
111
+ bg = 232 if bg == nil
112
+ f = " " * (self.maxx - self.curx)
113
+ self.p(self.fg, bg, self.attr, f)
114
+ end
110
115
  def pclr(fg = self.fg, bg = self.bg, attr = self.attr, text) # Puts text to window with full set of attributes and clears rest of window
111
116
  self.p(fg, bg, attr, text)
112
117
  self.clr_from_cur_line
113
118
  end
119
+ def frame(fg = self.fg, bg = self.bg)
120
+ self.setpos(0,0)
121
+ self.p("┌" + "─"*(self.maxx-2) + "┐")
122
+ (self.maxy-2).times {self.p("│" + " "*(self.maxx-2) + "│")}
123
+ self.p("└" + "─"*(self.maxx-2) + "┘")
124
+ end
114
125
  def format(text) # Format text so that it linebreaks neatly inside window
115
126
  return "\n" + text.gsub(/(.{1,#{self.maxx}})( +|$\n?)|(.{1,#{self.maxx}})/, "\\1\\3\n")
116
127
  end
117
128
  alias :puts :p
118
129
  end
119
130
 
131
+ # GENERAL FUNCTIONS
120
132
  def getchr # Process key presses
121
133
  c = STDIN.getch
122
134
  #c = STDIN.getch(min: 0, time: 1) # Use this if you need to poll for user keys
@@ -165,27 +177,51 @@ def getchr # Process key presses
165
177
  end
166
178
  return chr
167
179
  end
168
-
169
180
  def main_getkey # GET KEY FROM USER
170
181
  chr = getchr
171
182
  case chr
172
- when '?' # Show helptext in right window
173
- # Add code to show help text here
174
- when 'UP' # Examples of moving up and down in a window
175
- @index = @index <= @min_index ? @max_index : @index - 1
183
+ when '?' # Show helptext in right window - add code to show help text here
184
+ @w_t.clr
185
+ @w_t.p("Try pressing 'w'")
186
+ @w_t.nl
187
+ @w_t.update = false
188
+ # Examples of moving up and down in a window
189
+ # You must set @min_index and @max_index in the main loop of the program
190
+ when 'UP'
191
+ @w_l.index = @w_l.index <= @min_index ? @max_index : @w_l.index - 1
176
192
  when 'DOWN'
177
- @index = @index >= @max_index ? @min_index : @index + 1
193
+ @w_l.index = @w_l.index >= @max_index ? @min_index : @w_l.index + 1
178
194
  when 'PgUP'
179
- @index -= @w_l.maxy - 2
180
- @index = @min_index if @index < @min_index
195
+ @w_l.index -= @w_l.maxy - 2
196
+ @w_l.index = @min_index if @w_l.index < @min_index
181
197
  when 'PgDOWN'
182
- @index += @w_l.maxy - 2
183
- @index = @max_index if @index > @max_index
198
+ @w_l.index += @w_l.maxy - 2
199
+ @w_l.index = @max_index if @w_l.index > @max_index
184
200
  when 'HOME'
185
- @index = @min_index
201
+ @w_l.index = @min_index
186
202
  when 'END'
187
- @index = @max_index
188
- when 'l'
203
+ @w_l.index = @max_index
204
+ when 'w' # Shows how you can add a window and get input from there and then close the window
205
+ maxx = Curses.cols
206
+ maxy = Curses.lines
207
+ # Curses::Window.new ( h, w, y, x)
208
+ @w_w = Curses::Window.new( 6, 20, maxy/2-3, maxx/2-10)
209
+ @w_w.fg, @w_w.bg, @w_w.attr = 255, 233, Curses::A_BOLD
210
+ @w_w.frame
211
+ @w_w.setpos(4, 7)
212
+ @w_w.p(255,130," y/n? ")
213
+ chrw = getchr
214
+ case chrw
215
+ when 'y'
216
+ @w_w.setpos(4, 7)
217
+ @w_w.p(255,22," YES! ")
218
+ when 'n'
219
+ @w_w.setpos(4, 7)
220
+ @w_w.p(255,52," NO!! ")
221
+ end
222
+ chrw = getchr
223
+ @w_w.close
224
+ when 'a'
189
225
  # ...etc
190
226
  when 'r'
191
227
  @break = true
@@ -305,13 +341,15 @@ loop do # OUTER LOOP - (catching refreshes via 'r')
305
341
  @w_b.fg, @w_b.bg, @w_b.attr = 231, 238, 0
306
342
  @w_l.fg, @w_l.bg, @w_l.attr = 46, 234, 0
307
343
  @w_r.fg, @w_r.bg, @w_r.attr = 202, 235, 0
344
+ @w_t.fill
308
345
  loop do # INNER, CORE LOOP
309
- @w_t.fill; @w_b.fill; @w_l.fill; @w_r.fill
310
-
346
+ @w_b.fill; @w_l.fill; @w_r.fill
311
347
  # Example code to write to the panes in various ways
312
- @w_t.p("Top window")
313
- @w_b.p("Bottom window")
348
+ @w_t.p("Top window") unless @w_t.update == false
349
+ @w_b.p("Bottom window - try pressing '?'")
350
+ @w_l.setpos(@w_l.maxy/2, @w_l.maxx/2-6)
314
351
  @w_l.p(196,182,Curses::A_BOLD,"Left window")
352
+ @w_r.setpos(@w_r.maxy/2, @w_r.maxx/2-7)
315
353
  @w_r.p("Right window")
316
354
 
317
355
  # Top window (info line)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: curses-extension
3
3
  version: !ruby/object:Gem::Version
4
- version: '2.1'
4
+ version: '2.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geir Isene
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-07 00:00:00.000000000 Z
11
+ date: 2023-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: curses
@@ -35,7 +35,8 @@ description: 'The Ruby curses library is sorely lacking some important features.
35
35
  terminal curses applications in Ruby. See the Github page for information on what
36
36
  properties and functions are included: https://github.com/isene/Ruby-Curses-Class-Extension.
37
37
  The curses_template.rb is also installed in the lib directory and serves as the
38
- basis for my own curses applications. New in 2.1: Simplified'
38
+ basis for my own curses applications. New in 2.2: Added the method `frame` and made
39
+ the curses-template.rb more informative and sexy'
39
40
  email: g@isene.com
40
41
  executables: []
41
42
  extensions: []