| 1 |
package gui |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"regexp" |
| 7 |
|
| 8 |
"github.com/isacikgoz/gitbatch/internal/command" |
| 9 |
"github.com/jroimartin/gocui" |
| 10 |
) |
| 11 |
|
| 12 |
var ( |
| 13 |
commitFrameViewFeature = viewFeature{Name: "commitframe", Title: " Frame "} |
| 14 |
commitUserNameLabelFeature = viewFeature{Name: "commitusernamelabel", Title: " Name: "} |
| 15 |
commitUserEmailLabelViewFeature = viewFeature{Name: "commituseremaillabel", Title: " E-Mail: "} |
| 16 |
|
| 17 |
// these views used as a input for the credentials |
| •18 |
commitMessageViewFeature = viewFeature{Name: "commitmessage", Title: " Commit Message "} |
| 19 |
commitUserUserViewFeature = viewFeature{Name: "commitusername", Title: " Name "} |
| 20 |
commitUserEmailViewFeature = viewFeature{Name: "commituseremail", Title: " E-Mail "} |
| 21 |
|
| 22 |
commitViews = []viewFeature{commitMessageViewFeature, commitUserUserViewFeature, commitUserEmailViewFeature} |
| 23 |
commitLabelViews = []viewFeature{commitFrameViewFeature, commitUserNameLabelFeature, commitUserEmailLabelViewFeature} |
| 24 |
) |
| 25 |
|
| 26 |
// open the commit message views |
| 27 |
func (gui *Gui) openCommitMessageView(g *gocui.Gui, _ *gocui.View) error { |
| 28 |
maxX, maxY := g.Size() |
| 29 |
vFrame, err := g.SetView(commitFrameViewFeature.Name, maxX/2-30, maxY/2-4, maxX/2+30, maxY/2+3) |
| 30 |
if err != nil { |
| 31 |
if err != gocui.ErrUnknownView { |
| 32 |
return err |
| 33 |
} |
| 34 |
vFrame.Frame = true |
| 35 |
fmt.Fprintln(vFrame, " Enter your commit message:") |
| 36 |
} |
| 37 |
v, err := g.SetView(commitMessageViewFeature.Name, maxX/2-29, maxY/2-3, maxX/2+29, maxY/2) |
| 38 |
if err != nil { |
| 39 |
if err != gocui.ErrUnknownView { |
| 40 |
return err |
| 41 |
} |
| 42 |
v.Frame = false |
| 43 |
v.Wrap = true |
| 44 |
v.Editable = true |
| 45 |
v.Editor = gocui.DefaultEditor |
| 46 |
g.Cursor = true |
| 47 |
} |
| 48 |
if err := gui.openCommitUserNameView(g); err != nil { |
| 49 |
return err |
| 50 |
} |
| 51 |
if err := gui.openCommitUserEmailView(g); err != nil { |
| 52 |
return err |
| 53 |
} |
| 54 |
return gui.focusToView(commitMessageViewFeature.Name) |
| 55 |
} |
| 56 |
|
| 57 |
// open an error view to inform user with a message and a useful note |
| 58 |
func (gui *Gui) openCommitUserNameView(g *gocui.Gui) error { |
| 59 |
r := gui.getSelectedRepository() |
| 60 |
maxX, maxY := g.Size() |
| 61 |
// first, create the label for user |
| 62 |
vlabel, err := g.SetView(commitUserNameLabelFeature.Name, maxX/2-30, maxY/2, maxX/2-19, maxY/2+2) |
| 63 |
if err != nil { |
| 64 |
if err != gocui.ErrUnknownView { |
| 65 |
return err |
| 66 |
} |
| 67 |
fmt.Fprintln(vlabel, commitUserNameLabelFeature.Title) |
| 68 |
vlabel.Frame = false |
| 69 |
} |
| 70 |
// second, crete the user input |
| 71 |
v, err := g.SetView(commitUserUserViewFeature.Name, maxX/2-18, maxY/2, maxX/2+29, maxY/2+2) |
| 72 |
if err != nil { |
| 73 |
if err != gocui.ErrUnknownView { |
| 74 |
return err |
| 75 |
} |
| 76 |
name, err := command.Config(r, &command.ConfigOptions{ |
| 77 |
Section: "user", |
| 78 |
Option: "name", |
| 79 |
CommandMode: command.ModeLegacy, |
| 80 |
}) |
| 81 |
if err != nil { |
| 82 |
// possibly could not get the user name |
| 83 |
name = "" |
| 84 |
} |
| 85 |
fmt.Fprintln(v, name) |
| 86 |
v.Editable = true |
| 87 |
v.Frame = false |
| 88 |
} |
| 89 |
return nil |
| 90 |
} |
| 91 |
|
| 92 |
// open an error view to inform user with a message and a useful note |
| 93 |
func (gui *Gui) openCommitUserEmailView(g *gocui.Gui) error { |
| 94 |
r := gui.getSelectedRepository() |
| 95 |
maxX, maxY := g.Size() |
| 96 |
// first, create the label for password |
| 97 |
vlabel, err := g.SetView(commitUserEmailLabelViewFeature.Name, maxX/2-30, maxY/2+1, maxX/2-19, maxY/2+3) |
| 98 |
if err != nil { |
| 99 |
if err != gocui.ErrUnknownView { |
| 100 |
return err |
| 101 |
} |
| 102 |
fmt.Fprintln(vlabel, commitUserEmailLabelViewFeature.Title) |
| 103 |
vlabel.Frame = false |
| 104 |
} |
| 105 |
// second, crete the masked password input |
| 106 |
v, err := g.SetView(commitUserEmailViewFeature.Name, maxX/2-18, maxY/2+1, maxX/2+29, maxY/2+3) |
| 107 |
if err != nil { |
| 108 |
if err != gocui.ErrUnknownView { |
| 109 |
return err |
| 110 |
} |
| 111 |
email, err := command.Config(r, &command.ConfigOptions{ |
| 112 |
Section: "user", |
| 113 |
Option: "email", |
| 114 |
CommandMode: command.ModeLegacy, |
| 115 |
}) |
| 116 |
if err != nil { |
| 117 |
// possibly could not get the user email |
| 118 |
email = "" |
| 119 |
} |
| 120 |
fmt.Fprintln(v, email) |
| 121 |
v.Editable = true |
| 122 |
v.Frame = false |
| 123 |
} |
| 124 |
return nil |
| 125 |
} |
| 126 |
|
| 127 |
// close the opened commite mesage view |
| 128 |
func (gui *Gui) submitCommitMessageView(g *gocui.Gui, v *gocui.View) error { |
| 129 |
r := gui.getSelectedRepository() |
| 130 |
|
| 131 |
// in order to read buffer of the views, first we need to find'em |
| 132 |
vMsg, err := g.View(commitMessageViewFeature.Name) |
| 133 |
if err != nil { |
| 134 |
return err // should return?? |
| 135 |
} |
| 136 |
|
| 137 |
vName, err := g.View(commitUserUserViewFeature.Name) |
| 138 |
if err != nil { |
| 139 |
return err // should return?? |
| 140 |
} |
| 141 |
|
| 142 |
vEmail, err := g.View(commitUserEmailViewFeature.Name) |
| 143 |
if err != nil { |
| 144 |
return err // should return?? |
| 145 |
} |
| 146 |
|
| 147 |
// the return string of the views contain trailing new lines |
| 148 |
re := regexp.MustCompile(`\r?\n`) |
| 149 |
// TODO: maybe intentionally added new lines? |
| 150 |
msg := re.ReplaceAllString(vMsg.ViewBuffer(), "") |
| 151 |
name := re.ReplaceAllString(vName.ViewBuffer(), "") |
| 152 |
email := re.ReplaceAllString(vEmail.ViewBuffer(), "") |
| 153 |
if len(email) <= 0 { |
| 154 |
return errors.New("User email needs to be provided") |
| 155 |
} |
| 156 |
|
| 157 |
err = command.Commit(r, &command.CommitOptions{ |
| 158 |
CommitMsg: msg, |
| 159 |
User: name, |
| 160 |
Email: email, |
| 161 |
CommandMode: command.ModeNative, |
| 162 |
}) |
| 163 |
if err != nil { |
| 164 |
return err |
| 165 |
} |
| 166 |
|
| 167 |
return gui.closeCommitMessageView(g, v) |
| 168 |
} |
| 169 |
|
| 170 |
// focus to next view |
| 171 |
func (gui *Gui) nextCommitView(g *gocui.Gui, v *gocui.View) error { |
| 172 |
return gui.nextViewOfGroup(g, v, commitViews) |
| 173 |
} |
| 174 |
|
| 175 |
// close the opened commite mesage view |
| 176 |
func (gui *Gui) closeCommitMessageView(g *gocui.Gui, v *gocui.View) error { |
| 177 |
// r := gui.getSelectedRepository() |
| 178 |
g.Cursor = false |
| 179 |
for _, view := range commitViews { |
| 180 |
if err := g.DeleteView(view.Name); err != nil { |
| 181 |
return err |
| 182 |
} |
| 183 |
} |
| 184 |
for _, view := range commitLabelViews { |
| 185 |
if err := g.DeleteView(view.Name); err != nil { |
| 186 |
return err |
| 187 |
} |
| 188 |
} |
| 189 |
if err := gui.focusToRepository(g, v); err != nil { |
| 190 |
return err |
| 191 |
} |
| 192 |
return nil |
| 193 |
} |