Skip to content

Commit

Permalink
Allow for direct linking to a repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Toby Padilla committed Aug 13, 2021
1 parent 777ebb6 commit 189fdb5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 26 deletions.
3 changes: 3 additions & 0 deletions tui/bubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type SessionConfig struct {
Width int
Height int
WindowChanges <-chan ssh.Window
InitialRepo string
}

type Bubble struct {
Expand All @@ -52,6 +53,7 @@ type Bubble struct {
height int
windowChanges <-chan ssh.Window
repoSource *git.RepoSource
initialRepo string
repoMenu []MenuEntry
repos []*git.Repo
boxes []tea.Model
Expand All @@ -69,6 +71,7 @@ func NewBubble(cfg *Config, sCfg *SessionConfig) *Bubble {
repoSource: cfg.RepoSource,
repoMenu: make([]MenuEntry, 0),
boxes: make([]tea.Model, 2),
initialRepo: sCfg.InitialRepo,
}
b.state = startState
return b
Expand Down
24 changes: 12 additions & 12 deletions tui/bubbles/selection/bubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Bubble struct {
NormalStyle lipgloss.Style
SelectedStyle lipgloss.Style
Items []string
selectedItem int
SelectedItem int
}

func NewBubble(items []string) *Bubble {
Expand All @@ -37,7 +37,7 @@ func (b *Bubble) Init() tea.Cmd {
func (b *Bubble) View() string {
s := ""
for i, item := range b.Items {
if i == b.selectedItem {
if i == b.SelectedItem {
s += b.SelectedStyle.Render(item) + "\n"
} else {
s += b.NormalStyle.Render(item) + "\n"
Expand All @@ -52,13 +52,13 @@ func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
switch msg.String() {
case "k", "up":
if b.selectedItem > 0 {
b.selectedItem--
if b.SelectedItem > 0 {
b.SelectedItem--
cmds = append(cmds, b.sendActiveMessage)
}
case "j", "down":
if b.selectedItem < len(b.Items)-1 {
b.selectedItem++
if b.SelectedItem < len(b.Items)-1 {
b.SelectedItem++
cmds = append(cmds, b.sendActiveMessage)
}
case "enter":
Expand All @@ -69,20 +69,20 @@ func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (b *Bubble) sendActiveMessage() tea.Msg {
if b.selectedItem >= 0 && b.selectedItem < len(b.Items) {
if b.SelectedItem >= 0 && b.SelectedItem < len(b.Items) {
return ActiveMsg{
Name: b.Items[b.selectedItem],
Index: b.selectedItem,
Name: b.Items[b.SelectedItem],
Index: b.SelectedItem,
}
}
return nil
}

func (b *Bubble) sendSelectedMessage() tea.Msg {
if b.selectedItem >= 0 && b.selectedItem < len(b.Items) {
if b.SelectedItem >= 0 && b.SelectedItem < len(b.Items) {
return SelectedMsg{
Name: b.Items[b.selectedItem],
Index: b.selectedItem,
Name: b.Items[b.SelectedItem],
Index: b.SelectedItem,
}
}
return nil
Expand Down
18 changes: 16 additions & 2 deletions tui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,22 @@ func (b *Bubble) setupCmd() tea.Msg {
boxRightWidth-horizontalPadding-2,
b.repoSource.GetCommits(200),
)
b.boxes[1] = b.repoMenu[0].bubble
b.activeBox = 0
ir := -1
if b.initialRepo != "" {
for i, me := range b.repoMenu {
if me.Repo == b.initialRepo {
ir = i
}
}
}
if ir == -1 {
b.boxes[1] = b.repoMenu[0].bubble
b.activeBox = 0
} else {
b.boxes[1] = b.repoMenu[ir].bubble
b.repoSelect.SelectedItem = ir
b.activeBox = 1
}
b.state = loadedState
return nil
}
30 changes: 18 additions & 12 deletions tui/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,25 @@ func SessionHandler(reposPath string, repoPoll time.Duration) func(ssh.Session)
}()

return func(s ssh.Session) (tea.Model, []tea.ProgramOption) {
if len(s.Command()) == 0 {
pty, changes, active := s.Pty()
if !active {
return nil, nil
}
cfg := &SessionConfig{
Width: pty.Window.Width,
Height: pty.Window.Height,
WindowChanges: changes,
}
return NewBubble(appCfg, cfg), []tea.ProgramOption{tea.WithAltScreen()}
cmd := s.Command()
cfg := &SessionConfig{}
switch len(cmd) {
case 0:
cfg.InitialRepo = ""
case 1:
cfg.InitialRepo = cmd[0]
default:
return nil, nil
}
pty, changes, active := s.Pty()
if !active {
fmt.Println("not active")
return nil, nil
}
return nil, nil
cfg.Width = pty.Window.Width
cfg.Height = pty.Window.Height
cfg.WindowChanges = changes
return NewBubble(appCfg, cfg), []tea.ProgramOption{tea.WithAltScreen()}
}
}

Expand Down

0 comments on commit 189fdb5

Please sign in to comment.