awesome-gocui / gocui

Minimalist Go package aimed at creating Console User Interfaces.
BSD 3-Clause "New" or "Revised" License
344 stars 39 forks source link

Problem with SetCursor() used by mouse #81

Closed dankox closed 3 years ago

dankox commented 3 years ago

Describe the bug SetCursor() function working on top of the view buffer lines can cause panics when mouse control is turned on.

To Reproduce Steps to reproduce the behavior:

  1. clone branch from PR #80
  2. run go run _examples/mouse.go
  3. move cursor in to the spot in view which doesn't have text (basically just moving it at the end of the highlighted line does the trick)

Expected behavior It shouldn't panic.

Additional context SetCursor function was used before from outside "world" point of view with just knowledge of the View dimension. This is easy to deal with as all terminals/consoles have specific dimension, Views are setup according to it, and application can points to it.
If it's used with the knowledge of the view buffer content, programs need to be updated to consider it and not point to the position which would be outside of this buffer. Even in gocui mouse position is used relative to the terminal window and View and registering the location without any knowledge of the buffer content.

Proposed solution could be, that there will be another two functions SetCursorInBuffer, CursorInBuffer, which would act as the current SetCursor and Cursor and the original functions would be enhanced to translate the position from buffer position to "real" position on the screen.
This way SetCursor would use original View dimension to check for invalid point and the new functions could be used in edit.go to work with the buffer as it is right now.

dankox commented 3 years ago

@mjarkk Again with another problem with SetCursor :) I wrote down how I think it could work. I didn't start to write down the fix, because I wanted to confirm if you are ok with this way, or if you have better suggestion.
This problem is most apparent with mouse control, but I could imagine other application using gocui to hit similar problem too.

glvr182 commented 3 years ago

pinging @mjarkk

mjarkk commented 3 years ago

Sorry for the late reply last few week have been busy.

Anyway i should definitely fix that panic that's never good.
About the logic behind SetCursor i'm not sure if we should do that i see the issue of applications that have to change now because of this change but i don't think that are manny.
Besides in all GUI libraries the cursor position is based on the buffer so why not do that here? I cannot think personally of any possibilities where a TUI library's cursor handling is an exception to the rule?

dankox commented 3 years ago

I see... in that case I will leave it to you. I can't really think of any other problem than the mouse problem. But mouse is in gocui control, so it shouldn't be a problem to fix it.