Skip to content

Commit

Permalink
feat(revisions): highlight selected revision
Browse files Browse the repository at this point in the history
  • Loading branch information
idursun committed Mar 7, 2025
1 parent ec2528d commit be80655
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 27 deletions.
5 changes: 5 additions & 0 deletions internal/ui/common/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ var DropStyle = lipgloss.NewStyle().
Foreground(Black).
Background(Red)

var HighlightedBackground = lipgloss.AdaptiveColor{
Light: "#A0A0A0",
Dark: "#282a36",
}

var DefaultPalette = Palette{
Normal: lipgloss.NewStyle(),
ImmutableNode: lipgloss.NewStyle().Foreground(IntenseCyan).Bold(true),
Expand Down
87 changes: 70 additions & 17 deletions internal/ui/graph/default_row_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,26 @@ import (
)

type DefaultRowRenderer struct {
Palette common.Palette
IsHighlighted bool
Op operations.Operation
Palette common.Palette
HighlightBackground lipgloss.AdaptiveColor
IsHighlighted bool
Op operations.Operation
}

func (s DefaultRowRenderer) RenderNormal(text string) string {
normal := s.Palette.Normal
if s.IsHighlighted {
normal = normal.Background(s.HighlightBackground)
}
return normal.Render(text)
}

func (s DefaultRowRenderer) RenderConnection(connectionType jj.ConnectionType) string {
return s.Palette.Normal.Render(string(connectionType))
normal := s.Palette.Normal
if s.IsHighlighted {
normal = normal.Background(s.HighlightBackground)
}
return normal.Render(string(connectionType))
}

func (s DefaultRowRenderer) RenderBefore(*jj.Commit) string {
Expand Down Expand Up @@ -45,7 +58,7 @@ func (s DefaultRowRenderer) RenderGlyph(connection jj.ConnectionType, commit *jj
}
opMarker := ""
if s.IsHighlighted {
style = s.Palette.Selected
style = s.Palette.Selected.Background(s.HighlightBackground)
if s.Op.RenderPosition() == operations.RenderPositionGlyph {
opMarker = s.Op.Render()
}
Expand All @@ -58,10 +71,18 @@ func (s DefaultRowRenderer) RenderTermination(connection jj.ConnectionType) stri
}

func (s DefaultRowRenderer) RenderChangeId(commit *jj.Commit) string {
changeId := s.Palette.ChangeId.Render("", commit.ChangeIdShort) + s.Palette.Rest.Render(commit.ChangeId[len(commit.ChangeIdShort):])
normalStyle := s.Palette.Normal
changeIdStyle := s.Palette.ChangeId
restStyle := s.Palette.Rest
if s.IsHighlighted {
normalStyle = normalStyle.Background(s.HighlightBackground)
changeIdStyle = changeIdStyle.Background(s.HighlightBackground)
restStyle = restStyle.Background(s.HighlightBackground)
}
changeId := changeIdStyle.Render("", commit.ChangeIdShort) + restStyle.Render(commit.ChangeId[len(commit.ChangeIdShort):])
hidden := ""
if commit.Hidden {
hidden = s.Palette.Normal.Render(" hidden")
hidden = normalStyle.Render(" hidden")
return lipgloss.JoinHorizontal(0, changeId, hidden)
}
return changeId
Expand All @@ -71,24 +92,44 @@ func (s DefaultRowRenderer) RenderCommitId(commit *jj.Commit) string {
if commit.IsRoot() {
return ""
}
return s.Palette.CommitId.Render("", commit.CommitIdShort) + s.Palette.Rest.Render(commit.CommitId[len(commit.ChangeIdShort):])
commitIdStyle := s.Palette.CommitId
restStyle := s.Palette.Rest
if s.IsHighlighted {
commitIdStyle = commitIdStyle.Background(s.HighlightBackground)
restStyle = restStyle.Background(s.HighlightBackground)
}
return commitIdStyle.Render("", commit.CommitIdShort) + restStyle.Render(commit.CommitId[len(commit.ChangeIdShort):])
}

func (s DefaultRowRenderer) RenderAuthor(commit *jj.Commit) string {
placeholderStyle := s.Palette.EmptyPlaceholder
authorStyle := s.Palette.Author
if s.IsHighlighted {
placeholderStyle = placeholderStyle.Background(s.HighlightBackground)
authorStyle = authorStyle.Background(s.HighlightBackground)
}
if commit.IsRoot() {
return s.Palette.EmptyPlaceholder.Render("root()")
return placeholderStyle.Render("root()")
}
return s.Palette.Author.Render("", commit.Author)
return authorStyle.Render("", commit.Author)
}

func (s DefaultRowRenderer) RenderDate(commit *jj.Commit) string {
if commit.IsRoot() {
return ""
}
return s.Palette.Timestamp.Render("", commit.Timestamp)
timestamp := s.Palette.Timestamp
if s.IsHighlighted {
timestamp = timestamp.Background(s.HighlightBackground)
}
return timestamp.Render("", commit.Timestamp)
}

func (s DefaultRowRenderer) RenderBookmarks(commit *jj.Commit) string {
bookmarksStyle := s.Palette.Bookmarks
if s.IsHighlighted {
bookmarksStyle = bookmarksStyle.Background(s.HighlightBackground)
}
var w strings.Builder
if s.IsHighlighted && s.Op.RenderPosition() == operations.RenderPositionBookmark {
w.WriteString(" ")
Expand All @@ -98,30 +139,42 @@ func (s DefaultRowRenderer) RenderBookmarks(commit *jj.Commit) string {
var bookmarks []string
bookmarks = append(bookmarks, "")
bookmarks = append(bookmarks, commit.Bookmarks...)
w.WriteString(s.Palette.Bookmarks.Render(bookmarks...))
w.WriteString(bookmarksStyle.Render(bookmarks...))
}
return w.String()
}

func (s DefaultRowRenderer) RenderMarkers(commit *jj.Commit) string {
conflictStyle := s.Palette.Conflict
if s.IsHighlighted {
conflictStyle = conflictStyle.Background(s.HighlightBackground)
}
if commit.Conflict {
return s.Palette.Conflict.Render("conflict")
return conflictStyle.Render(" conflict")
}
return ""
}

func (s DefaultRowRenderer) RenderDescription(commit *jj.Commit) string {
emptyPlaceholderStyle := s.Palette.EmptyPlaceholder
placeholderStyle := s.Palette.Placeholder
normalStyle := s.Palette.Normal
if s.IsHighlighted {
emptyPlaceholderStyle = emptyPlaceholderStyle.Background(s.HighlightBackground)
placeholderStyle = placeholderStyle.Background(s.HighlightBackground)
normalStyle = normalStyle.Background(s.HighlightBackground)
}
if s.IsHighlighted && s.Op.RenderPosition() == operations.RenderPositionDescription {
return s.Op.Render()
}

if commit.Empty && commit.Description == "" {
return s.Palette.EmptyPlaceholder.Render(" (empty) (no description set)")
return emptyPlaceholderStyle.Render(" (empty) (no description set)")
}
if commit.Empty {
return lipgloss.JoinHorizontal(0, s.Palette.EmptyPlaceholder.Render(" (empty)", s.Palette.Normal.Render(commit.Description)))
return lipgloss.JoinHorizontal(0, emptyPlaceholderStyle.Render(" (empty)", normalStyle.Render(commit.Description)))
} else if commit.Description == "" {
return s.Palette.Placeholder.Render(" (no description set)")
return placeholderStyle.Render(" (no description set)")
}
return s.Palette.Normal.Render("", commit.Description)
return normalStyle.Render("", commit.Description)
}
33 changes: 26 additions & 7 deletions internal/ui/graph/graph_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"fmt"
"github.com/charmbracelet/lipgloss"
"github.com/idursun/jjui/internal/jj"
"slices"
"strings"
Expand All @@ -17,6 +18,7 @@ type GraphWriter struct {
connectionsWritten bool
renderer RowRenderer
row jj.GraphRow
Width int
}

func (w *GraphWriter) Write(p []byte) (n int, err error) {
Expand Down Expand Up @@ -86,12 +88,22 @@ func (w *GraphWriter) RenderRow(row jj.GraphRow, renderer RowRenderer) {
}
w.connectionsWritten = false
w.connections = row.Connections[0]
fmt.Fprint(w, renderer.RenderChangeId(row.Commit))
fmt.Fprint(w, renderer.RenderAuthor(row.Commit))
fmt.Fprint(w, renderer.RenderDate(row.Commit))
fmt.Fprint(w, renderer.RenderBookmarks(row.Commit))
fmt.Fprint(w, renderer.RenderMarkers(row.Commit))
fmt.Fprint(w, renderer.RenderCommitId(row.Commit))
lw := strings.Builder{}
prefix := len(w.connections)*2 + 1
fmt.Fprint(&lw, renderer.RenderChangeId(row.Commit))
fmt.Fprint(&lw, renderer.RenderAuthor(row.Commit))
fmt.Fprint(&lw, renderer.RenderDate(row.Commit))
fmt.Fprint(&lw, renderer.RenderBookmarks(row.Commit))
fmt.Fprint(&lw, renderer.RenderMarkers(row.Commit))
fmt.Fprint(&lw, renderer.RenderCommitId(row.Commit))
line := lw.String()
width := lipgloss.Width(line)
fmt.Fprint(w, line)

gap := w.Width - prefix - width
if gap > 0 {
fmt.Fprint(w, renderer.RenderNormal(strings.Repeat(" ", gap)))
}
fmt.Fprintln(w)

if row.Commit.IsRoot() {
Expand All @@ -113,7 +125,14 @@ func (w *GraphWriter) RenderRow(row jj.GraphRow, renderer RowRenderer) {
} else {
w.connections = extendConnections(row.Connections[0])
}
fmt.Fprintln(w, line)
prefix = len(w.connections)*2 + 1
width = lipgloss.Width(line)
fmt.Fprint(w, line)
gap = w.Width - prefix - width
if gap > 0 {
fmt.Fprint(w, renderer.RenderNormal(strings.Repeat(" ", gap)))
}
fmt.Fprintln(w)
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/ui/graph/row_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ type RowRenderer interface {
RenderDescription(commit *jj.Commit) string
RenderMarkers(commit *jj.Commit) string
RenderConnection(connectionType jj.ConnectionType) string
RenderNormal(text string) string
}
8 changes: 5 additions & 3 deletions internal/ui/revisions/revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,15 @@ func (m *Model) View() string {
}

var w graph.GraphWriter
w.Width = m.width
selectedLineStart := -1
selectedLineEnd := -1
for i, row := range m.rows {
nodeRenderer := graph.DefaultRowRenderer{
Palette: common.DefaultPalette,
Op: m.op,
IsHighlighted: i == m.cursor,
Palette: common.DefaultPalette,
HighlightBackground: common.HighlightedBackground,
Op: m.op,
IsHighlighted: i == m.cursor,
}

if i == m.cursor {
Expand Down
4 changes: 4 additions & 0 deletions test/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ type TestRenderer struct {
highlighted bool
}

func (t TestRenderer) RenderNormal(text string) string {
return text
}

func (t TestRenderer) RenderConnection(connectionType jj.ConnectionType) string {
return string(connectionType)
}
Expand Down

0 comments on commit be80655

Please sign in to comment.