You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For a ,x, and R to work properly, they must have different behavior when the cursor is on the final character of the line. I implement this with:
// True if the textarea cursor is at the end of its given line
fn is_before_line_end(textarea: &TextArea<'_>) -> bool {
let (cursor_line, cursor_char) = textarea.cursor();
let lines = textarea.lines();
let line = &lines[cursor_line];
let line_length = line.chars().count(); // FIXME: Not acceptable-- O(N)
cursor_char < line_length
}
The problem is that when retrieving the cursor from the library, the offset is in characters², but when accessing the strings in the buffer, you cannot random-access a character index. Your only option is to use the chars() iterator, which will become slow for very long lines.
My proposal is: In addition to .cursor(), offer a .cursor_bytes(), which gives a (line_idx, byte_idx) position instead of (line_idx, char_idx). This would be practical for two reasons:
It would easily, efficiently, and safely solve my problem, because I could compare the cursor_bytes to line_string.len().
If someone was willing to use unsafe {}, I think they could do some string operations more efficiently even when the cursor is not at the end of the line— and despite the use of unsafe {} it would be safe, because we can trust that tui-textarea will only allow the cursor to stop on a unicode character boundary.
However, what is important to me is solving the problem, there may be other proposals that solve it in a different or better way. What do you think?
¹ The added characters in my patch work correctly in normal mode, but still have some FIXMEs when pressed in visual mode; they do not do the same thing in that case as standard vim.
² This is what the documentation says, and what my testing with multibyte characters confirms 👍, but by the way, can you confirm that the cursor() character index really is chars, not graphemes?
The text was updated successfully, but these errors were encountered:
Half a feature request, half a support request.
I am improving the vim compatibility of this library's "vim.rs" example.. So far I've added arrow keys,
J
,S
,r
, andR
, and I've improved the end-of-line behavior ofa
andx
. I will send a PR soon¹, but I'm hitting a problem:For
a
,x
, andR
to work properly, they must have different behavior when the cursor is on the final character of the line. I implement this with:The problem is that when retrieving the cursor from the library, the offset is in characters², but when accessing the strings in the buffer, you cannot random-access a character index. Your only option is to use the chars() iterator, which will become slow for very long lines.
My proposal is: In addition to
.cursor()
, offer a.cursor_bytes()
, which gives a (line_idx, byte_idx) position instead of (line_idx, char_idx). This would be practical for two reasons:cursor_bytes
toline_string.len()
.unsafe {}
, I think they could do some string operations more efficiently even when the cursor is not at the end of the line— and despite the use ofunsafe {}
it would be safe, because we can trust that tui-textarea will only allow the cursor to stop on a unicode character boundary.However, what is important to me is solving the problem, there may be other proposals that solve it in a different or better way. What do you think?
¹ The added characters in my patch work correctly in normal mode, but still have some FIXMEs when pressed in visual mode; they do not do the same thing in that case as standard vim.
² This is what the documentation says, and what my testing with multibyte characters confirms 👍, but by the way, can you confirm that the
cursor()
character index really is chars, not graphemes?The text was updated successfully, but these errors were encountered: