mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
140 lines
2.5 KiB
Go
140 lines
2.5 KiB
Go
package git
|
|
|
|
import (
|
|
"github.com/gdamore/tcell"
|
|
"github.com/olebedev/config"
|
|
"github.com/rivo/tview"
|
|
"github.com/senorprogrammer/wtf/wtf"
|
|
)
|
|
|
|
// Config is a pointer to the global config object
|
|
var Config *config.Config
|
|
|
|
const HelpText = `
|
|
Keyboard commands for Git:
|
|
|
|
/: Show/hide this help window
|
|
h: Previous git repository
|
|
l: Next git repository
|
|
|
|
arrow left: Previous git repository
|
|
arrow right: Next git repository
|
|
`
|
|
|
|
type Widget struct {
|
|
wtf.TextWidget
|
|
|
|
app *tview.Application
|
|
Data []*GitRepo
|
|
Idx int
|
|
pages *tview.Pages
|
|
}
|
|
|
|
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
|
|
widget := Widget{
|
|
TextWidget: wtf.NewTextWidget(" Git ", "git", true),
|
|
|
|
app: app,
|
|
Idx: 0,
|
|
pages: pages,
|
|
}
|
|
|
|
widget.View.SetInputCapture(widget.keyboardIntercept)
|
|
|
|
return &widget
|
|
}
|
|
|
|
/* -------------------- Exported Functions -------------------- */
|
|
|
|
func (widget *Widget) Refresh() {
|
|
if widget.Disabled() {
|
|
return
|
|
}
|
|
|
|
repoPaths := wtf.ToStrs(Config.UList("wtf.mods.git.repositories"))
|
|
|
|
widget.UpdateRefreshedAt()
|
|
widget.Data = widget.gitRepos(repoPaths)
|
|
widget.display()
|
|
}
|
|
|
|
func (widget *Widget) Next() {
|
|
widget.Idx = widget.Idx + 1
|
|
if widget.Idx == len(widget.Data) {
|
|
widget.Idx = 0
|
|
}
|
|
|
|
widget.display()
|
|
}
|
|
|
|
func (widget *Widget) Prev() {
|
|
widget.Idx = widget.Idx - 1
|
|
if widget.Idx < 0 {
|
|
widget.Idx = len(widget.Data) - 1
|
|
}
|
|
|
|
widget.display()
|
|
}
|
|
|
|
/* -------------------- Unexported Functions -------------------- */
|
|
|
|
func (widget *Widget) currentData() *GitRepo {
|
|
if len(widget.Data) == 0 {
|
|
return nil
|
|
}
|
|
|
|
if widget.Idx < 0 || widget.Idx >= len(widget.Data) {
|
|
return nil
|
|
}
|
|
|
|
return widget.Data[widget.Idx]
|
|
}
|
|
|
|
func (widget *Widget) gitRepos(repoPaths []string) []*GitRepo {
|
|
repos := []*GitRepo{}
|
|
|
|
for _, repoPath := range repoPaths {
|
|
repo := NewGitRepo(repoPath)
|
|
repos = append(repos, repo)
|
|
}
|
|
|
|
return repos
|
|
}
|
|
|
|
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
|
|
switch string(event.Rune()) {
|
|
case "/":
|
|
widget.showHelp()
|
|
return nil
|
|
case "h":
|
|
widget.Prev()
|
|
return nil
|
|
case "l":
|
|
widget.Next()
|
|
return nil
|
|
}
|
|
|
|
switch event.Key() {
|
|
case tcell.KeyLeft:
|
|
widget.Prev()
|
|
return nil
|
|
case tcell.KeyRight:
|
|
widget.Next()
|
|
return nil
|
|
default:
|
|
return event
|
|
}
|
|
}
|
|
|
|
func (widget *Widget) showHelp() {
|
|
closeFunc := func() {
|
|
widget.pages.RemovePage("help")
|
|
widget.app.SetFocus(widget.View)
|
|
}
|
|
|
|
modal := wtf.NewBillboardModal(HelpText, closeFunc)
|
|
|
|
widget.pages.AddPage("help", modal, false, true)
|
|
widget.app.SetFocus(modal)
|
|
}
|