Skip to content

Conversation

GinjaNinja32
Copy link
Contributor

This is mostly a rebase of this patch, with a few bugfixes (a segfault in gui_entry_transpose_words, and a pointer-to-int assignment).

Allows scripts to specify that ranges of characters in the input bar should be rendered in specific colors; the original intent for this was to implement a spellchecker, my use is to colorise nicks in the input bar.

Example script using this, mostly based on my rewritten colorize_nicks.pl: https://thanatos.gn32.uk/f/colorise_input.pl (I'll likely clean up the code a little and submit for scripts.irssi.org, assuming this PR gets merged)

term_set_color(root_window, ATTR_RESET);
term_move(root_window, xpos, entry->ypos);

for (i = entry->scrstart + pos; i < entry->text_len; i++) {
col = entry->scrstart + pos < entry->text_len ?
entry->colors + entry->scrstart + pos : 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/0/NULL/

@@ -266,14 +274,19 @@ static void gui_entry_draw_from(GUI_ENTRY_REC *entry, int pos)
if (xpos > end_xpos)
break;

if (*col != color) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If col is set to NULL above you get a NPE.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the intention was that col is only set to NULL if the loop won't run.

Perhaps this should instead use entry->colors[i], since col is equal to entry->colors + i at all points during the loop?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's much better!

@@ -505,6 +522,10 @@ void gui_entry_insert_text(GUI_ENTRY_REC *entry, const char *str)
}
}

for(i = 0; i < len; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after for.

@@ -700,6 +726,9 @@ void gui_entry_erase(GUI_ENTRY_REC *entry, int size, CUTBUFFER_UPDATE_OP update_
g_memmove(entry->text + entry->pos - size, entry->text + entry->pos,
(entry->text_len-entry->pos+1) * sizeof(unichar));

g_memmove(entry->colors + entry->pos - size, entry->colors + entry->pos,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The memmove above has a +1 that's missing here, why is that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure. This is straight from the original patch.

Is entry->text NULL-terminated, and entry->colors implicitly has the same number of elements?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's it, sorry for the noise.

(entry->text_len-entry->pos-size+1) * sizeof(unichar));
(entry->text_len-entry->pos-size+1) * sizeof(unichar));

g_memmove(entry->colors + entry->pos, entry->colors + entry->pos + size,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto as before.

g_free(first_color);
g_free(sep_color);
g_free(second_color);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stray newline.

for (i = pos; i < end; i++) {
if (entry->colors[i] != color) {
entry->colors[i] = color;
update = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/1/TRUE/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume FALSE on L1109 too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.

@@ -700,6 +726,9 @@ void gui_entry_erase(GUI_ENTRY_REC *entry, int size, CUTBUFFER_UPDATE_OP update_
g_memmove(entry->text + entry->pos - size, entry->text + entry->pos,
(entry->text_len-entry->pos+1) * sizeof(unichar));

g_memmove(entry->colors + entry->pos - size, entry->colors + entry->pos,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's it, sorry for the noise.

for (i = pos; i < end; i++) {
if (entry->colors[i] != color) {
entry->colors[i] = color;
update = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.

@@ -266,14 +274,19 @@ static void gui_entry_draw_from(GUI_ENTRY_REC *entry, int pos)
if (xpos > end_xpos)
break;

if (*col != color) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's much better!

@@ -1042,3 +1100,31 @@ void gui_entry_redraw(GUI_ENTRY_REC *entry)
gui_entry_fix_cursor(entry);
gui_entry_draw(entry);
}

void gui_entry_set_color(GUI_ENTRY_REC *entry, int pos, int len, int color)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add a couple of checks to make sure pos and len are >= 0 to avoid some potential memory corruption bugs due to careless scripts.

@@ -1042,3 +1103,31 @@ void gui_entry_redraw(GUI_ENTRY_REC *entry)
gui_entry_fix_cursor(entry);
gui_entry_draw(entry);
}

void gui_entry_set_color(GUI_ENTRY_REC *entry, int pos, int len, int color)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add a couple of checks to make sure pos and len are >= 0 to avoid some potential memory corruption bugs due to careless scripts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 8326588

@ailin-nemui
Copy link
Contributor

seems mostly ok, but I don't like having to use the internal colour attributes from XS -- maybe we can add in some functions to do colour code conversions between \004, % and this. And another thing: it doesn't do 24bit "true colour" (api is term_set_color2, didn't exist back then)

The more general question is if we like this implementation. Alternative would be to have in-line colours like in print text / gui prompt. But this code here is much simpler.

In general I think it would be a nice to have feature for your and the other use case

@LemonBoy
Copy link
Member

maybe we can add in some functions to do colour code conversions between \004, % and this.

What if we reuse the same parsing logic that's used for themes formats? It's not that good but at least spares us from writing another parser and having the script writers learn a new set of specifiers.

Alternative would be to have in-line colours

The principle of separation between data and metadata applies here.
A script may as well render the inline colours though, that's an interesting idea.

other use case

I'm using it to turn the whole prompt red whenever there's a cmdchar with leading rubbish, no more accidental commands for me 😃

@ailin-nemui
Copy link
Contributor

I'm using it to turn the whole prompt red whenever there's a cmdchar with leading rubbish, no more accidental commands for me

if we had something more generic like in line colours then it could offer more flexibility like also in line altering of the actual displayed text. think of a blinking "warning" appended to the prompt or an in line display of spelling suggestions after the unrecognised word

@GinjaNinja32
Copy link
Contributor Author

I'm not entirely sure how inline colors would work here.

Using format codes instead of ints sounds good, though. get_nick_color2 appears to return \004-style codes, so it'd be good if it accepts those - perhaps a string containing either a \cD.. code or a %#/%X## code?

I have no idea how I'd implement that, but I can take a look and poke around IRC and see if I can put something together.

@ailin-nemui
Copy link
Contributor

just conceptually speaking, one way to think about inline colours would be to take the input

 /whois xxx

then call a function (e.g. XS) which would change it to

[NOT COMMAND!]  /whois xxx

and that's what would then be visible. This would demand the callback to transform "input" into "displayed input" to be called at basically every change to the prompt

colours would then be realised by "returning"

%R /whois xxx

from the signal handler. There /should/ be some methods in irssi (which may need to be moved to some util/misc function) that turn %codes into the appropriate attributes

@ailin-nemui
Copy link
Contributor

todo: change XS to work on %-codes (maybe exposing some functions for \004 conversion at the same time)

@ailin-nemui
Copy link
Contributor

first version, still some errors, feel free to play with it and fix the remaining bugs

@ailin-nemui ailin-nemui force-pushed the colorful-input branch 3 times, most recently from 99e2257 to 3e4e52b Compare January 18, 2018 22:27
@ailin-nemui
Copy link
Contributor

Irssi::gui_input_clear_extents(pos, len)
Irssi::gui_input_get_extent(pos)
Irssi::gui_input_set_extents(pos, len, left, right)

@ailin-nemui
Copy link
Contributor

@ailin-nemui
Copy link
Contributor

@GinjaNinja32 I changed the api again slightly,extent -> extents. hope you can test this again and discuss any usability issues that we may need to fix before merge

@ailin-nemui
Copy link
Contributor

@irssi/developers I think we should merge this

@ailin-nemui ailin-nemui added the auto-merge This PR is scheduled for merge if no further comments are opened label Feb 2, 2018
@ailin-nemui ailin-nemui merged commit 8183180 into irssi:master Feb 5, 2018
@ailin-nemui ailin-nemui added this to the 1.2.0 milestone Jun 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-merge This PR is scheduled for merge if no further comments are opened needs review needs testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants