rcurses 3.0 → 3.1

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rcurses/pane.rb +70 -53
  3. data/lib/rcurses.rb +1 -1
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 706550c12ee9965c33b958c6ce4ec5cb0aa12fb37196b4dd623cea43b982528f
4
- data.tar.gz: 17e20e12a50a34f2d36069041b3f1dfbedbf27ab7dddf8f3681fd8b289e6fe00
3
+ metadata.gz: 07bf042dcaa49e65d87441e77e32cb04f6d8738abe081979bc8589966b2ec65f
4
+ data.tar.gz: 234ad19f3e79216d0dd0561e2cfbb43318d7f308c596ab2a847eeda6fefe9563
5
5
  SHA512:
6
- metadata.gz: 68fe3d1c80b12fae7ad09b49efdec40a889bf36f6d269e68ab09971bf9f057874ac122cff4d1c08c9b0673e13e65857c98e9e888977138a1c51a385544520901
7
- data.tar.gz: aa6d784ef29afbb0b5e09ada56b2caadf2b474e0bf909eaa92dc161f88d6d1e6c33424d3c1ed2c407148d9ce502bd5563bf1eb1118e3b2a7a15444df18494a89
6
+ metadata.gz: d8d764879351431d6057dd1fa573ca2bba3134c207c8c4f1ad4f511d1d12ca93c392e163401a9592c18b6af48f590d7509e510592f4191bf156c89c1c89d644a
7
+ data.tar.gz: 8ffe0551b49238aa84ef6811d140c398a96736c92f77afd10afd0116431d6856d6472f619345b96827391a9b83d152965704f586119bcd56410b626100da71e1
data/lib/rcurses/pane.rb CHANGED
@@ -8,22 +8,25 @@ module Rcurses
8
8
  attr_accessor :moreup, :moredown
9
9
 
10
10
  def initialize(x = 1, y = 1, w = 1, h = 1, fg = nil, bg = nil)
11
+ @max_h, @max_w = IO.console.winsize
11
12
  @x = x
12
13
  @y = y
13
14
  @w = w
14
15
  @h = h
15
16
  @fg, @bg = fg, bg
16
- @text = "" # Initialize text variable
17
- @align = "l" # Default alignment
18
- @scroll = true # Enable scroll indicators
19
- @prompt = "" # Prompt for editline
20
- @ix = 0 # Starting text line index
21
- @max_h, @max_w = IO.console.winsize
17
+ @text = "" # Initialize text variable
18
+ @align = "l" # Default alignment
19
+ @scroll = true # Enable scroll indicators
20
+ @prompt = "" # Prompt for editline
21
+ @ix = 0 # Starting text line index
22
+ @prev_frame = nil # Holds the previously rendered frame (array of lines)
23
+ @line = 0 # For cursor tracking during editing:
24
+ @pos = 0 # For cursor tracking during editing:
22
25
  end
23
26
 
24
- def move(x, y)
25
- @x += x
26
- @y += y
27
+ def move(dx, dy)
28
+ @x += dx
29
+ @y += dy
27
30
  refresh
28
31
  end
29
32
 
@@ -74,11 +77,13 @@ module Rcurses
74
77
  refresh
75
78
  end
76
79
 
77
- # Optimized refresh using double buffering to minimize flicker.
80
+ # Diff-based refresh that minimizes flicker.
81
+ # Building a frame (an array of lines) that includes borders (if enabled).
82
+ # Content lines are wrapped in vertical border characters when @border is true.
78
83
  def refresh(cont = @text)
79
84
  @max_h, @max_w = IO.console.winsize
80
85
 
81
- # Adjust pane dimensions (unchanged logic)
86
+ # Adjust pane dimensions and positions.
82
87
  if @border
83
88
  @w = @max_w - 2 if @w > @max_w - 2
84
89
  @h = @max_h - 2 if @h > @max_h - 2
@@ -94,21 +99,26 @@ module Rcurses
94
99
  # Save current cursor position.
95
100
  o_row, o_col = pos
96
101
 
97
- # Hide cursor and switch to alternate screen buffer (if desired). Hide it any case and use Curses.show to bring it back.
98
- # Note: The alternate screen buffer means your program’s output won’t mix with the normal terminal content.
99
- STDOUT.print "\e[?25l" # hide cursor
100
- # Uncomment the next line to use the alternate screen buffer.
101
- #STDOUT.print "\e[?1049h"
102
+ STDOUT.print "\e[?25l" # Hide cursor
102
103
 
103
- buf = ""
104
- buf << "\e[#{@y};#{@x}H" # Move cursor to start of pane.
105
104
  fmt = [@fg, @bg].compact.join(',')
106
105
  @txt = cont.split("\n")
107
106
  @txt = textformat(cont) if @txt.any? { |line| line.pure.length >= @w }
108
107
  @ix = @txt.length - 1 if @ix > @txt.length - 1
109
108
  @ix = 0 if @ix < 0
110
109
 
111
- @h.times do |i|
110
+ # Build the new frame as an array of strings.
111
+ new_frame = []
112
+ if @border
113
+ # Top border spans (@w + 2) characters.
114
+ top_border = ("┌" + "─" * @w + "┐").c(fmt)
115
+ new_frame << top_border
116
+ end
117
+
118
+ # Build content lines.
119
+ content_rows = @h
120
+ content_rows.times do |i|
121
+ line_str = ""
112
122
  l = @ix + i
113
123
  if @txt[l].to_s != ""
114
124
  pl = @w - @txt[l].pure.length
@@ -116,53 +126,60 @@ module Rcurses
116
126
  hl = pl / 2
117
127
  case @align
118
128
  when "l"
