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
|
|
|
|
2021-06-14 20:52:00 +00:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
2024-07-03 15:11:29 +00:00
|
|
|
"github.com/charmbracelet/log"
|
2020-12-14 23:42:33 +00:00
|
|
|
"github.com/muesli/reflow/truncate"
|
2020-11-16 20:25:23 +00:00
|
|
|
"github.com/sahilm/fuzzy"
|
2020-05-26 15:51:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-08-25 13:56:17 +00:00
|
|
|
verticalLine = "│"
|
|
|
|
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 (
|
2024-07-03 15:11:29 +00:00
|
|
|
truncateTo = uint(m.common.width - stashViewHorizontalPadding*2)
|
|
|
|
gutter string
|
|
|
|
title = truncate.StringWithTail(md.Note, truncateTo, ellipsis)
|
|
|
|
date = md.relativeTime()
|
|
|
|
editedBy = ""
|
|
|
|
hasEditedBy = false
|
|
|
|
icon = ""
|
|
|
|
separator = ""
|
2020-08-25 17:32:49 +00:00
|
|
|
)
|
2020-05-26 15:51:08 +00:00
|
|
|
|
2020-12-08 23:38:10 +00:00
|
|
|
isSelected := index == m.cursor()
|
2020-11-26 01:15:21 +00:00
|
|
|
isFiltering := m.filterState == filtering
|
2020-12-17 23:31:08 +00:00
|
|
|
singleFilteredItem := isFiltering && len(m.getVisibleMarkdowns()) == 1
|
2020-11-10 23:48:34 +00:00
|
|
|
|
2020-12-17 23:31:08 +00:00
|
|
|
// If there are multiple items being filtered don't highlight a selected
|
2020-11-10 23:48:34 +00:00
|
|
|
// item in the results. If we've filtered down to one item, however,
|
|
|
|
// highlight that first item since pressing return will open it.
|
2020-11-26 01:15:21 +00:00
|
|
|
if isSelected && !isFiltering || singleFilteredItem {
|
2020-11-04 01:15:30 +00:00
|
|
|
// Selected item
|
2024-07-03 15:11:29 +00:00
|
|
|
if m.statusMessage == stashingStatusMessage {
|
|
|
|
gutter = greenFg(verticalLine)
|
|
|
|
icon = dimGreenFg(icon)
|
|
|
|
title = greenFg(title)
|
|
|
|
date = semiDimGreenFg(date)
|
|
|
|
editedBy = semiDimGreenFg(editedBy)
|
|
|
|
separator = semiDimGreenFg(separator)
|
|
|
|
} else {
|
|
|
|
gutter = dullFuchsiaFg(verticalLine)
|
|
|
|
if m.currentSection().key == filterSection &&
|
|
|
|
m.filterState == filterApplied || singleFilteredItem {
|
|
|
|
s := lipgloss.NewStyle().Foreground(fuchsia)
|
|
|
|
title = styleFilteredText(title, m.filterInput.Value(), s, s.Underline(true))
|
2020-11-13 23:34:59 +00:00
|
|
|
} else {
|
2024-07-03 15:11:29 +00:00
|
|
|
title = fuchsiaFg(title)
|
|
|
|
icon = fuchsiaFg(icon)
|
2020-11-13 23:34:59 +00:00
|
|
|
}
|
2024-07-03 15:11:29 +00:00
|
|
|
date = dimFuchsiaFg(date)
|
|
|
|
editedBy = dimDullFuchsiaFg(editedBy)
|
|
|
|
separator = dullFuchsiaFg(separator)
|
2020-05-26 15:51:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-12-17 23:31:08 +00:00
|
|
|
gutter = " "
|
2024-07-03 15:11:29 +00:00
|
|
|
if m.statusMessage == stashingStatusMessage {
|
2020-12-17 20:45:08 +00:00
|
|
|
icon = dimGreenFg(icon)
|
|
|
|
title = greenFg(title)
|
2020-12-17 23:31:08 +00:00
|
|
|
date = semiDimGreenFg(date)
|
2024-07-03 15:11:29 +00:00
|
|
|
editedBy = semiDimGreenFg(editedBy)
|
|
|
|
separator = semiDimGreenFg(separator)
|
2020-11-26 01:15:21 +00:00
|
|
|
} else if isFiltering && m.filterInput.Value() == "" {
|
2020-11-04 01:15:30 +00:00
|
|
|
icon = dimGreenFg(icon)
|
2024-07-03 15:11:29 +00:00
|
|
|
title = dimNormalFg(title)
|
2020-12-01 01:32:48 +00:00
|
|
|
date = dimBrightGrayFg(date)
|
2024-07-03 15:11:29 +00:00
|
|
|
editedBy = dimBrightGrayFg(editedBy)
|
|
|
|
separator = dimBrightGrayFg(separator)
|
2020-05-26 15:51:08 +00:00
|
|
|
} else {
|
2020-07-15 16:07:15 +00:00
|
|
|
icon = greenFg(icon)
|
2024-07-03 15:11:29 +00:00
|
|
|
|
|
|
|
s := lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#1a1a1a", Dark: "#dddddd"})
|
|
|
|
title = styleFilteredText(title, m.filterInput.Value(), s, s.Underline(true))
|
|
|
|
date = grayFg(date)
|
|
|
|
editedBy = midGrayFg(editedBy)
|
|
|
|
separator = brightGrayFg(separator)
|
2020-05-26 15:51:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-03 15:11:29 +00:00
|
|
|
fmt.Fprintf(b, "%s %s%s%s%s\n", gutter, icon, separator, separator, title)
|
2020-05-26 16:36:14 +00:00
|
|
|
fmt.Fprintf(b, "%s %s", gutter, date)
|
2024-07-03 15:11:29 +00:00
|
|
|
if hasEditedBy {
|
|
|
|
fmt.Fprintf(b, " %s", editedBy)
|
|
|
|
}
|
2020-05-26 15:51:08 +00:00
|
|
|
}
|
2020-11-13 23:34:59 +00:00
|
|
|
|
2021-06-14 20:52:00 +00:00
|
|
|
func styleFilteredText(haystack, needles string, defaultStyle, matchedStyle lipgloss.Style) string {
|
2020-11-13 23:34:59 +00:00
|
|
|
b := strings.Builder{}
|
2020-11-16 20:25:23 +00:00
|
|
|
|
2020-11-17 02:17:11 +00:00
|
|
|
normalizedHay, err := normalize(haystack)
|
2024-07-03 15:11:29 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("error normalizing", "haystack", haystack, "error", err)
|
2020-11-17 02:17:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
matches := fuzzy.Find(needles, []string{normalizedHay})
|
2020-11-16 20:25:23 +00:00
|
|
|
if len(matches) == 0 {
|
2021-06-14 20:52:00 +00:00
|
|
|
return defaultStyle.Render(haystack)
|
2020-11-16 20:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m := matches[0] // only one match exists
|
2020-11-17 02:17:11 +00:00
|
|
|
for i, rune := range []rune(haystack) {
|
2020-11-16 20:25:23 +00:00
|
|
|
styled := false
|
|
|
|
for _, mi := range m.MatchedIndexes {
|
|
|
|
if i == mi {
|
2021-06-14 20:52:00 +00:00
|
|
|
b.WriteString(matchedStyle.Render(string(rune)))
|
2020-11-16 20:25:23 +00:00
|
|
|
styled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !styled {
|
2021-06-14 20:52:00 +00:00
|
|
|
b.WriteString(defaultStyle.Render(string(rune)))
|
2020-11-13 23:34:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.String()
|
|
|
|
}
|