curses-extension 2.0 → 2.2

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: 1a1e832808f3f3ffbf4d39755719168b08fbc0c644a7a48840839844b3fca3a4
4
- data.tar.gz: 23e73a5413638273336b211d3274a48bdeb21963b7600f0c731968e2269bb343
3
+ metadata.gz: '04280f3b09961bca34f139e055228c7206b28dcae370c22ae0417dca8fbb5fc2'
4
+ data.tar.gz: 063e86ad7733dd4c5ee83664a59a3a3dbf59e45bdfda09a8efc1bc4e09cfd392
5
5
  SHA512:
6
- metadata.gz: 34e132b85309f493f6b31efb216edd963b32c13dea3b62b3ca56df56d50c6eb89dcc8783f3a33aa9f8dea5f951c82c9d966623ccee8f75ccc0cc9197a3826338
7
- data.tar.gz: 8237d8b29d71c83a1e13d5a4d679ea3fc55cc80cb2d2f635bba070dd29b8ca4bd06039c79b98eaa6df026bdadcb0a4ce87d3a375c125d782ba9c9531ea075e5f
6
+ metadata.gz: 65c963c468c4b9101e59f9208815b58bb4d9f7bb07c34298aa1628a3f783de61cf9de186bed60f6b0246bd1f20be7eb9d4bfbb7cede62e7952e33fa17aa6f1bf
7
+ data.tar.gz: c80f69f856b9a4faacdabf5a3bc8b8f69293b463f1b6f9b308319b98146d7ba6a0516387bb2e65cec4a121bfc96d2b06682851b1fb2b84ef293994e49e170198
data/lib/README.md CHANGED
@@ -1,13 +1,27 @@
1
1
  # Ruby-Curses-Class-Extension
2
2
  Extending the Ruby Curses module with some obvious needed functionality.
3
3
 
4
+ With this extension, you don't need to initiate any color pairs or add text to
5
+ windows with odd syntax. Use any foreground and backround color you see fit
6
+ and let this extension take care of the weird need to pair colors and initiate
7
+ the color pairs. You also get a bunch of extra methods to manipulate curses
8
+ windows.
9
+
10
+ With this extension writing text to windows becomes a breeze. You can set
11
+ default foreground, background and attribute for a window (e.g. `win.fg =
12
+ 124`, `win.bg = 234` and `win.attr = Curses::A_BOLD`) - and you can write text
13
+ to a window with `win.p("Hello World")` where the defaults will be applied.
14
+ You can override the defaults with e.g. `win.p(124, "Hello World")` to write
15
+ 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.
17
+
18
+
4
19
  ## Attributes
5
20
  Attribute | Description
6
21
  --------------------|--------------------------------------------------------
7
22
  fg | Foreground color for window (0-255)
8
23
  bg | Background color for window (0-255)
9
24
  attr | Attributes for window (such as Curses::A_BOLD) - string with "\|" (such as Curses::A_BOLD \| Curses::A_UNDERLINE)
10
- text | Holds the text to be written with the `write`method
11
25
  update | Whether to update the window on the next refresh
12
26
  index | Used to track an index for each window (used to display lists such as the content of a directory, etc.)
13
27
 
@@ -20,12 +34,10 @@ clr_from_cur_line | Clears the rest of the window after the current line
20
34
  fill | Fill window with color as set by :color ( or :bg if not :color is set)
21
35
  fill_to_cur_pos | Fill the window up to the current line
22
36
  fill_from_cur_pos | Fill the rest of the window after the current line
23
- write | Write what is already stored in window.text with the set values for fg, bg and attr
24
- p(text) or puts(test) | Write text to window with fg/bg and attributes (will handle the exceptions if no colors are set)
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)
25
39
  pclr(text) | As `p(text)` but also clears the rest of the window
26
- pa(fg, bg, attr, text) | Write text to window with specified fg, bg and attribute(s)
27
- paclr(text, fg=255, bg=0, attr=0) | As `pa(text)` but also clears the rest of the window
28
- format_text(text) | Format text so that it linebreaks neatly inside window
40
+ format(text) | Format text so that it linebreaks neatly inside window
29
41
 
30
42
  ## Curses template
31
43
  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.
@@ -8,7 +8,7 @@ class Curses::Window # CLASS EXTENSION
8
8
  # self.attr is set for text attributes like Curses::A_BOLD
9
9
  # self.update can be used to indicate if a window should be updated (true/false)
10
10
  # self.index can be used to keep track of the current list item in a window
