-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Toby Padilla
committed
Aug 12, 2021
1 parent
e1250b9
commit bcbb569
Showing
6 changed files
with
113 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,95 @@ | ||
package repo | ||
|
||
import ( | ||
"bytes" | ||
"smoothie/git" | ||
"text/template" | ||
|
||
"github.com/charmbracelet/bubbles/viewport" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/go-git/go-git/v5" | ||
"github.com/charmbracelet/glamour" | ||
) | ||
|
||
type ErrMsg struct { | ||
Error error | ||
} | ||
|
||
type Bubble struct { | ||
repo *git.Repository | ||
templateObject interface{} | ||
repoSource *git.RepoSource | ||
name string | ||
repo *git.Repo | ||
readmeViewport *ViewportBubble | ||
} | ||
|
||
func NewBubble(rs *git.RepoSource, name string, width int, height int, tmp interface{}) *Bubble { | ||
return &Bubble{ | ||
templateObject: tmp, | ||
repoSource: rs, | ||
name: name, | ||
readmeViewport: &ViewportBubble{ | ||
Viewport: &viewport.Model{ | ||
Width: width, | ||
Height: height, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (b *Bubble) Init() tea.Cmd { | ||
return nil | ||
return b.setupCmd | ||
} | ||
|
||
func (b *Bubble) Update(tea.Msg) (tea.Model, tea.Cmd) { | ||
return nil, nil | ||
func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
return b.readmeViewport.Update(msg) | ||
} | ||
|
||
func (b *Bubble) View() string { | ||
return "repo" | ||
return b.readmeViewport.View() | ||
} | ||
|
||
func (b *Bubble) setupCmd() tea.Msg { | ||
r, err := b.repoSource.GetRepo(b.name) | ||
if err != nil { | ||
return ErrMsg{err} | ||
} | ||
md := r.Readme | ||
if b.templateObject != nil { | ||
t, err := template.New("readme").Parse(md) | ||
if err != nil { | ||
return ErrMsg{err} | ||
} | ||
buf := &bytes.Buffer{} | ||
err = t.Execute(buf, b.templateObject) | ||
if err != nil { | ||
return ErrMsg{err} | ||
} | ||
md = buf.String() | ||
} | ||
md, err = b.glamourize(md) | ||
if err != nil { | ||
return ErrMsg{err} | ||
} | ||
b.readmeViewport.Viewport.GotoTop() | ||
b.readmeViewport.Viewport.SetContent(md) | ||
return nil | ||
} | ||
|
||
func (b *Bubble) templatize(t string) (string, error) { | ||
return "", nil | ||
} | ||
|
||
func (b *Bubble) glamourize(md string) (string, error) { | ||
tr, err := glamour.NewTermRenderer( | ||
glamour.WithAutoStyle(), | ||
glamour.WithWordWrap(b.readmeViewport.Viewport.Width), | ||
) | ||
if err != nil { | ||
return "", err | ||
} | ||
mdt, err := tr.Render(md) | ||
if err != nil { | ||
return "", err | ||
} | ||
return mdt, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package tui | ||
package repo | ||
|
||
import ( | ||
"github.com/charmbracelet/bubbles/viewport" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters