glow/ui/stashitem.go

109 lines
2.6 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
2020-07-15 00:25:51 +00:00
rw "github.com/mattn/go-runewidth"
2020-05-26 15:51:08 +00:00
)
const (
newsPrefix = "News: "
verticalLine = "│"
noMemoTitle = "No Memo"
fileListingStashIcon = "• "
2020-05-26 15:51:08 +00:00
)
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)
}
isSelected := index == m.index
notSearchingNotes := m.state != stashStateSearchNotes
// If there are multiple items being filtered we don't highlight a selected
// item in the results. If we've filtered down to one item, however,
// highlight that first item since pressing return will open it.
singleFilteredItem := m.state == stashStateSearchNotes && len(m.getNotes()) == 1
if isSelected && notSearchingNotes || singleFilteredItem {
// 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 = ""
title = m.noteInput.View()
date = dullYellowFg(date)
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 = " "
if m.state == stashStateSearchNotes && m.searchInput.Value() == "" {
title = dimIndigoFg(title)
date = dimSubtleIndigoFg(date)
} else {
title = indigoFg(title)
date = subtleIndigoFg(date)
}
} else if m.state == stashStateSearchNotes && m.searchInput.Value() == "" {
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
}