11
- attr_accessor :fg, :bg, :attr, :text, :update, :index
11
+ attr_accessor :fg, :bg, :attr, :update, :index
12
12
  def self.pair(fg, bg)
13
13
  @p = [[]] if @p == nil
14
14
  fg = fg.to_i; bg = bg.to_i
@@ -67,38 +67,27 @@ class Curses::Window # CLASS EXTENSION
67
67
  self.refresh
68
68
  self.setpos(y, x)
69
69
  end
70
- def write
71
- self.attr = 0 if self.attr == nil
72
- self.bg = 0 if self.bg == nil
73
- self.fg = 255 if self.fg == nil
74
- cp = Curses::Window.pair(self.fg, self.bg)
75
- self.attron(color_pair(cp) | self.attr) { self << self.text }
76
- self.refresh
77
- end
78
- def p(text) # Puts text to window
79
- self.attr = 0 if self.attr == nil
80
- self.bg = 0 if self.bg == nil
81
- self.fg = 255 if self.fg == nil
82
- cp = Curses::Window.pair(self.fg, self.bg)
83
- self.attron(color_pair(cp) | attr) { self << text }
84
- self.refresh
85
- end
86
- def pclr(text) # Puts text to window and clears the rest of the window
87
- self.p(text)
88
- self.clr_from_cur_line
89
- end
90
- def pa(fg = self.fg, bg = self.bg, attr = self.attr, text) # Puts text to window with full set of attributes
70
+ def p(fg = self.fg, bg = self.bg, attr = self.attr, text) # Puts text to window with full set of attributes
71
+ fg = 255 if fg == nil
72
+ bg = 0 if bg == nil
73
+ attr = 0 if attr == nil
91
74
  cp = Curses::Window.pair(fg, bg)
92
75
  self.attron(color_pair(cp) | attr) { self << text }
93
76
  self.refresh
94
77
  end
95
- def paclr(fg = self.fg, bg = self.bg, attr = self.attr, text) # Puts text to window with full set of attributes and clears rest of window
96
- self.pa(fg, bg, attr, text)
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
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
84
+ self.p(fg, bg, attr, text)
97
85
  self.clr_from_cur_line
98
86
  end
