1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

added git pull and checkout commands

This commit is contained in:
Wilq 2018-06-02 16:52:29 +02:00
parent c51d6bdecf
commit 66c14a666d
2 changed files with 85 additions and 0 deletions

View File

@ -69,6 +69,19 @@ func (repo *GitRepo) repository() string {
return str return str
} }
func (repo *GitRepo) pull() string {
arg := []string{repo.gitDir(), repo.workTree(), "pull"}
cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd)
return str
}
func (repo *GitRepo) checkout(branch string) string {
arg := []string{repo.gitDir(), repo.workTree(), "checkout", branch}
cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd)
return str
}
func (repo *GitRepo) gitDir() string { func (repo *GitRepo) gitDir() string {
return fmt.Sprintf("--git-dir=%s/.git", repo.Path) return fmt.Sprintf("--git-dir=%s/.git", repo.Path)

View File

@ -16,6 +16,8 @@ const HelpText = `
/: Show/hide this help window /: Show/hide this help window
h: Previous git repository h: Previous git repository
l: Next git repository l: Next git repository
p: Pull current git repository
c: Checkout to branch
arrow left: Previous git repository arrow left: Previous git repository
arrow right: Next git repository arrow right: Next git repository
@ -75,8 +77,72 @@ func (widget *Widget) Prev() {
widget.display() widget.display()
} }
func (widget *Widget) Pull() {
repoToPull := widget.Data[widget.Idx]
repoToPull.pull()
widget.Refresh()
}
func (widget *Widget) Checkout() {
form := widget.modalForm("Branch to checkout:", "")
checkoutFctn := func() {
text := form.GetFormItem(0).(*tview.InputField).GetText()
repoToCheckout := widget.Data[widget.Idx]
repoToCheckout.checkout(text)
widget.pages.RemovePage("modal")
widget.app.SetFocus(widget.View)
widget.display()
widget.Refresh()
}
widget.addButtons(form, checkoutFctn)
widget.modalFocus(form)
}
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */
func (widget *Widget) addCheckoutButton(form *tview.Form, fctn func()) {
form.AddButton("Checkout", fctn)
}
func (widget *Widget) addButtons(form *tview.Form, checkoutFctn func()) {
widget.addCheckoutButton(form, checkoutFctn)
widget.addCancelButton(form)
}
func (widget *Widget) addCancelButton(form *tview.Form) {
cancelFn := func() {
widget.pages.RemovePage("modal")
widget.app.SetFocus(widget.View)
widget.display()
}
form.AddButton("Cancel", cancelFn)
form.SetCancelFunc(cancelFn)
}
func (widget *Widget) modalFocus(form *tview.Form) {
frame := widget.modalFrame(form)
widget.pages.AddPage("modal", frame, false, true)
widget.app.SetFocus(frame)
}
func (widget *Widget) modalForm(lbl, text string) *tview.Form {
form := tview.NewForm().
SetButtonsAlign(tview.AlignCenter).
SetButtonTextColor(tview.Styles.PrimaryTextColor)
form.AddInputField(lbl, text, 60, nil, nil)
return form
}
func (widget *Widget) modalFrame(form *tview.Form) *tview.Frame {
_, _, w, h := widget.View.GetInnerRect()
frame := tview.NewFrame(form).SetBorders(0, 0, 0, 0, 0, 0)
frame.SetBorder(true)
frame.SetRect(w+20, h+2, 80, 7)
return frame
}
func (widget *Widget) currentData() *GitRepo { func (widget *Widget) currentData() *GitRepo {
if len(widget.Data) == 0 { if len(widget.Data) == 0 {
@ -112,6 +178,12 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
case "l": case "l":
widget.Next() widget.Next()
return nil return nil
case "p":
widget.Pull()
return nil
case "c":
widget.Checkout()
return nil
} }
switch event.Key() { switch event.Key() {