| 1 | package gui |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/isacikgoz/gitbatch/internal/git" |
| 7 | "github.com/jroimartin/gocui" |
| 8 | ) |
| 9 | |
| 10 | // listens the event -> "branch.updated" |
| 11 | func (gui *Gui) branchUpdated(event *git.RepositoryEvent) error { |
| 12 | gui.g.Update(func(g *gocui.Gui) error { |
| 13 | return gui.renderCommits(gui.getSelectedRepository()) |
| 14 | }) |
| 15 | return nil |
| 16 | } |
| 17 | |
| 18 | // moves the cursor downwards for the main view and if it goes to bottom it |
| 19 | // prevents from going further |
| 20 | func (gui *Gui) commitCursorDown(g *gocui.Gui, v *gocui.View) error { |
| 21 | if v != nil { |
| 22 | _, cy := v.Cursor() |
| 23 | _, oy := v.Origin() |
| 24 | ly := len(v.BufferLines()) - 1 |
| 25 | |
| 26 | // if we are at the end we just return |
| 27 | if cy+oy == ly-1 { |
| 28 | return nil |
| 29 | } |
| 30 | v.EditDelete(true) |
| 31 | pos := cy + oy + 1 |
| 32 | _ = adjustAnchor(pos, ly, v) |
| 33 | if err := gui.commitStats(pos); err != nil { |
| 34 | return err |
| 35 | } |
| 36 | } |
| 37 | return nil |
| 38 | } |
| 39 | |
| 40 | // moves the cursor upwards for the main view |
| 41 | func (gui *Gui) commitCursorUp(g *gocui.Gui, v *gocui.View) error { |
| 42 | if v != nil { |
| 43 | _, oy := v.Origin() |
| 44 | _, cy := v.Cursor() |
| 45 | ly := len(v.BufferLines()) - 1 |
| 46 | v.EditDelete(true) |
| 47 | pos := cy + oy - 1 |
| 48 | _ = adjustAnchor(pos, ly, v) |
| 49 | if pos >= 0 { |
| 50 | if err := gui.commitStats(cy + oy - 1); err != nil { |
| 51 | return err |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | return nil |
| 56 | } |
| 57 | |
| 58 | // updates the commitsview for given entity |
| 59 | func (gui *Gui) renderCommits(r *git.Repository) error { |
| 60 | v, err := gui.g.View(commitViewFeature.Name) |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | v.Clear() |
| 65 | cs := r.State.Branch.Commits |
| 66 | // bc := r.State.Branch.State.Commit |
| 67 | si := 0 |
| 68 | fmt.Fprintln(v, " "+yellow.Sprint("*******")+" "+yellow.Sprint("Current State")) |
| 69 | |
| 70 | for _, c := range cs { |
| 71 | // if c.Hash == bc.Hash { |
| 72 | // si = i |
| 73 | // fmt.Fprintln(v, ws+commitLabel(c, false)) |
| 74 | // continue |
| 75 | // } |
| 76 | |
| 77 | fmt.Fprintln(v, tab+commitLabel(c, false)) |
| 78 | } |
| 79 | _ = adjustAnchor(si, len(cs), v) |
| 80 | return nil |
| 81 | } |
| 82 | |
| 83 | // moves cursor down for a page size |
| 84 | func (gui *Gui) commitPageDown(g *gocui.Gui, v *gocui.View) error { |
| 85 | if v != nil { |
| 86 | _, oy := v.Origin() |
| 87 | _, vy := v.Size() |
| 88 | _, cy := v.Cursor() |
| 89 | lr := len(v.BufferLines()) |
| 90 | if lr < vy { |
| 91 | return nil |
| 92 | } |
| 93 | v.EditDelete(true) |
| 94 | |
| 95 | _ = adjustAnchor(oy+cy+vy-1, lr, v) |
| 96 | // if err := gui.commitStats(oy + cy + vy - 1); err != nil { |
| 97 | // return err |
| 98 | // } |
| 99 | |
| 100 | } |
| 101 | return nil |
| 102 | } |
| 103 | |
| 104 | // moves cursor to the top |
| 105 | func (gui *Gui) commitCursorTop(g *gocui.Gui, v *gocui.View) error { |
| 106 | if v != nil { |
| 107 | |
| 108 | v.EditDelete(true) |
| 109 | lr := len(v.BufferLines()) |
| 110 | |
| 111 | _ = adjustAnchor(0, lr, v) |
| 112 | if err := gui.commitStats(0); err != nil { |
| 113 | return err |
| 114 | } |
| 115 | } |
| 116 | return nil |
| 117 | } |
| 118 | |
| 119 | // moves cursor up for a page size |
| 120 | func (gui *Gui) commitPageUp(g *gocui.Gui, v *gocui.View) error { |
| 121 | if v != nil { |
| 122 | _, oy := v.Origin() |
| 123 | _, cy := v.Cursor() |
| 124 | _, vy := v.Size() |
| 125 | lr := len(v.BufferLines()) |
| 126 | v.EditDelete(true) |
| 127 | _ = adjustAnchor(oy+cy-vy+1, lr, v) |
| 128 | // if err := gui.commitStats(oy + cy - vy + 1); err != nil { |
| 129 | // return err |
| 130 | // } |
| 131 | } |
| 132 | return nil |
| 133 | } |