glow/ui/ui.go

424 lines
9.2 KiB
Go
Raw Normal View History

package ui
import (
2020-05-22 18:03:21 +00:00
"fmt"
"os"
"path/filepath"
2020-05-27 15:55:00 +00:00
"strings"
2020-08-21 17:39:59 +00:00
"time"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glamour"
2020-10-30 15:14:36 +00:00
"github.com/charmbracelet/glow/utils"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/muesli/gitcha"
2020-05-14 19:06:13 +00:00
te "github.com/muesli/termenv"
)
2020-08-21 20:41:06 +00:00
const (
statusMessageTimeout = time.Second * 3 // how long to show status messages like "stashed!"
2020-12-14 23:42:33 +00:00
ellipsis = "…"
2020-08-21 20:41:06 +00:00
)
2020-08-13 19:37:53 +00:00
var (
config Config
2020-12-10 01:12:01 +00:00
markdownExtensions = []string{
"*.md", "*.mdown", "*.mkdn", "*.mkd", "*.markdown",
}
2020-05-22 02:29:46 +00:00
)
2020-08-13 19:37:53 +00:00
// NewProgram returns a new Tea program.
func NewProgram(cfg Config) *tea.Program {
log.Debug(
"Starting glow",
"high_perf_pager",
cfg.HighPerformancePager,
"glamour",
cfg.GlamourEnabled,
)
config = cfg
opts := []tea.ProgramOption{tea.WithAltScreen()}
2022-10-25 14:40:51 +00:00
if cfg.EnableMouse {
opts = append(opts, tea.WithMouseCellMotion())
}
m := newModel(cfg)
return tea.NewProgram(m, opts...)
}
type errMsg struct{ err error }
func (e errMsg) Error() string { return e.err.Error() }
type (
initLocalFileSearchMsg struct {
cwd string
ch chan gitcha.SearchResult
}
)
2022-10-25 14:40:51 +00:00
type (
foundLocalFileMsg gitcha.SearchResult
localFileSearchFinished struct{}
statusMessageTimeoutMsg applicationContext
)
2020-12-11 00:26:24 +00:00
// applicationContext indicates the area of the application something applies
2020-12-11 01:35:00 +00:00
// to. Occasionally used as an argument to commands and messages.
2020-08-21 17:39:59 +00:00
type applicationContext int
const (
stashContext applicationContext = iota
pagerContext
)
2020-12-11 01:35:00 +00:00
// state is the top-level application state.
type state int
const (
stateShowStash state = iota
2020-05-14 02:08:17 +00:00
stateShowDocument
)
2020-05-22 02:29:46 +00:00
func (s state) String() string {
2020-12-11 01:35:00 +00:00
return map[state]string{
stateShowStash: "showing file listing",
stateShowDocument: "showing document",
2020-05-22 02:29:46 +00:00
}[s]
}
// Common stuff we'll need to access in all models.
type commonModel struct {
cfg Config
cwd string
width int
height int
}
type model struct {
common *commonModel
state state
fatalErr error
// Sub-models
stash stashModel
pager pagerModel
// Channel that receives paths to local markdown files
// (via the github.com/muesli/gitcha package)
localFileFinder chan gitcha.SearchResult
2020-05-14 02:08:17 +00:00
}
// unloadDocument unloads a document from the pager. Note that while this
// method alters the model we also need to send along any commands returned.
func (m *model) unloadDocument() []tea.Cmd {
2020-05-14 02:08:17 +00:00
m.state = stateShowStash
m.stash.viewState = stashStateReady
m.pager.unload()
2020-06-03 18:17:41 +00:00
m.pager.showHelp = false
var batch []tea.Cmd
if m.pager.viewport.HighPerformanceRendering {
batch = append(batch, tea.ClearScrollArea)
}
if !m.stash.shouldSpin() {
batch = append(batch, m.stash.spinner.Tick)
}
return batch
}
func newModel(cfg Config) tea.Model {
2023-05-05 09:40:36 +00:00
initSections()
if cfg.GlamourStyle == glamour.AutoStyle {
if te.HasDarkBackground() {
cfg.GlamourStyle = glamour.DarkStyle
} else {
cfg.GlamourStyle = glamour.LightStyle
}
}
teamList := list.New([]list.Item{}, list.NewDefaultDelegate(), 0, 0)
teamList.Styles.Title = lipgloss.NewStyle().Foreground(yellowGreen)
teamList.SetStatusBarItemName("team", "teams")
teamList.SetShowHelp(true)
// We use the team list status message as a permanent placeholder.
teamList.StatusMessageLifetime = time.Hour
common := commonModel{
cfg: cfg,
}
return model{
common: &common,
state: stateShowStash,
pager: newPagerModel(&common),
stash: newStashModel(&common),
}
}
func (m model) Init() tea.Cmd {
cmds := []tea.Cmd{m.stash.spinner.Tick}
cmds = append(cmds, findLocalFiles(*m.common))
return tea.Batch(cmds...)
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
2020-06-05 20:57:27 +00:00
// If there's been an error, any key exits
if m.fatalErr != nil {
2020-06-05 20:57:27 +00:00
if _, ok := msg.(tea.KeyMsg); ok {
return m, tea.Quit
}
}
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
2021-03-09 13:16:17 +00:00
case "esc":
if m.state == stateShowDocument || m.stash.viewState == stashStateLoadingDocument {
2021-03-09 13:16:17 +00:00
batch := m.unloadDocument()
return m, tea.Batch(batch...)
}
case "r":
if m.state == stateShowStash {
m.stash.markdowns = nil
return m, m.Init()
}
2021-03-09 13:16:17 +00:00
case "q":
var cmd tea.Cmd
2020-05-22 02:29:46 +00:00
switch m.state {
case stateShowStash:
// pass through all keys if we're editing the filter
if m.stash.filterState == filtering {
m.stash, cmd = m.stash.update(msg)
return m, cmd
2020-05-22 02:29:46 +00:00
}
2020-05-14 02:08:17 +00:00
}
2020-05-22 02:29:46 +00:00
return m, tea.Quit
2020-05-14 02:08:17 +00:00
case "left", "h", "delete":
if m.state == stateShowDocument {
cmds = append(cmds, m.unloadDocument()...)
return m, tea.Batch(cmds...)
}
2020-06-23 19:11:54 +00:00
// Ctrl+C always quits no matter where in the application you are.
case "ctrl+c":
return m, tea.Quit
}
// Window size is received when starting up and on every resize
case tea.WindowSizeMsg:
m.common.width = msg.Width
m.common.height = msg.Height
m.stash.setSize(msg.Width, msg.Height)
m.pager.setSize(msg.Width, msg.Height)
2020-05-18 22:58:19 +00:00
case initLocalFileSearchMsg:
m.localFileFinder = msg.ch
m.common.cwd = msg.cwd
cmds = append(cmds, findNextLocalFile(m))
2020-05-22 19:31:54 +00:00
case fetchedMarkdownMsg:
// We've loaded a markdown file's contents for rendering
2020-08-21 00:21:52 +00:00
m.pager.currentDocument = *msg
body := string(utils.RemoveFrontmatter([]byte(msg.Body)))
cmds = append(cmds, renderWithGlamour(m.pager, body))
2020-05-15 19:08:45 +00:00
case contentRenderedMsg:
m.state = stateShowDocument
case localFileSearchFinished:
// Always pass these messages to the stash so we can keep it updated
// about network activity, even if the user isn't currently viewing
// the stash.
2020-11-25 16:40:24 +00:00
stashModel, cmd := m.stash.update(msg)
m.stash = stashModel
return m, cmd
case foundLocalFileMsg:
newMd := localFileToMarkdown(m.common.cwd, gitcha.SearchResult(msg))
m.stash.addMarkdowns(newMd)
2020-12-15 18:37:11 +00:00
if m.stash.filterApplied() {
newMd.buildFilterValue()
}
if m.stash.shouldUpdateFilter() {
cmds = append(cmds, filterMarkdowns(m.stash))
}
cmds = append(cmds, findNextLocalFile(m))
case filteredMarkdownMsg:
if m.state == stateShowDocument {
newStashModel, cmd := m.stash.update(msg)
m.stash = newStashModel
cmds = append(cmds, cmd)
2020-08-21 17:39:59 +00:00
}
}
// Process children
switch m.state {
case stateShowStash:
2020-11-25 16:40:24 +00:00
newStashModel, cmd := m.stash.update(msg)
m.stash = newStashModel
cmds = append(cmds, cmd)
2020-05-14 02:08:17 +00:00
case stateShowDocument:
2020-11-25 16:40:24 +00:00
newPagerModel, cmd := m.pager.update(msg)
m.pager = newPagerModel
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}
func (m model) View() string {
if m.fatalErr != nil {
return errorView(m.fatalErr, true)
}
switch m.state {
2020-05-14 02:08:17 +00:00
case stateShowDocument:
return m.pager.View()
2020-08-13 19:26:34 +00:00
default:
2020-11-25 16:40:24 +00:00
return m.stash.view()
}
}
func errorView(err error, fatal bool) string {
exitMsg := "press any key to "
if fatal {
exitMsg += "exit"
} else {
exitMsg += "return"
}
s := fmt.Sprintf("%s\n\n%v\n\n%s",
errorTitleStyle.Render("ERROR"),
2020-06-05 20:57:27 +00:00
err,
subtleStyle.Render(exitMsg),
2020-06-05 20:57:27 +00:00
)
2020-07-16 19:37:48 +00:00
return "\n" + indent(s, 3)
2020-06-05 20:57:27 +00:00
}
// COMMANDS
2020-05-18 21:53:46 +00:00
func findLocalFiles(m commonModel) tea.Cmd {
return func() tea.Msg {
log.Info("findLocalFiles")
var (
cwd = m.cfg.WorkingDirectory
err error
)
if cwd == "" {
cwd, err = os.Getwd()
} else {
var info os.FileInfo
info, err = os.Stat(cwd)
if err == nil && info.IsDir() {
cwd, err = filepath.Abs(cwd)
}
}
// Note that this is one error check for both cases above
if err != nil {
log.Error("error finding local files", "error", err)
return errMsg{err}
2020-08-18 19:41:57 +00:00
}
log.Debug("local directory is", "cwd", cwd)
// Switch between FindFiles and FindAllFiles to bypass .gitignore rules
var ch chan gitcha.SearchResult
if m.cfg.ShowAllFiles {
ch, err = gitcha.FindAllFilesExcept(cwd, markdownExtensions, nil)
} else {
ch, err = gitcha.FindFilesExcept(cwd, markdownExtensions, ignorePatterns(m))
}
if err != nil {
log.Error("error finding local files", "error", err)
return errMsg{err}
2020-08-22 08:16:01 +00:00
}
return initLocalFileSearchMsg{ch: ch, cwd: cwd}
}
}
func findNextLocalFile(m model) tea.Cmd {
return func() tea.Msg {
res, ok := <-m.localFileFinder
if ok {
// Okay now find the next one
return foundLocalFileMsg(res)
}
// We're done
log.Debug("local file search finished")
return localFileSearchFinished{}
}
}
2020-08-21 17:39:59 +00:00
func waitForStatusMessageTimeout(appCtx applicationContext, t *time.Timer) tea.Cmd {
return func() tea.Msg {
<-t.C
return statusMessageTimeoutMsg(appCtx)
}
}
// ETC
// Convert a Gitcha result to an internal representation of a markdown
// document. Note that we could be doing things like checking if the file is
// a directory, but we trust that gitcha has already done that.
2020-08-24 20:10:06 +00:00
func localFileToMarkdown(cwd string, res gitcha.SearchResult) *markdown {
md := &markdown{
localPath: res.Path,
Note: stripAbsolutePath(res.Path, cwd),
Modtime: res.Info.ModTime(),
}
2020-08-24 20:10:06 +00:00
return md
}
func stripAbsolutePath(fullPath, cwd string) string {
return strings.ReplaceAll(fullPath, cwd+string(os.PathSeparator), "")
}
// Lightweight version of reflow's indent function.
2020-05-27 15:55:00 +00:00
func indent(s string, n int) string {
if n <= 0 || s == "" {
return s
}
l := strings.Split(s, "\n")
b := strings.Builder{}
i := strings.Repeat(" ", n)
for _, v := range l {
fmt.Fprintf(&b, "%s%s\n", i, v)
}
return b.String()
}
2020-05-14 00:21:37 +00:00
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}