glow/ui/stashitem.go

150 lines
4.1 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"
fileListingStashIcon = "• "
2020-05-26 15:51:08 +00:00
)
var (
normalFg = makeFgStyle(common.NewColorPair("#dddddd", "#000"))
dimNormalFg = makeFgStyle(common.NewColorPair("#777777", "#000"))
warmGrayFg = makeFgStyle(common.NewColorPair("#979797", "#847A85"))
dimWarmGrayFg = makeFgStyle(common.NewColorPair("#4D4D4D", "#000000"))
grayFg = makeFgStyle(common.NewColorPair("#626262", "#000"))
dimGrayFg = makeFgStyle(common.NewColorPair("#3F3F3F", "#000"))
greenFg = makeFgStyle(common.NewColorPair("#04B575", "#04B575"))
dimGreenFg = makeFgStyle(common.NewColorPair("#0B5137", "#000000"))
fuchsiaFg = makeFgStyle(common.Fuschia)
dimFuchsiaFg = makeFgStyle(common.NewColorPair("#99519E", "#000000"))
dullFuchsiaFg = makeFgStyle(common.NewColorPair("#AD58B4", "#F793FF"))
dimDullFuchsiaFg = makeFgStyle(common.NewColorPair("#6B3A6F", "#000000"))
indigoFg = makeFgStyle(common.Indigo)
dimIndigoFg = makeFgStyle(common.NewColorPair("#494690", "#000000"))
subtleIndigoFg = makeFgStyle(common.NewColorPair("#514DC1", "#7D79F6"))
dimSubtleIndigoFg = makeFgStyle(common.NewColorPair("#383584", "#000000"))
yellowFg = makeFgStyle(common.YellowGreen) // renders light green on light backgrounds
dullYellowFg = makeFgStyle(common.NewColorPair("#9BA92F", "#6BCB94")) // renders light green on light backgrounds
redFg = makeFgStyle(common.Red)
faintRedFg = makeFgStyle(common.FaintRed)
2020-05-26 15:51:08 +00:00
)
func makeFgStyle(c common.ColorPair) func(string) string {
return te.Style{}.Foreground(c.Color()).Styled
}
func stashItemView(b *strings.Builder, m stashModel, index int, md *markdown) {
var (
truncateTo = m.terminalWidth - stashViewHorizontalPadding*2
gutter string
title = md.Note
date = relativeTime(md.CreatedAt)
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
}
case stashedMarkdown, convertedMarkdown:
icon = fileListingStashIcon
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 {
// Selected item
2020-05-26 15:51:08 +00:00
switch m.state {
case stashStatePromptDelete:
gutter = faintRedFg(verticalLine)
icon = faintRedFg(icon)
title = redFg(title)
date = faintRedFg(date)
2020-05-26 15:51:08 +00:00
case stashStateSettingNote:
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-10-26 14:52:54 +00:00
case stashStateSearchNotes:
if len(m.getNotes()) != 1 {
gutter = dimDullFuchsiaFg(verticalLine)
icon = dimDullFuchsiaFg(icon)
title = dimFuchsiaFg(title)
date = dimDullFuchsiaFg(date)
2020-10-26 14:52:54 +00:00
break
}
// If we've filtered down to exactly item color it as though it's
// not filtered, since pressing return will open it.
2020-10-26 14:52:54 +00:00
fallthrough
2020-05-26 15:51:08 +00:00
default:
gutter = dullFuchsiaFg(verticalLine)
icon = dullFuchsiaFg(icon)
title = fuchsiaFg(title)
date = dullFuchsiaFg(date)
2020-05-26 15:51:08 +00:00
}
} else {
// Regular (non-selected) items
2020-05-26 15:51:08 +00:00
if md.markdownType == newsMarkdown {
gutter = " "
2020-10-26 14:52:54 +00:00
if m.state == stashStateSearchNotes {
title = dimIndigoFg(title)
date = dimSubtleIndigoFg(date)
} else {
title = indigoFg(title)
date = subtleIndigoFg(date)
}
} else if m.state == stashStateSearchNotes {
icon = dimGreenFg(icon)
if title == noMemoTitle {
title = dimWarmGrayFg(title)
2020-10-26 14:52:54 +00:00
} else {
title = dimNormalFg(title)
2020-10-26 14:52:54 +00:00
}
gutter = " "
date = dimWarmGrayFg(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)
} else {
title = normalFg(title)
2020-05-26 15:51:08 +00:00
}
gutter = " "
date = warmGrayFg(date)
2020-05-26 15:51:08 +00:00
}
2020-10-26 14:52:54 +00:00
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
}