119
- buf << @txt[l].c(fmt) << " ".c(fmt) * pl
129
+ line_str = @txt[l].c(fmt) + " ".c(fmt) * pl
120
130
  when "r"
121
- buf << " ".c(fmt) * pl << @txt[l].c(fmt)
131
+ line_str = " ".c(fmt) * pl + @txt[l].c(fmt)
122
132
  when "c"
123
- buf << " ".c(fmt) * hl << @txt[l].c(fmt) << " ".c(fmt) * (pl - hl)
133
+ line_str = " ".c(fmt) * hl + @txt[l].c(fmt) + " ".c(fmt) * (pl - hl)
124
134
  end
125
135
  else
126
- buf << " ".c(fmt) * @w
136
+ line_str = " ".c(fmt) * @w
127
137
  end
128
- buf << "\e[#{@y + i + 1};#{@x}H" # Next line.
129
- end
130
138
 
131
- # Draw scroll markers.
132
- if @ix > 0 and @scroll
133
- buf << "\e[#{@y};#{@x + @w - 1}H" << "∆".c(fmt)
134
- @moreup = true
135
- else
136
- @moreup = false
137
- end
139
+ # If border is enabled, add vertical border characters.
140
+ if @border
141
+ line_str = "│" + line_str + ""
142
+ end
138
143
 
139
- if @txt.length - @ix > @h and @scroll
140
- buf << "\e[#{@y + @h - 1};#{@x + @w - 1}H" << "∇".c(fmt)
141
- @moredown = true
142
- else
143
- @moredown = false
144
+ # Add scroll markers (overwrite the last character) if needed.
145
+ if i == 0 and @ix > 0 and @scroll
146
+ line_str[-1] = "∆".c(fmt)
147
+ @moreup = true
148
+ elsif i == content_rows - 1 and @txt.length - @ix > @h and @scroll
149
+ line_str[-1] = "∇".c(fmt)
150
+ @moredown = true
151
+ else
152
+ @moreup = false
153
+ @moredown = false
154
+ end
155
+ new_frame << line_str
144
156
  end
145
157
 
146
- # Draw border if enabled.
147
158
  if @border
148
- buf << "\e[#{@y - 1};#{@x - 1}H" << ("┌" + "─" * @w + "┐").c(fmt)
149
- @h.times do |i|
150
- buf << "\e[#{@y + i};#{@x - 1}H" << "│".c(fmt)
151
- buf << "\e[#{@y + i};#{@x + @w}H" << "│".c(fmt)
152
- end
153
- buf << "\e[#{@y + @h};#{@x - 1}H" << ("└" + "─" * @w + "┘").c(fmt)
159
+ # Bottom border.
160
+ bottom_border = ("└" + "─" * @w + "┘").c(fmt)
161
+ new_frame << bottom_border
154
162
  end
155
163
 
156
- # Restore original cursor position.
157
- buf << "\e[#{o_row};#{o_col}H"
158
-
159
- # Print the whole buffer at once.
160
- print buf
164
+ # Diff-based update: update only lines that changed.
165
+ diff_buf = ""
166
+ new_frame.each_with_index do |line, i|
167
+ # Determine row number:
168
+ row_num = @border ? (@y - 1 + i) : (@y + i)
169
+ # When border is enabled, all lines (including content) start at column (@x - 1)
170
+ col_num = @border ? (@x - 1) : @x
171
+ if @prev_frame.nil? || @prev_frame[i] != line ||
172
+ (@border && (i == 0 || i == new_frame.size - 1))
173
+ diff_buf << "\e[#{row_num};#{col_num}H" << line
174
+ end
175
+ end
161
176
 
162
- # Uncomment the next line to switched to the alternate screen buffer.
163
- #STDOUT.print "\e[?1049l"
177
+ diff_buf << "\e[#{o_row};#{o_col}H"
178
+ print diff_buf
179
+ #STDOUT.print "\e[?25h" # Show cursor - but use Cursor.show instead if needed
164
180
 
165
- @txt
181
+ @prev_frame = new_frame
182
+ new_frame.join("\n")
166
183
  end
167
184
 
168
185
  def textformat(cont)
@@ -439,7 +456,7 @@ module Rcurses
439
456
  def calculate_posx
440
457
  total_length = 0
441
458
  (@ix + @line).times do |i|
442
- total_length += @txt[i].pure.length + 1 # +1 for the newline
459
+ total_length += @txt[i].pure.length + 1 # +1 for newline
443
460
  end
444
461
  total_length += @pos
445
462
  total_length
data/lib/rcurses.rb CHANGED
@@ -5,7 +5,7 @@
5
5
  # Web_site: http://isene.com/
6
6
  # Github: https://github.com/isene/rcurses
7
7
  # License: Public domain
8
- # Version: 3.0: Flicker reduction by double screen buffer
8
+ # Version: 3.1: Flicker reduction by diff rendering
9
9
 
10
10
  require 'io/console' # Basic gem for rcurses
11
11
  require 'io/wait' # stdin handling
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rcurses
3
3
  version: !ruby/object:Gem::Version
4
- version: '3.0'
4
+ version: '3.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geir Isene
@@ -29,8 +29,8 @@ description: 'Create curses applications for the terminal easier than ever. Crea
29
29
  up text (in panes or anywhere in the terminal) in bold, italic, underline, reverse
30
30
  color, blink and in any 256 terminal colors for foreground and background. Use a
31
31
  simple editor to let users edit text in panes. Left, right or center align text
32
- in panes. Cursor movement around the terminal. New in 3.0: Flicker reduction by
33
- double screen buffer.'
32
+ in panes. Cursor movement around the terminal. New in 3.1: Flicker reduction by
33
+ diff rendering.'
34
34
  email: g@isene.com
35
35
  executables: []
36
36
  extensions: []