99
- def format_text(text) # Format text so that it linebreaks neatly inside window
87
+ def format(text) # Format text so that it linebreaks neatly inside window
100
88
  return "\n" + text.gsub(/(.{1,#{self.maxx}})( +|$\n?)|(.{1,#{self.maxx}})/, "\\1\\3\n")
101
89
  end
102
90
  alias :puts :p
103
91
  end
104
92
 
93
+ # vim: set sw=2 sts=2 et fdm=syntax fdn=2 fcs=fold\:\ :
@@ -40,7 +40,7 @@ class Curses::Window # CLASS EXTENSION
40
40
  # self.attr is set for text attributes like Curses::A_BOLD
41
41
  # self.update can be used to indicate if a window should be updated (true/false)
42
42
  # self.index can be used to keep track of the current list item in a window
43
- attr_accessor :fg, :bg, :attr, :text, :update, :index
43
+ attr_accessor :fg, :bg, :attr, :update, :index
44
44
  def self.pair(fg, bg)
45
45
  @p = [[]] if @p == nil
46
46
  fg = fg.to_i; bg = bg.to_i
@@ -99,36 +99,24 @@ class Curses::Window # CLASS EXTENSION
99
99
  self.refresh
100
100
  self.setpos(y, x)
101
101
  end
102
- def write
103
- self.attr = 0 if self.attr == nil
104
- self.bg = 0 if self.bg == nil
105
- self.fg = 255 if self.fg == nil
106
- cp = Curses::Window.pair(self.fg, self.bg)
107
- self.attron(color_pair(cp) | self.attr) { self << self.text }
108
- self.refresh
109
- end
110
- def p(text) # Puts text to window
111
- self.attr = 0 if self.attr == nil
112
- self.bg = 0 if self.bg == nil
113
- self.fg = 255 if self.fg == nil
114
- cp = Curses::Window.pair(self.fg, self.bg)
102
+ def p(fg = self.fg, bg = self.bg, attr = self.attr, text) # Puts text to window with full set of attributes
103
+ fg = 255 if fg == nil
104
+ bg = 0 if bg == nil
105
+ attr = 0 if attr == nil
106
+ cp = Curses::Window.pair(fg, bg)
115
107
  self.attron(color_pair(cp) | attr) { self << text }
116
108
  self.refresh
117
109
  end
118
- def pclr(text) # Puts text to window and clears the rest of the window
119
- self.p(text)
120
- self.clr_from_cur_line
121
- end
122
- def pa(fg = self.fg, bg = self.bg, attr = self.attr, text) # Puts text to window with full set of attributes
123
- cp = Curses::Window.pair(fg, bg)
124
- self.attron(color_pair(cp) | attr) { self << text }
125
- self.refresh
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)
126
114
  end
127
- def paclr(fg = self.fg, bg = self.bg, attr = self.attr, text) # Puts text to window with full set of attributes and clears rest of window
128
- self.pa(fg, bg, attr, text)
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
116
+ self.p(fg, bg, attr, text)
129
117
  self.clr_from_cur_line
130
118
  end
131
- def format_text(text) # Format text so that it linebreaks neatly inside window
119
+ def format(text) # Format text so that it linebreaks neatly inside window
132
120
  return "\n" + text.gsub(/(.{1,#{self.maxx}})( +|$\n?)|(.{1,#{self.maxx}})/, "\\1\\3\n")
133
121
  end
134
122
  alias :puts :p
@@ -224,9 +212,8 @@ def main_getkey # GET KEY FROM USER
224
212
  @w_r.update = false
225
213
  cmd = w_b_getstr("◆ ", "")
226
214
  begin
227
- @w_r.text = eval(cmd)
228
- #@w_r.fill
229
- @w_r.write
215
+ @w_r.fill
216
+ @w_r.p(eval(cmd))
230
217
  rescue StandardError => e
231
218
  w_b("Error: #{e.inspect}")
232
219
  end
@@ -245,8 +232,7 @@ def w_b(info) # SHOW INFO IN @W_B
245
232
  info = "Choose window: i=IMDB list (+/- to add/remove from My list), g=Genres (+/- to add/remove), m=My list. " if info == nil
246
233
  info = info[1..(@w_b.maxx - 3)] + "…" if info.length + 3 > @w_b.maxx
247
234
  info += " " * (@w_b.maxx - info.length) if info.length < @w_b.maxx
248
- @w_b.text = info
249
- @w_b.write
235
+ @w_b.p(info)
250
236
  @w_b.update = false
251
237
  end
252
238
  def w_b_getstr(pretext, text) # A SIMPLE READLINE-LIKE ROUTINE
@@ -257,9 +243,9 @@ def w_b_getstr(pretext, text) # A SIMPLE READLINE-LIKE ROUTINE
257
243
  chr = ""
258
244
  while chr != "ENTER"
259
245
  @w_b.setpos(0,0)
260
- @w_b.text = pretext + text
261
- @w_b.text += " " * (@w_b.maxx - text.length) if text.length < @w_b.maxx
262
- @w_b.write
246
+ text = pretext + text
247
+ text += " " * (@w_b.maxx - text.length) if text.length < @w_b.maxx
248
+ @w_b.p(text)
263
249
  @w_b.setpos(0,pretext.length + pos)
264
250
  @w_b.refresh
265
251
  chr = getchr
@@ -301,8 +287,7 @@ def w_r_info(info) # SHOW INFO IN THE RIGHT WINDOW
301
287
  begin
302
288
  @w_r.clr
303
289
  @w_r.refresh
304
- @w_r.text = info
305
- @w_r.write
290
+ @w_r.p(info)
306
291
  @w_r.update = false
307
292
  rescue
308
293
  end
@@ -329,12 +314,10 @@ loop do # OUTER LOOP - (catching refreshes via 'r')
329
314
  @w_t.fill; @w_b.fill; @w_l.fill; @w_r.fill
330
315
 
331
316
  # Example code to write to the panes in various ways
332
- @w_t.text = "Top window"
333
- @w_t.write
334
- @w_b.pa("Bottom window")
335
- @w_l.pa(196,182,Curses::A_BOLD,"Left window")
336
- @w_r.text = "Right window"
337
- @w_r.write
317
+ @w_t.p("Top window")
318
+ @w_b.p("Bottom window")
319
+ @w_l.p(196,182,Curses::A_BOLD,"Left window")
320
+ @w_r.p("Right window")
338
321
 
339
322
  # Top window (info line)
340
323
 
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.0'
4
+ version: '2.2'
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-05 00:00:00.000000000 Z
11
+ date: 2023-07-09 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.0: Full rewrite/upgrade'
38
+ basis for my own curses applications. New in 2.2: "Added the method `nl`, an effective
39
+ newline"'
39
40
  email: g@isene.com
40
41
  executables: []
41
42
  extensions: []