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-07-15 00:25:51 +00:00
|
|
|
rw "github.com/mattn/go-runewidth"
|
2020-05-26 15:51:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-08-25 13:56:17 +00:00
|
|
|
newsPrefix = "News: "
|
|
|
|
verticalLine = "│"
|
|
|
|
noMemoTitle = "No Memo"
|
|
|
|
fileListingStashIcon = "• "
|
2020-05-26 15:51:08 +00:00
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-11-10 23:48:34 +00:00
|
|
|
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 {
|
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-11-13 21:17:50 +00:00
|
|
|
title = m.noteInput.View()
|
2020-07-15 16:07:15 +00:00
|
|
|
date = dullYellowFg(date)
|
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-11-04 17:15:03 +00:00
|
|
|
if m.state == stashStateSearchNotes && m.searchInput.Value() == "" {
|
2020-11-04 01:15:30 +00:00
|
|
|
title = dimIndigoFg(title)
|
|
|
|
date = dimSubtleIndigoFg(date)
|
|
|
|
} else {
|
|
|
|
title = indigoFg(title)
|
|
|
|
date = subtleIndigoFg(date)
|
|
|
|
}
|
2020-11-04 17:15:03 +00:00
|
|
|
} else if m.state == stashStateSearchNotes && m.searchInput.Value() == "" {
|
2020-11-04 01:15:30 +00:00
|
|
|
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
|
|
|
}
|