awesome-gocui / gocui

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

[BUG] EditGoToEndOfLine and EditGoToStartOfLine do not account for origin #65

Closed rmasp98 closed 3 years ago

rmasp98 commented 4 years ago

This is a great project and I am really glad you guys are still supporting it and adding some really useful features. I was fiddling about with my app the recently and I noticed that a couple of functions don't seem to be working properly. Sorry if I have misunderstood anything but I am still fairly new to golang

Both the following functions are part of the view class in edit.go: EditGoToEndOfLine if already on the last line loops over MoveCursor until the cursor no longer changes. However, when it reaches the end of the view, it is the origin that changes not the cursor. Therefore, it does not actually go to the end of the line but the end of the view

EditGoToStartOfLine does much the same in that it sets cursor to zero but does nothing to the origin. This means that if origin is not zero, it will not go back to the start of the line

Steps to reproduce the behavior:

  1. Create a view without wrap enabled and fill it with data (only on single line) at least 10 or so characters longer than the view
  2. Move origin to about midway through line
  3. Bind EditGoToStartOfLine and EditGoToEndOfLine to keys
  4. Flick between "start" and "end" and notice it does not actually reach the end nor beginning

Origin should also be updated within these functions to go to the true start/end of line

I think simple solutions would be:

func (v *View) EditGotoToStartOfLine() {
   _, cursorY := v.Cursor()
   _, originY := v.Origin()
   v.SetCursor(0, cursorY)
   v.SetOrigin(0, originY)
}
func (v *View) EditGotoToEndOfLine() {
   _, cursorY := v.Cursor()
   _, originY := v.Origin()
   if line, err := v.Line(cursorY + originY); err == nil {
      width, _ := v.Size()
      if len(line) < width {
         v.SetCursor(width, cursorY)
      } else {
         v.SetCursor(width-1, cursorY)
         v.SetOrigin(len(line) - (width - 1), originY)
      }
   }
} 
mjarkk commented 3 years ago

Sorry for the late reply,
Can you check if this issues still is a thing in v1.0.0-beta-2
If so i'll see if i can add the code you suggested

mjarkk commented 3 years ago

We've now released v1.0.0 that is fundamentally different and should fix this issue.