| 1 | package gui |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | |
| 7 | "github.com/isacikgoz/gitbatch/internal/command" |
| 8 | "github.com/isacikgoz/gitbatch/internal/git" |
| 9 | "github.com/jroimartin/gocui" |
| 10 | ) |
| 11 | |
| 12 | // initialize stashed items |
| 13 | func (gui *Gui) initStashedView(r *git.Repository) error { |
| 14 | v, err := gui.g.View(stashViewFeature.Name) |
| 15 | if err != nil { |
| 16 | return err |
| 17 | } |
| 18 | v.Clear() |
| 19 | st := r.Stasheds |
| 20 | for _, s := range st { |
| 21 | fmt.Fprintf(v, " %d %s: %s\n", s.StashID, cyan.Sprint(s.BranchName), s.Description) |
| 22 | } |
| 23 | if len(st) > 0 { |
| 24 | _ = adjustAnchor(0, len(st), v) |
| 25 | } |
| 26 | return nil |
| 27 | } |
| 28 | |
| 29 | // moves the cursor downwards for the stash view and if it goes to bottom it |
| 30 | // prevents from going further |
| 31 | func (gui *Gui) stashCursorDown(g *gocui.Gui, v *gocui.View) error { |
| 32 | if v != nil { |
| 33 | _, cy := v.Cursor() |
| 34 | _, oy := v.Origin() |
| 35 | ly := len(v.BufferLines()) - 1 |
| 36 | |
| 37 | // if we are at the end we just return |
| 38 | if cy+oy == ly-1 || ly < 0 { |
| 39 | return nil |
| 40 | } |
| 41 | v.EditDelete(true) |
| 42 | pos := cy + oy + 1 |
| 43 | _ = adjustAnchor(pos, ly, v) |
| 44 | _ = gui.renderStashDiff(pos) |
| 45 | } |
| 46 | return nil |
| 47 | } |
| 48 | |
| 49 | // moves the cursor upwards for the stash view |
| 50 | func (gui *Gui) stashCursorUp(g *gocui.Gui, v *gocui.View) error { |
| 51 | if v != nil { |
| 52 | _, oy := v.Origin() |
| 53 | _, cy := v.Cursor() |
| 54 | ly := len(v.BufferLines()) - 1 |
| 55 | // if we are at the end we just return |
| 56 | if ly < 0 { |
| 57 | return nil |
| 58 | } |
| 59 | v.EditDelete(true) |
| 60 | pos := cy + oy - 1 |
| 61 | _ = adjustAnchor(pos, ly, v) |
| 62 | if pos >= 0 { |
| 63 | _ = gui.renderStashDiff(cy + oy - 1) |
| 64 | } |
| 65 | } |
| 66 | return nil |
| 67 | } |
| 68 | |
| 69 | // open stashed item diff which given with index |
| 70 | func (gui *Gui) renderStashDiff(ix int) error { |
| 71 | r := gui.getSelectedRepository() |
| 72 | st := r.Stasheds |
| 73 | if len(st) <= 0 { |
| 74 | return nil |
| 75 | } |
| 76 | v, err := gui.g.View(dynamicViewFeature.Name) |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | v.Title = string(StashDiffMode) |
| 81 | if err := gui.updateDynamicKeybindings(); err != nil { |
| 82 | return err |
| 83 | } |
| 84 | v.Clear() |
| 85 | d, err := command.StashDiff(r, st[ix].StashID) |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | s := colorizeDiff(d) |
| 90 | fmt.Fprintf(v, "%s", strings.Join(s, "\n")) |
| 91 | return nil |
| 92 | } |
| 93 | |
| 94 | // open stash item diff |
| 95 | func (gui *Gui) stashDiff(g *gocui.Gui, v *gocui.View) error { |
| 96 | |
| 97 | _, oy := v.Origin() |
| 98 | _, cy := v.Cursor() |
| 99 | |
| 100 | return gui.renderStashDiff(oy + cy) |
| 101 | } |
| 102 | |
| 103 | // pop out the stash |
| 104 | func (gui *Gui) stashPop(g *gocui.Gui, v *gocui.View) error { |
| 105 | r := gui.getSelectedRepository() |
| 106 | _, oy := v.Origin() |
| 107 | _, cy := v.Cursor() |
| 108 | if len(r.Stasheds) <= 0 { |
| 109 | return nil |
| 110 | } |
| 111 | stashedItem := r.Stasheds[oy+cy] |
| 112 | output, err := stashedItem.Pop() |
| 113 | if err != nil { |
| 114 | if err = gui.openErrorView(g, output, |
| 115 | "You should manually resolve this issue", |
| 116 | stashViewFeature.Name); err != nil { |
| 117 | return err |
| 118 | } |
| 119 | } |
| 120 | // since the pop is a func of stashed item, we need to refresh entity here |
| 121 | _ = r.Refresh() |
| 122 | if err := gui.focusToRepository(g, v); err != nil { |
| 123 | return err |
| 124 | } |
| 125 | if err := gui.initStashedView(r); err != nil { |
| 126 | return err |
| 127 | } |
| 128 | return nil |
| 129 | } |