-
-
Notifications
You must be signed in to change notification settings - Fork 452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade lengths of RichString to size_t
& many related changes.
#1592
base: main
Are you sure you want to change the base?
Changes from 16 commits
233c953
160c066
f83ab04
56654dd
9a395cb
9eb2343
473c847
7ff906c
bd27b7e
1715ad0
4ef38cb
fd54355
39c14cd
521e563
78184a1
e101339
9af21a7
4b32769
5d61426
22deed3
1b8f1ab
005c9b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ in the source distribution for its full text. | |
|
||
#include <assert.h> | ||
#include <ctype.h> | ||
#include <limits.h> | ||
#include <stdbool.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
@@ -79,7 +80,16 @@ void Panel_done(Panel* this) { | |
|
||
void Panel_setCursorToSelection(Panel* this) { | ||
this->cursorY = this->y + this->selected - this->scrollV + 1; | ||
this->cursorX = this->x + this->selectedLen - this->scrollH; | ||
|
||
int offset = this->w; | ||
if (this->selectedLen - this->scrollH < (unsigned int)this->w) { | ||
offset = (int)(this->selectedLen - this->scrollH); | ||
} | ||
if ((unsigned int)offset <= INT_MAX - (unsigned int)this->x) { | ||
this->cursorX = this->x + offset; | ||
} else { | ||
this->cursorX = INT_MAX; | ||
} | ||
} | ||
|
||
void Panel_setSelectionColor(Panel* this, ColorElements colorId) { | ||
|
@@ -222,7 +232,7 @@ void Panel_draw(Panel* this, bool force_redraw, bool focus, bool highlightSelect | |
assert (this != NULL); | ||
|
||
int size = Vector_size(this->items); | ||
int scrollH = this->scrollH; | ||
size_t scrollH = this->scrollH; | ||
int y = this->y; | ||
int x = this->x; | ||
int h = this->h; | ||
|
@@ -239,13 +249,12 @@ void Panel_draw(Panel* this, bool force_redraw, bool focus, bool highlightSelect | |
else | ||
RichString_setAttr(&this->header, header_attr); | ||
} | ||
int headerLen = RichString_sizeVal(this->header); | ||
size_t headerLen = RichString_sizeVal(this->header); | ||
if (headerLen > 0) { | ||
attrset(header_attr); | ||
mvhline(y, x, ' ', this->w); | ||
if (scrollH < headerLen) { | ||
RichString_printoffnVal(this->header, y, x, scrollH, | ||
MINIMUM(headerLen - scrollH, this->w)); | ||
RichString_printoffnVal(this->header, y, x, scrollH, this->w); | ||
} | ||
attrset(CRT_colors[RESET_COLOR]); | ||
y++; | ||
|
@@ -282,8 +291,7 @@ void Panel_draw(Panel* this, bool force_redraw, bool focus, bool highlightSelect | |
const Object* itemObj = Vector_get(this->items, i); | ||
RichString_begin(item); | ||
Object_display(itemObj, &item); | ||
int itemLen = RichString_sizeVal(item); | ||
int amt = MINIMUM(itemLen - scrollH, this->w); | ||
size_t itemLen = RichString_sizeVal(item); | ||
if (highlightSelected && i == this->selected) { | ||
item.highlightAttr = selectionColor; | ||
} | ||
|
@@ -293,8 +301,8 @@ void Panel_draw(Panel* this, bool force_redraw, bool focus, bool highlightSelect | |
this->selectedLen = itemLen; | ||
} | ||
mvhline(y + line, x, ' ', this->w); | ||
if (amt > 0) | ||
RichString_printoffnVal(item, y + line, x, scrollH, amt); | ||
if (scrollH < itemLen) | ||
RichString_printoffnVal(item, y + line, x, scrollH, this->w); | ||
if (item.highlightAttr) | ||
attrset(CRT_colors[RESET_COLOR]); | ||
RichString_delete(&item); | ||
|
@@ -309,22 +317,22 @@ void Panel_draw(Panel* this, bool force_redraw, bool focus, bool highlightSelect | |
const Object* oldObj = Vector_get(this->items, this->oldSelected); | ||
RichString_begin(old); | ||
Object_display(oldObj, &old); | ||
int oldLen = RichString_sizeVal(old); | ||
size_t oldLen = RichString_sizeVal(old); | ||
const Object* newObj = Vector_get(this->items, this->selected); | ||
RichString_begin(new); | ||
Object_display(newObj, &new); | ||
int newLen = RichString_sizeVal(new); | ||
size_t newLen = RichString_sizeVal(new); | ||
this->selectedLen = newLen; | ||
mvhline(y + this->oldSelected - first, x + 0, ' ', this->w); | ||
if (scrollH < oldLen) | ||
RichString_printoffnVal(old, y + this->oldSelected - first, x, | ||
scrollH, MINIMUM(oldLen - scrollH, this->w)); | ||
scrollH, this->w); | ||
attrset(selectionColor); | ||
mvhline(y + this->selected - first, x + 0, ' ', this->w); | ||
RichString_setAttr(&new, selectionColor); | ||
if (scrollH < newLen) | ||
RichString_printoffnVal(new, y + this->selected - first, x, | ||
scrollH, MINIMUM(newLen - scrollH, this->w)); | ||
scrollH, this->w); | ||
attrset(CRT_colors[RESET_COLOR]); | ||
RichString_delete(&new); | ||
RichString_delete(&old); | ||
|
@@ -378,7 +386,11 @@ bool Panel_onKey(Panel* this, int key) { | |
case KEY_LEFT: | ||
case KEY_CTRL('B'): | ||
if (this->scrollH > 0) { | ||
this->scrollH -= MAXIMUM(CRT_scrollHAmount, 0); | ||
if (this->scrollH > CRT_scrollHAmount) { | ||
this->scrollH = this->scrollH - CRT_scrollHAmount; | ||
} else { | ||
this->scrollH = 0; | ||
} | ||
Comment on lines
+389
to
+393
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to self: This may be changed to a |
||
this->needsRedraw = true; | ||
} | ||
break; | ||
|
@@ -421,7 +433,10 @@ bool Panel_onKey(Panel* this, int key) { | |
|
||
case KEY_CTRL('E'): | ||
case '$': | ||
this->scrollH = MAXIMUM(this->selectedLen - this->w, 0); | ||
this->scrollH = 0; | ||
if (this->selectedLen > (unsigned int)this->w) { | ||
this->scrollH = this->selectedLen - (unsigned int)this->w; | ||
} | ||
this->needsRedraw = true; | ||
break; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ in the source distribution for its full text. | |
|
||
#include <assert.h> | ||
#include <stdbool.h> | ||
#include <stddef.h> | ||
|
||
#include "CRT.h" | ||
#include "FunctionBar.h" | ||
|
@@ -67,10 +68,10 @@ struct Panel_ { | |
Vector* items; | ||
int selected; | ||
int oldSelected; | ||
int selectedLen; | ||
size_t selectedLen; | ||
void* eventHandlerState; | ||
int scrollV; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency, this would need to change to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was considering that changing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Capping the internal value is independent from the data type used (to some extend). Also, the marked line is about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I was considering is setting the data type of |
||
int scrollH; | ||
size_t scrollH; | ||
bool needsRedraw; | ||
bool cursorOn; | ||
bool wasFocus; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the second cast also be
size_t
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BenBE Let me explain a bit:
signed int
cast tosize_t
: Perform a sign extension.unsigned int
cast tosize_t
: Perform a zero extension.When I made this patch, I made a personal rule that, if a zero extension would suffice, I would avoid sign extension. This also applies to the noisy-as-you-called-it changes about me casting the
xSnprintf
results tosize_t
.