2020-05-26 15:51:08 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-05-26 16:36:14 +00:00
|
|
|
"strings"
|
2020-05-26 15:51:08 +00:00
|
|
|
|
2020-05-26 17:42:25 +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 (
|
2020-08-25 13:56:17 +00:00
|
|
|
newsPrefix = "News: "
|
|
|
|
verticalLine = "│"
|
|
|
|
noMemoTitle = "No Memo"
|
|
|
|
fileListingStashIcon = "• "
|
2020-05-26 15:51:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-11-04 01:15:30 +00:00
|
|
|
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
|
|
|
)
|
|
|
|
|
2020-11-04 01:15:30 +00:00
|
|
|
func makeFgStyle(c common.ColorPair) func(string) string {
|
|
|
|
return te.Style{}.Foreground(c.Color()).Styled
|
|
|
|
}
|
|
|
|
|
2020-05-26 19:10:45 +00:00
|
|
|
func stashItemView(b *strings.Builder, m stashModel, index int, md *markdown) {
|
2020-08-25 17:32:49 +00:00
|
|
|
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
|
|
|
}
|
2020-08-24 18:56:23 +00:00
|
|
|
case stashedMarkdown, convertedMarkdown:
|
2020-08-25 13:56:17 +00:00
|
|
|
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 {
|
2020-11-04 01:15:30 +00:00
|
|
|
// Selected item
|
|
|
|
|
2020-05-26 15:51:08 +00:00
|
|
|
switch m.state {
|
|
|
|
case stashStatePromptDelete:
|
2020-07-15 16:07:15 +00:00
|
|
|
gutter = faintRedFg(verticalLine)
|
|
|
|
icon = faintRedFg(icon)
|
|
|
|
title = redFg(title)
|
|
|
|
date = faintRedFg(date)
|
2020-05-26 15:51:08 +00:00
|
|
|
case stashStateSettingNote:
|
2020-07-15 16:07:15 +00:00
|
|
|
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)
|
2020-07-15 16:07:15 +00:00
|
|
|
date = dullYellowFg(date)
|
2020-10-26 14:52:54 +00:00
|
|
|
case stashStateSearchNotes:
|
|
|
|
if len(m.getNotes()) != 1 {
|
2020-11-04 01:15:30 +00:00
|
|
|
gutter = dimDullFuchsiaFg(verticalLine)
|
|
|
|
icon = dimDullFuchsiaFg(icon)
|
|
|
|
title = dimFuchsiaFg(title)
|
|
|
|
date = dimDullFuchsiaFg(date)
|
2020-10-26 14:52:54 +00:00
|
|
|
break
|
|
|
|
}
|
2020-11-04 01:15:30 +00:00
|
|
|
// 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:
|
2020-07-15 16:07:15 +00:00
|
|
|
gutter = dullFuchsiaFg(verticalLine)
|
|
|
|
icon = dullFuchsiaFg(icon)
|
|
|
|
title = fuchsiaFg(title)
|
|
|
|
date = dullFuchsiaFg(date)
|
2020-05-26 15:51:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-11-04 01:15:30 +00:00
|
|
|
// 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 {
|
2020-11-04 01:15:30 +00:00
|
|
|
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 {
|
2020-11-04 01:15:30 +00:00
|
|
|
title = dimNormalFg(title)
|
2020-10-26 14:52:54 +00:00
|
|
|
}
|
2020-11-04 01:15:30 +00:00
|
|
|
gutter = " "
|
|
|
|
date = dimWarmGrayFg(date)
|
|
|
|
|
2020-05-26 15:51:08 +00:00
|
|
|
} else {
|
2020-07-15 16:07:15 +00:00
|
|
|
icon = greenFg(icon)
|
2020-05-26 15:51:08 +00:00
|
|
|
if title == noMemoTitle {
|
2020-07-15 16:07:15 +00:00
|
|
|
title = warmGrayFg(title)
|
2020-11-04 01:15:30 +00:00
|
|
|
} else {
|
|
|
|
title = normalFg(title)
|
2020-05-26 15:51:08 +00:00
|
|
|
}
|
|
|
|
gutter = " "
|
2020-07-15 16:07:15 +00:00
|
|
|
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)
|
2020-05-26 16:36:14 +00:00
|
|
|
fmt.Fprintf(b, "%s %s", gutter, date)
|
2020-05-26 15:51:08 +00:00
|
|
|
}
|