glow/ui/stashitem.go

100 lines
2.9 KiB
Go
Raw Normal View History

2020-05-26 15:51:08 +00:00
package ui
import (
"fmt"
"strings"
2020-05-26 15:51:08 +00:00
"github.com/charmbracelet/bubbles/textinput"
2020-05-26 15:51:08 +00:00
"github.com/charmbracelet/charm/ui/common"
2020-07-15 00:25:51 +00:00
rw "github.com/mattn/go-runewidth"
2020-05-26 15:51:08 +00:00
te "github.com/muesli/termenv"
)
const (
newsPrefix = "News: "
verticalLine = "│"
noMemoTitle = "No Memo"
2020-07-15 00:25:51 +00:00
stashIcon = "• "
2020-05-26 15:51:08 +00:00
)
var (
greenFg = te.Style{}.Foreground(common.NewColorPair("#04B575", "#04B575").Color()).Styled
faintGreenFg = te.Style{}.Foreground(common.NewColorPair("#2B4A3F", "#ABE5D1").Color()).Styled
fuchsiaFg = te.Style{}.Foreground(common.Fuschia.Color()).Styled
dullFuchsiaFg = te.Style{}.Foreground(common.NewColorPair("#AD58B4", "#F793FF").Color()).Styled
yellowFg = te.Style{}.Foreground(common.YellowGreen.Color()).Styled // renders light green on light backgrounds
dullYellowFg = te.Style{}.Foreground(common.NewColorPair("#9BA92F", "#6BCB94").Color()).Styled // renders light green on light backgrounds
indigoFg = te.Style{}.Foreground(common.Indigo.Color()).Styled
subtleIndigoFg = te.Style{}.Foreground(common.NewColorPair("#514DC1", "#7D79F6").Color()).Styled
redFg = te.Style{}.Foreground(common.Red.Color()).Styled
faintRedFg = te.Style{}.Foreground(common.FaintRed.Color()).Styled
warmGrayFg = te.Style{}.Foreground(common.NewColorPair("#979797", "#847A85").Color()).Styled
2020-05-26 15:51:08 +00:00
)
func stashItemView(b *strings.Builder, m stashModel, index int, md *markdown) {
2020-05-26 15:51:08 +00:00
truncateTo := m.terminalWidth - stashViewHorizontalPadding*2
2020-05-26 15:51:08 +00:00
gutter := " "
title := md.Note
date := relativeTime(*md.CreatedAt)
2020-07-15 00:25:51 +00:00
icon := ""
2020-05-26 15:51:08 +00:00
2020-07-15 00:25:51 +00:00
switch md.markdownType {
case newsMarkdown:
2020-05-26 15:51:08 +00:00
if title == "" {
title = "News"
} else {
2020-07-15 00:25:51 +00:00
title = newsPrefix + truncate(title, truncateTo-rw.StringWidth(newsPrefix))
2020-05-26 15:51:08 +00:00
}
2020-07-15 00:25:51 +00:00
case stashedMarkdown:
icon = stashIcon
icon = stashIcon
2020-05-26 15:51:08 +00:00
if title == "" {
title = noMemoTitle
}
2020-07-15 00:25:51 +00:00
title = truncate(title, truncateTo-rw.StringWidth(icon))
default:
2020-05-26 15:51:08 +00:00
title = truncate(title, truncateTo)
}
if index == m.index {
switch m.state {
case stashStatePromptDelete:
// Deleting
gutter = faintRedFg(verticalLine)
icon = faintRedFg(icon)
title = redFg(title)
date = faintRedFg(date)
2020-05-26 15:51:08 +00:00
case stashStateSettingNote:
// Setting note
gutter = dullYellowFg(verticalLine)
2020-07-15 00:25:51 +00:00
icon = ""
2020-05-26 15:51:08 +00:00
title = textinput.View(m.noteInput)
date = dullYellowFg(date)
2020-05-26 15:51:08 +00:00
default:
// Selected
gutter = dullFuchsiaFg(verticalLine)
icon = dullFuchsiaFg(icon)
title = fuchsiaFg(title)
date = dullFuchsiaFg(date)
2020-05-26 15:51:08 +00:00
}
} else {
// Normal
if md.markdownType == newsMarkdown {
gutter = " "
title = te.String(title).Foreground(common.Indigo.Color()).String()
date = subtleIndigoFg(date)
2020-05-26 15:51:08 +00:00
} else {
icon = greenFg(icon)
2020-05-26 15:51:08 +00:00
if title == noMemoTitle {
title = warmGrayFg(title)
2020-05-26 15:51:08 +00:00
}
gutter = " "
date = warmGrayFg(date)
2020-05-26 15:51:08 +00:00
}
}
2020-07-15 00:25:51 +00:00
fmt.Fprintf(b, "%s %s%s\n", gutter, icon, title)
fmt.Fprintf(b, "%s %s", gutter, date)
2020-05-26 15:51:08 +00:00
}