-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
249 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,27 @@ | ||
module diffrn | ||
|
||
go 1.21.6 | ||
|
||
require ( | ||
github.com/charmbracelet/bubbles v0.18.0 | ||
github.com/charmbracelet/bubbletea v0.26.2 | ||
github.com/charmbracelet/lipgloss v0.10.0 | ||
) | ||
|
||
require ( | ||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect | ||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect | ||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect | ||
github.com/mattn/go-isatty v0.0.18 // indirect | ||
github.com/mattn/go-localereader v0.0.1 // indirect | ||
github.com/mattn/go-runewidth v0.0.15 // indirect | ||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect | ||
github.com/muesli/cancelreader v0.2.2 // indirect | ||
github.com/muesli/reflow v0.3.0 // indirect | ||
github.com/muesli/termenv v0.15.2 // indirect | ||
github.com/rivo/uniseg v0.4.7 // indirect | ||
golang.org/x/sync v0.7.0 // indirect | ||
golang.org/x/sys v0.20.0 // indirect | ||
golang.org/x/term v0.20.0 // indirect | ||
golang.org/x/text v0.3.8 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= | ||
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= | ||
github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/39KLfy0= | ||
github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw= | ||
github.com/charmbracelet/bubbletea v0.26.2 h1:Eeb+n75Om9gQ+I6YpbCXQRKHt5Pn4vMwusQpwLiEgJQ= | ||
github.com/charmbracelet/bubbletea v0.26.2/go.mod h1:6I0nZ3YHUrQj7YHIHlM8RySX4ZIthTliMY+W8X8b+Gs= | ||
github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s= | ||
github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE= | ||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4= | ||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= | ||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= | ||
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= | ||
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= | ||
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= | ||
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= | ||
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= | ||
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= | ||
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= | ||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= | ||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= | ||
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= | ||
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= | ||
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= | ||
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= | ||
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= | ||
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= | ||
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= | ||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= | ||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= | ||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= | ||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= | ||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= | ||
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= | ||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw= | ||
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= | ||
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= | ||
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,5 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os/exec" | ||
) | ||
|
||
func main() { | ||
cmd := exec.Command("git", "diff") | ||
stdout, err := cmd.Output() | ||
|
||
if err != nil { | ||
fmt.Println(err.Error()) | ||
return | ||
} | ||
diff, _ := parseDiff(string(stdout)) | ||
diffJson, _ := json.MarshalIndent(diff, "", " ") | ||
fmt.Println(string(diffJson)) | ||
render() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"time" | ||
|
||
"github.com/charmbracelet/bubbles/viewport" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/lipgloss" | ||
) | ||
|
||
type model struct { | ||
content string | ||
ready bool | ||
viewport viewport.Model | ||
diff Diff | ||
} | ||
|
||
type refreshMsg struct { | ||
rawDiff string | ||
} | ||
|
||
func (m model) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func buildOutput(m model) string { | ||
var content strings.Builder | ||
if len(m.diff.Files) == 0 { | ||
return "No diff to show! Working directory is clean." | ||
} | ||
|
||
for _, file := range m.diff.Files { | ||
content.WriteString(fileStyle.Width(m.viewport.Width).Render(file.Name)) | ||
content.WriteString("\n") | ||
for blockI, block := range file.Blocks { | ||
for _, line := range block.Lines { | ||
if strings.HasPrefix(line, "-") { | ||
content.WriteString(removedStyle.Width(m.viewport.Width).Render(line)) | ||
} else if strings.HasPrefix(line, "+") { | ||
content.WriteString(addedStyle.Width(m.viewport.Width).Render(line)) | ||
} else { | ||
content.WriteString(line) | ||
} | ||
content.WriteString("\n") | ||
} | ||
if blockI < len(file.Blocks)-1 { | ||
content.WriteString(hr.Render("···")) | ||
content.WriteString("\n") | ||
} | ||
} | ||
} | ||
return content.String() | ||
} | ||
|
||
func (m *model) windowSizeUpdate(msg tea.WindowSizeMsg) { | ||
headerHeight := lipgloss.Height(m.headerView()) | ||
footerHeight := lipgloss.Height(m.footerView()) | ||
verticalMarginHeight := headerHeight + footerHeight | ||
|
||
if !m.ready { | ||
m.viewport = viewport.New(msg.Width, msg.Height-verticalMarginHeight) | ||
m.viewport.YPosition = headerHeight | ||
m.viewport.SetContent(m.content) | ||
m.ready = true | ||
} else { | ||
m.viewport.Width = msg.Width | ||
m.viewport.Height = msg.Height - verticalMarginHeight | ||
} | ||
} | ||
|
||
func (m *model) refreshUpdate(msg refreshMsg) { | ||
m.diff, _ = parseDiff(msg.rawDiff) | ||
m.viewport.SetContent(buildOutput(*m)) | ||
} | ||
|
||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
var ( | ||
cmd tea.Cmd | ||
cmds []tea.Cmd | ||
) | ||
|
||
switch msg := msg.(type) { | ||
case refreshMsg: | ||
m.refreshUpdate(msg) | ||
case tea.KeyMsg: | ||
if k := msg.String(); k == "ctrl+c" || k == "q" || k == "esc" { | ||
return m, tea.Quit | ||
} | ||
|
||
case tea.WindowSizeMsg: | ||
m.windowSizeUpdate(msg) | ||
} | ||
|
||
m.viewport, cmd = m.viewport.Update(msg) | ||
cmds = append(cmds, cmd) | ||
|
||
return m, tea.Batch(cmds...) | ||
} | ||
|
||
func (m model) View() string { | ||
if !m.ready { | ||
return "\n Initializing..." | ||
} | ||
return fmt.Sprintf("%s\n%s\n%s", m.headerView(), m.viewport.View(), m.footerView()) | ||
} | ||
|
||
func (m model) headerView() string { | ||
title := titleStyle.Render("diffrn") | ||
return lipgloss.JoinHorizontal(lipgloss.Center, title) | ||
} | ||
|
||
func (m model) footerView() string { | ||
info := infoStyle.Render(fmt.Sprintf("%3.f%%", m.viewport.ScrollPercent()*100)) | ||
line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(info))) | ||
return lipgloss.JoinHorizontal(lipgloss.Center, line, info) | ||
} | ||
|
||
func max(a, b int) int { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} | ||
|
||
func gitDiffRaw() string { | ||
cmd := exec.Command("git", "diff") | ||
stdout, _ := cmd.Output() | ||
return string(stdout) | ||
} | ||
|
||
func render() { | ||
p := tea.NewProgram( | ||
model{}, | ||
tea.WithAltScreen(), | ||
tea.WithMouseCellMotion(), | ||
) | ||
|
||
go func() { | ||
for { | ||
pause := time.Duration(1) * time.Second | ||
p.Send(refreshMsg{rawDiff: gitDiffRaw()}) | ||
time.Sleep(pause) | ||
} | ||
}() | ||
|
||
if _, err := p.Run(); err != nil { | ||
fmt.Println("could not run program:", err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import "github.com/charmbracelet/lipgloss" | ||
|
||
var fileStyle = lipgloss.NewStyle(). | ||
Bold(true). | ||
Foreground(lipgloss.Color("#FAFAFA")). | ||
Background(lipgloss.Color("#7D56F4")). | ||
Width(100) | ||
|
||
var removedStyle = lipgloss.NewStyle(). | ||
Background(lipgloss.Color("#ffe8e7")). | ||
Width(100) | ||
|
||
var addedStyle = lipgloss.NewStyle(). | ||
Background(lipgloss.Color("#f0f8ec")). | ||
Width(100) | ||
|
||
var hr = lipgloss.NewStyle(). | ||
Background(lipgloss.Color("#f6f6f6")). | ||
Width(100) | ||
|
||
var titleStyle = lipgloss.NewStyle().Padding(0, 0) | ||
|
||
var infoStyle = func() lipgloss.Style { | ||
b := lipgloss.RoundedBorder() | ||
b.Left = "┤" | ||
return titleStyle.Copy().BorderStyle(b) | ||
}() |