remove node dependency and implement diff format

Kujtim Hoxha created

Change summary

go.mod                                       |  24 
go.sum                                       |  57 -
internal/diff/diff.go                        | 995 ++++++++++++++++++++++
internal/git/diff.go                         | 264 -----
internal/llm/tools/edit.go                   |  39 
internal/llm/tools/write.go                  |  15 
internal/tui/components/dialog/permission.go |   6 
7 files changed, 1,028 insertions(+), 372 deletions(-)

Detailed changes

go.mod 🔗

@@ -7,6 +7,7 @@ toolchain go1.24.2
 require (
 	github.com/JohannesKaufmann/html-to-markdown v1.6.0
 	github.com/PuerkitoBio/goquery v1.9.2
+	github.com/alecthomas/chroma/v2 v2.15.0
 	github.com/anthropics/anthropic-sdk-go v0.2.0-beta.2
 	github.com/bmatcuk/doublestar/v4 v4.8.1
 	github.com/catppuccin/go v0.3.0
@@ -17,7 +18,6 @@ require (
 	github.com/charmbracelet/lipgloss v1.1.0
 	github.com/charmbracelet/x/ansi v0.8.0
 	github.com/fsnotify/fsnotify v1.8.0
-	github.com/go-git/go-git/v5 v5.15.0
 	github.com/go-logfmt/logfmt v0.6.0
 	github.com/golang-migrate/migrate/v4 v4.18.2
 	github.com/google/generative-ai-go v0.19.0
@@ -31,6 +31,7 @@ require (
 	github.com/muesli/reflow v0.3.0
 	github.com/muesli/termenv v0.16.0
 	github.com/openai/openai-go v0.1.0-beta.2
+	github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3
 	github.com/spf13/cobra v1.9.1
 	github.com/spf13/viper v1.20.0
 	github.com/stretchr/testify v1.10.0
@@ -45,10 +46,6 @@ require (
 	cloud.google.com/go/auth/oauth2adapt v0.2.6 // indirect
 	cloud.google.com/go/compute/metadata v0.6.0 // indirect
 	cloud.google.com/go/longrunning v0.5.7 // indirect
-	dario.cat/mergo v1.0.0 // indirect
-	github.com/Microsoft/go-winio v0.6.2 // indirect
-	github.com/ProtonMail/go-crypto v1.1.6 // indirect
-	github.com/alecthomas/chroma/v2 v2.15.0 // indirect
 	github.com/andybalholm/cascadia v1.3.2 // indirect
 	github.com/atotto/clipboard v0.1.4 // indirect
 	github.com/aws/aws-sdk-go-v2 v1.30.3 // indirect
@@ -71,20 +68,15 @@ require (
 	github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect
 	github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0 // indirect
 	github.com/charmbracelet/x/term v0.2.1 // indirect
-	github.com/cloudflare/circl v1.6.1 // indirect
-	github.com/cyphar/filepath-securejoin v0.4.1 // indirect
 	github.com/davecgh/go-spew v1.1.1 // indirect
 	github.com/dlclark/regexp2 v1.11.4 // indirect
 	github.com/dustin/go-humanize v1.0.1 // indirect
-	github.com/emirpasic/gods v1.18.1 // indirect
 	github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
 	github.com/felixge/httpsnoop v1.0.4 // indirect
-	github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
-	github.com/go-git/go-billy/v5 v5.6.2 // indirect
 	github.com/go-logr/logr v1.4.2 // indirect
 	github.com/go-logr/stdr v1.2.2 // indirect
 	github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
-	github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
+	github.com/google/go-cmp v0.7.0 // indirect
 	github.com/google/s2a-go v0.1.8 // indirect
 	github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
 	github.com/googleapis/gax-go/v2 v2.14.1 // indirect
@@ -92,8 +84,6 @@ require (
 	github.com/hashicorp/errwrap v1.1.0 // indirect
 	github.com/hashicorp/go-multierror v1.1.1 // indirect
 	github.com/inconshreveable/mousetrap v1.1.0 // indirect
-	github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
-	github.com/kevinburke/ssh_config v1.2.0 // indirect
 	github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
 	github.com/mattn/go-isatty v0.0.20 // indirect
 	github.com/mattn/go-localereader v0.0.1 // indirect
@@ -101,13 +91,11 @@ require (
 	github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
 	github.com/muesli/cancelreader v0.2.2 // indirect
 	github.com/pelletier/go-toml/v2 v2.2.3 // indirect
-	github.com/pjbgf/sha1cd v0.3.2 // indirect
 	github.com/pmezard/go-difflib v1.0.0 // indirect
 	github.com/rivo/uniseg v0.4.7 // indirect
+	github.com/rogpeppe/go-internal v1.14.1 // indirect
 	github.com/sagikazarmark/locafero v0.7.0 // indirect
 	github.com/sahilm/fuzzy v0.1.1 // indirect
-	github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
-	github.com/skeema/knownhosts v1.3.1 // indirect
 	github.com/sourcegraph/conc v0.3.0 // indirect
 	github.com/spf13/afero v1.12.0 // indirect
 	github.com/spf13/cast v1.7.1 // indirect
@@ -117,7 +105,6 @@ require (
 	github.com/tidwall/match v1.1.1 // indirect
 	github.com/tidwall/pretty v1.2.1 // indirect
 	github.com/tidwall/sjson v1.2.5 // indirect
-	github.com/xanzy/ssh-agent v0.3.3 // indirect
 	github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
 	github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
 	github.com/yuin/goldmark v1.7.8 // indirect
@@ -131,6 +118,7 @@ require (
 	go.uber.org/multierr v1.9.0 // indirect
 	golang.design/x/clipboard v0.7.0 // indirect
 	golang.org/x/crypto v0.37.0 // indirect
+	golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
 	golang.org/x/exp/shiny v0.0.0-20250305212735-054e65f0b394 // indirect
 	golang.org/x/image v0.14.0 // indirect
 	golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a // indirect
@@ -144,6 +132,6 @@ require (
 	google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8 // indirect
 	google.golang.org/grpc v1.67.3 // indirect
 	google.golang.org/protobuf v1.36.1 // indirect
-	gopkg.in/warnings.v0 v0.1.2 // indirect
+	gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
 	gopkg.in/yaml.v3 v3.0.1 // indirect
 )

go.sum 🔗

@@ -10,17 +10,10 @@ cloud.google.com/go/compute/metadata v0.6.0 h1:A6hENjEsCDtC1k8byVsgwvVcioamEHvZ4
 cloud.google.com/go/compute/metadata v0.6.0/go.mod h1:FjyFAW1MW0C203CEOMDTu3Dk1FlqW3Rga40jzHL4hfg=
 cloud.google.com/go/longrunning v0.5.7 h1:WLbHekDbjK1fVFD3ibpFFVoyizlLRl73I7YKuAKilhU=
 cloud.google.com/go/longrunning v0.5.7/go.mod h1:8GClkudohy1Fxm3owmBGid8W0pSgodEMwEAztp38Xng=
-dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
-dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
 github.com/JohannesKaufmann/html-to-markdown v1.6.0 h1:04VXMiE50YYfCfLboJCLcgqF5x+rHJnb1ssNmqpLH/k=
 github.com/JohannesKaufmann/html-to-markdown v1.6.0/go.mod h1:NUI78lGg/a7vpEJTz/0uOcYMaibytE4BUOQS8k78yPQ=
 github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
 github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
-github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
-github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
-github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
-github.com/ProtonMail/go-crypto v1.1.6 h1:ZcV+Ropw6Qn0AX9brlQLAUXfqLBc7Bl+f/DmNxpLfdw=
-github.com/ProtonMail/go-crypto v1.1.6/go.mod h1:rA3QumHc/FZ8pAHreoekgiAbzpNsfQAosU5td4SnOrE=
 github.com/PuerkitoBio/goquery v1.9.2 h1:4/wZksC3KgkQw7SQgkKotmKljk0M6V8TUvA8Wb4yPeE=
 github.com/PuerkitoBio/goquery v1.9.2/go.mod h1:GHPCaP0ODyyxqcNoFGYlAprUFH81NuRPd0GX3Zu2Mvk=
 github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0=
@@ -31,12 +24,8 @@ github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc
 github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
 github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
 github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
-github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
-github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
 github.com/anthropics/anthropic-sdk-go v0.2.0-beta.2 h1:h7qxtumNjKPWFv1QM/HJy60MteeW23iKeEtBoY7bYZk=
 github.com/anthropics/anthropic-sdk-go v0.2.0-beta.2/go.mod h1:AapDW22irxK2PSumZiQXYUFvsdQgkwIWlpESweWZI/c=
-github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
-github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
 github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
 github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
 github.com/aws/aws-sdk-go-v2 v1.30.3 h1:jUeBtG0Ih+ZIFH0F4UkmL9w3cSpaMv9tYYDbzILP8dY=
@@ -99,11 +88,7 @@ github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0 h1:qko
 github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0/go.mod h1:pBhA0ybfXv6hDjQUZ7hk1lVxBiUbupdw5R31yPUViVQ=
 github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
 github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
-github.com/cloudflare/circl v1.6.1 h1:zqIqSPIndyBh1bjLVVDHMPpVKqp8Su/V+6MeDzzQBQ0=
-github.com/cloudflare/circl v1.6.1/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs=
 github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
-github.com/cyphar/filepath-securejoin v0.4.1 h1:JyxxyPEaktOD+GAnqIqTf9A8tHyAG22rowi7HkoSU1s=
-github.com/cyphar/filepath-securejoin v0.4.1/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGLDGQL7h7bg04C/+u9jI=
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -111,10 +96,6 @@ github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yA
 github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
 github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
 github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
-github.com/elazarl/goproxy v1.7.2 h1:Y2o6urb7Eule09PjlhQRGNsqRfPmYI3KKQLFpCAV3+o=
-github.com/elazarl/goproxy v1.7.2/go.mod h1:82vkLNir0ALaW14Rc399OTTjyNREgmdL2cVoIbS6XaE=
-github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
-github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
 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/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
@@ -123,16 +104,6 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
 github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
 github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
 github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
-github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c=
-github.com/gliderlabs/ssh v0.3.8/go.mod h1:xYoytBv1sV0aL3CavoDuJIQNURXkkfPA/wxQ1pL1fAU=
-github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=
-github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic=
-github.com/go-git/go-billy/v5 v5.6.2 h1:6Q86EsPXMa7c3YZ3aLAQsMA0VlWmy43r6FHqa/UNbRM=
-github.com/go-git/go-billy/v5 v5.6.2/go.mod h1:rcFC2rAsp/erv7CMz9GczHcuD0D32fWzH+MJAU+jaUU=
-github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4=
-github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII=
-github.com/go-git/go-git/v5 v5.15.0 h1:f5Qn0W0F7ry1iN0ZwIU5m/n7/BKB4hiZfc+zlZx7ly0=
-github.com/go-git/go-git/v5 v5.15.0/go.mod h1:4Ge4alE/5gPs30F2H1esi2gPd69R0C39lolkucHBOp8=
 github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
 github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
 github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
@@ -144,8 +115,6 @@ github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIx
 github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
 github.com/golang-migrate/migrate/v4 v4.18.2 h1:2VSCMz7x7mjyTXx3m2zPokOY82LTRgxK1yQYKo6wWQ8=
 github.com/golang-migrate/migrate/v4 v4.18.2/go.mod h1:2CM6tJvn2kqPXwnXO/d3rAQYiyoIm180VsO8PRX6Rpk=
-github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8JmEHVZIycC7hBoQxHH9pNKQORJNozsQ=
-github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw=
 github.com/google/generative-ai-go v0.19.0 h1:R71szggh8wHMCUlEMsW2A/3T+5LdEIkiaHSYgSpUgdg=
 github.com/google/generative-ai-go v0.19.0/go.mod h1:JYolL13VG7j79kM5BtHz4qwONHkeJQzOCkKXnpqtS/E=
 github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
@@ -169,11 +138,8 @@ github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUq
 github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
 github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
 github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
-github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
-github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
-github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
-github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
 github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
+github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
 github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
 github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
 github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -213,17 +179,11 @@ 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.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
 github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
-github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
-github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY=
 github.com/openai/openai-go v0.1.0-beta.2 h1:Ra5nCFkbEl9w+UJwAciC4kqnIBUCcJazhmMA0/YN894=
 github.com/openai/openai-go v0.1.0-beta.2/go.mod h1:g461MYGXEXBVdV5SaR/5tNzNbSfwTBBefwc+LlDCK0Y=
 github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
 github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
-github.com/pjbgf/sha1cd v0.3.2 h1:a9wb0bp1oC2TGwStyn0Umc/IGKQnEgF0vVaZ8QF8eo4=
-github.com/pjbgf/sha1cd v0.3.2/go.mod h1:zQWigSxVmsHEZow5qaLtPYxpcKMMQpa09ixqBxuCS6A=
 github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
-github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
-github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
@@ -243,9 +203,6 @@ github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAm
 github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
 github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
 github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
-github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
-github.com/skeema/knownhosts v1.3.1 h1:X2osQ+RAjK76shCbvhHHHVl3ZlgDm8apHEHFqRjnBY8=
-github.com/skeema/knownhosts v1.3.1/go.mod h1:r7KTdC8l4uxWRyK2TpQZ/1o5HaSzh06ePQNxPwTcfiY=
 github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
 github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
 github.com/spf13/afero v1.12.0 h1:UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs=
@@ -259,7 +216,6 @@ github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
 github.com/spf13/viper v1.20.0 h1:zrxIyR3RQIOsarIrgL8+sAvALXul9jeEPa06Y0Ph6vY=
 github.com/spf13/viper v1.20.0/go.mod h1:P9Mdzt1zoHIG8m2eZQinpiBjo6kCmZSKBClNNqjJvu4=
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
-github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
 github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
 github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
@@ -276,8 +232,6 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
 github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
 github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
 github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
-github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
-github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
 github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
 github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
 github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
@@ -306,7 +260,6 @@ golang.design/x/clipboard v0.7.0 h1:4Je8M/ys9AJumVnl8m+rZnIvstSnYj1fvzqYrU3TXvo=
 golang.design/x/clipboard v0.7.0/go.mod h1:PQIvqYO9GP29yINEfsEn5zSQKAz3UgXmZKzDA6dnq2E=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
 golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
-golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
 golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
 golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
 golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
@@ -324,7 +277,6 @@ golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91
 golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
 golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
 golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
-golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
 golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
 golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
 golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
@@ -342,14 +294,10 @@ golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610=
 golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
 golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
-golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -372,7 +320,6 @@ golang.org/x/term v0.31.0 h1:erwDkOK1Msy6offm1mOgvspSkslFnIGsFnxOKoufg3o=
 golang.org/x/term v0.31.0/go.mod h1:R4BeIy7D95HzImkxGkTW1UQTtP54tio2RyHz7PwK0aw=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
-golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
 golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
 golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
@@ -401,8 +348,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
-gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
-gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
 gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

internal/diff/diff.go 🔗

@@ -0,0 +1,995 @@
+package diff
+
+import (
+	"bytes"
+	"fmt"
+	"io"
+	"regexp"
+	"strconv"
+	"strings"
+	"time"
+
+	"github.com/alecthomas/chroma/v2"
+	"github.com/alecthomas/chroma/v2/formatters"
+	"github.com/alecthomas/chroma/v2/lexers"
+	"github.com/alecthomas/chroma/v2/styles"
+	"github.com/charmbracelet/lipgloss"
+	"github.com/charmbracelet/x/ansi"
+	"github.com/sergi/go-diff/diffmatchpatch"
+)
+
+// LineType represents the kind of line in a diff.
+type LineType int
+
+const (
+	// LineContext represents a line that exists in both the old and new file.
+	LineContext LineType = iota
+	// LineAdded represents a line added in the new file.
+	LineAdded
+	// LineRemoved represents a line removed from the old file.
+	LineRemoved
+)
+
+// DiffLine represents a single line in a diff, either from the old file,
+// the new file, or a context line.
+type DiffLine struct {
+	OldLineNo int      // Line number in the old file (0 for added lines)
+	NewLineNo int      // Line number in the new file (0 for removed lines)
+	Kind      LineType // Type of line (added, removed, context)
+	Content   string   // Content of the line
+}
+
+// Hunk represents a section of changes in a diff.
+type Hunk struct {
+	Header string
+	Lines  []DiffLine
+}
+
+// DiffResult contains the parsed result of a diff.
+type DiffResult struct {
+	OldFile string
+	NewFile string
+	Hunks   []Hunk
+}
+
+// HunkDelta represents the change statistics for a hunk.
+type HunkDelta struct {
+	StartLine1 int
+	LineCount1 int
+	StartLine2 int
+	LineCount2 int
+}
+
+// linePair represents a pair of lines to be displayed side by side.
+type linePair struct {
+	left  *DiffLine
+	right *DiffLine
+}
+
+// -------------------------------------------------------------------------
+// Style Configuration with Option Pattern
+// -------------------------------------------------------------------------
+
+// StyleConfig defines styling for diff rendering.
+type StyleConfig struct {
+	RemovedLineBg       lipgloss.Color
+	AddedLineBg         lipgloss.Color
+	ContextLineBg       lipgloss.Color
+	HunkLineBg          lipgloss.Color
+	HunkLineFg          lipgloss.Color
+	RemovedFg           lipgloss.Color
+	AddedFg             lipgloss.Color
+	LineNumberFg        lipgloss.Color
+	HighlightStyle      string
+	RemovedHighlightBg  lipgloss.Color
+	AddedHighlightBg    lipgloss.Color
+	RemovedLineNumberBg lipgloss.Color
+	AddedLineNamerBg    lipgloss.Color
+	RemovedHighlightFg  lipgloss.Color
+	AddedHighlightFg    lipgloss.Color
+}
+
+// StyleOption defines a function that modifies a StyleConfig.
+type StyleOption func(*StyleConfig)
+
+// NewStyleConfig creates a StyleConfig with default values and applies any provided options.
+func NewStyleConfig(opts ...StyleOption) StyleConfig {
+	// Set default values
+	config := StyleConfig{
+		RemovedLineBg:       lipgloss.Color("#3A3030"),
+		AddedLineBg:         lipgloss.Color("#303A30"),
+		ContextLineBg:       lipgloss.Color("#212121"),
+		HunkLineBg:          lipgloss.Color("#2A2822"),
+		HunkLineFg:          lipgloss.Color("#D4AF37"),
+		RemovedFg:           lipgloss.Color("#7C4444"),
+		AddedFg:             lipgloss.Color("#478247"),
+		LineNumberFg:        lipgloss.Color("#888888"),
+		HighlightStyle:      "dracula",
+		RemovedHighlightBg:  lipgloss.Color("#612726"),
+		AddedHighlightBg:    lipgloss.Color("#256125"),
+		RemovedLineNumberBg: lipgloss.Color("#332929"),
+		AddedLineNamerBg:    lipgloss.Color("#293229"),
+		RemovedHighlightFg:  lipgloss.Color("#FADADD"),
+		AddedHighlightFg:    lipgloss.Color("#DAFADA"),
+	}
+
+	// Apply all provided options
+	for _, opt := range opts {
+		opt(&config)
+	}
+
+	return config
+}
+
+// WithRemovedLineBg sets the background color for removed lines.
+func WithRemovedLineBg(color lipgloss.Color) StyleOption {
+	return func(s *StyleConfig) {
+		s.RemovedLineBg = color
+	}
+}
+
+// WithAddedLineBg sets the background color for added lines.
+func WithAddedLineBg(color lipgloss.Color) StyleOption {
+	return func(s *StyleConfig) {
+		s.AddedLineBg = color
+	}
+}
+
+// WithContextLineBg sets the background color for context lines.
+func WithContextLineBg(color lipgloss.Color) StyleOption {
+	return func(s *StyleConfig) {
+		s.ContextLineBg = color
+	}
+}
+
+// WithRemovedFg sets the foreground color for removed line markers.
+func WithRemovedFg(color lipgloss.Color) StyleOption {
+	return func(s *StyleConfig) {
+		s.RemovedFg = color
+	}
+}
+
+// WithAddedFg sets the foreground color for added line markers.
+func WithAddedFg(color lipgloss.Color) StyleOption {
+	return func(s *StyleConfig) {
+		s.AddedFg = color
+	}
+}
+
+// WithLineNumberFg sets the foreground color for line numbers.
+func WithLineNumberFg(color lipgloss.Color) StyleOption {
+	return func(s *StyleConfig) {
+		s.LineNumberFg = color
+	}
+}
+
+// WithHighlightStyle sets the syntax highlighting style.
+func WithHighlightStyle(style string) StyleOption {
+	return func(s *StyleConfig) {
+		s.HighlightStyle = style
+	}
+}
+
+// WithRemovedHighlightColors sets the colors for highlighted parts in removed text.
+func WithRemovedHighlightColors(bg, fg lipgloss.Color) StyleOption {
+	return func(s *StyleConfig) {
+		s.RemovedHighlightBg = bg
+		s.RemovedHighlightFg = fg
+	}
+}
+
+// WithAddedHighlightColors sets the colors for highlighted parts in added text.
+func WithAddedHighlightColors(bg, fg lipgloss.Color) StyleOption {
+	return func(s *StyleConfig) {
+		s.AddedHighlightBg = bg
+		s.AddedHighlightFg = fg
+	}
+}
+
+// WithRemovedLineNumberBg sets the background color for removed line numbers.
+func WithRemovedLineNumberBg(color lipgloss.Color) StyleOption {
+	return func(s *StyleConfig) {
+		s.RemovedLineNumberBg = color
+	}
+}
+
+// WithAddedLineNumberBg sets the background color for added line numbers.
+func WithAddedLineNumberBg(color lipgloss.Color) StyleOption {
+	return func(s *StyleConfig) {
+		s.AddedLineNamerBg = color
+	}
+}
+
+func WithHunkLineBg(color lipgloss.Color) StyleOption {
+	return func(s *StyleConfig) {
+		s.HunkLineBg = color
+	}
+}
+
+func WithHunkLineFg(color lipgloss.Color) StyleOption {
+	return func(s *StyleConfig) {
+		s.HunkLineFg = color
+	}
+}
+
+// -------------------------------------------------------------------------
+// Parse Options with Option Pattern
+// -------------------------------------------------------------------------
+
+// ParseConfig configures the behavior of diff parsing.
+type ParseConfig struct {
+	ContextSize int // Number of context lines to include
+}
+
+// ParseOption defines a function that modifies a ParseConfig.
+type ParseOption func(*ParseConfig)
+
+// NewParseConfig creates a ParseConfig with default values and applies any provided options.
+func NewParseConfig(opts ...ParseOption) ParseConfig {
+	// Set default values
+	config := ParseConfig{
+		ContextSize: 3,
+	}
+
+	// Apply all provided options
+	for _, opt := range opts {
+		opt(&config)
+	}
+
+	return config
+}
+
+// WithContextSize sets the number of context lines to include.
+func WithContextSize(size int) ParseOption {
+	return func(p *ParseConfig) {
+		if size >= 0 {
+			p.ContextSize = size
+		}
+	}
+}
+
+// -------------------------------------------------------------------------
+// Side-by-Side Options with Option Pattern
+// -------------------------------------------------------------------------
+
+// SideBySideConfig configures the rendering of side-by-side diffs.
+type SideBySideConfig struct {
+	TotalWidth int
+	Style      StyleConfig
+}
+
+// SideBySideOption defines a function that modifies a SideBySideConfig.
+type SideBySideOption func(*SideBySideConfig)
+
+// NewSideBySideConfig creates a SideBySideConfig with default values and applies any provided options.
+func NewSideBySideConfig(opts ...SideBySideOption) SideBySideConfig {
+	// Set default values
+	config := SideBySideConfig{
+		TotalWidth: 160, // Default width for side-by-side view
+		Style:      NewStyleConfig(),
+	}
+
+	// Apply all provided options
+	for _, opt := range opts {
+		opt(&config)
+	}
+
+	return config
+}
+
+// WithTotalWidth sets the total width for side-by-side view.
+func WithTotalWidth(width int) SideBySideOption {
+	return func(s *SideBySideConfig) {
+		if width > 0 {
+			s.TotalWidth = width
+		}
+	}
+}
+
+// WithStyle sets the styling configuration.
+func WithStyle(style StyleConfig) SideBySideOption {
+	return func(s *SideBySideConfig) {
+		s.Style = style
+	}
+}
+
+// WithStyleOptions applies the specified style options.
+func WithStyleOptions(opts ...StyleOption) SideBySideOption {
+	return func(s *SideBySideConfig) {
+		s.Style = NewStyleConfig(opts...)
+	}
+}
+
+// -------------------------------------------------------------------------
+// Diff Parsing and Generation
+// -------------------------------------------------------------------------
+
+// ParseUnifiedDiff parses a unified diff format string into structured data.
+func ParseUnifiedDiff(diff string) (DiffResult, error) {
+	var result DiffResult
+	var currentHunk *Hunk
+
+	hunkHeaderRe := regexp.MustCompile(`^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@`)
+	lines := strings.Split(diff, "\n")
+
+	var oldLine, newLine int
+	inFileHeader := true
+
+	for _, line := range lines {
+		// Parse the file headers
+		if inFileHeader {
+			if strings.HasPrefix(line, "--- a/") {
+				result.OldFile = strings.TrimPrefix(line, "--- a/")
+				continue
+			}
+			if strings.HasPrefix(line, "+++ b/") {
+				result.NewFile = strings.TrimPrefix(line, "+++ b/")
+				inFileHeader = false
+				continue
+			}
+		}
+
+		// Parse hunk headers
+		if matches := hunkHeaderRe.FindStringSubmatch(line); matches != nil {
+			if currentHunk != nil {
+				result.Hunks = append(result.Hunks, *currentHunk)
+			}
+			currentHunk = &Hunk{
+				Header: line,
+				Lines:  []DiffLine{},
+			}
+
+			oldStart, _ := strconv.Atoi(matches[1])
+			newStart, _ := strconv.Atoi(matches[3])
+			oldLine = oldStart
+			newLine = newStart
+
+			continue
+		}
+
+		if currentHunk == nil {
+			continue
+		}
+
+		if len(line) > 0 {
+			// Process the line based on its prefix
+			switch line[0] {
+			case '+':
+				currentHunk.Lines = append(currentHunk.Lines, DiffLine{
+					OldLineNo: 0,
+					NewLineNo: newLine,
+					Kind:      LineAdded,
+					Content:   line[1:], // skip '+'
+				})
+				newLine++
+			case '-':
+				currentHunk.Lines = append(currentHunk.Lines, DiffLine{
+					OldLineNo: oldLine,
+					NewLineNo: 0,
+					Kind:      LineRemoved,
+					Content:   line[1:], // skip '-'
+				})
+				oldLine++
+			default:
+				currentHunk.Lines = append(currentHunk.Lines, DiffLine{
+					OldLineNo: oldLine,
+					NewLineNo: newLine,
+					Kind:      LineContext,
+					Content:   line,
+				})
+				oldLine++
+				newLine++
+			}
+		} else {
+			// Handle empty lines
+			currentHunk.Lines = append(currentHunk.Lines, DiffLine{
+				OldLineNo: oldLine,
+				NewLineNo: newLine,
+				Kind:      LineContext,
+				Content:   "",
+			})
+			oldLine++
+			newLine++
+		}
+	}
+
+	// Add the last hunk if there is one
+	if currentHunk != nil {
+		result.Hunks = append(result.Hunks, *currentHunk)
+	}
+
+	return result, nil
+}
+
+// HighlightIntralineChanges updates the content of lines in a hunk to show
+// character-level differences within lines.
+func HighlightIntralineChanges(h *Hunk, style StyleConfig) {
+	var updated []DiffLine
+	dmp := diffmatchpatch.New()
+
+	for i := 0; i < len(h.Lines); i++ {
+		// Look for removed line followed by added line, which might have similar content
+		if i+1 < len(h.Lines) &&
+			h.Lines[i].Kind == LineRemoved &&
+			h.Lines[i+1].Kind == LineAdded {
+
+			oldLine := h.Lines[i]
+			newLine := h.Lines[i+1]
+
+			// Find character-level differences
+			patches := dmp.DiffMain(oldLine.Content, newLine.Content, false)
+			patches = dmp.DiffCleanupEfficiency(patches)
+			patches = dmp.DiffCleanupSemantic(patches)
+
+			// Apply highlighting to the differences
+			oldLine.Content = colorizeSegments(patches, true, style)
+			newLine.Content = colorizeSegments(patches, false, style)
+
+			updated = append(updated, oldLine, newLine)
+			i++ // Skip the next line as we've already processed it
+		} else {
+			updated = append(updated, h.Lines[i])
+		}
+	}
+
+	h.Lines = updated
+}
+
+// colorizeSegments applies styles to the character-level diff segments.
+func colorizeSegments(diffs []diffmatchpatch.Diff, isOld bool, style StyleConfig) string {
+	var buf strings.Builder
+
+	removeBg := lipgloss.NewStyle().
+		Background(style.RemovedHighlightBg).
+		Foreground(style.RemovedHighlightFg)
+
+	addBg := lipgloss.NewStyle().
+		Background(style.AddedHighlightBg).
+		Foreground(style.AddedHighlightFg)
+
+	removedLineStyle := lipgloss.NewStyle().Background(style.RemovedLineBg)
+	addedLineStyle := lipgloss.NewStyle().Background(style.AddedLineBg)
+
+	afterBg := false
+
+	for _, d := range diffs {
+		switch d.Type {
+		case diffmatchpatch.DiffEqual:
+			// Handle text that's the same in both versions
+			if afterBg {
+				if isOld {
+					buf.WriteString(removedLineStyle.Render(d.Text))
+				} else {
+					buf.WriteString(addedLineStyle.Render(d.Text))
+				}
+			} else {
+				buf.WriteString(d.Text)
+			}
+		case diffmatchpatch.DiffDelete:
+			// Handle deleted text (only show in old version)
+			if isOld {
+				buf.WriteString(removeBg.Render(d.Text))
+				afterBg = true
+			}
+		case diffmatchpatch.DiffInsert:
+			// Handle inserted text (only show in new version)
+			if !isOld {
+				buf.WriteString(addBg.Render(d.Text))
+				afterBg = true
+			}
+		}
+	}
+
+	return buf.String()
+}
+
+// pairLines converts a flat list of diff lines to pairs for side-by-side display.
+func pairLines(lines []DiffLine) []linePair {
+	var pairs []linePair
+	i := 0
+
+	for i < len(lines) {
+		switch lines[i].Kind {
+		case LineRemoved:
+			// Check if the next line is an addition, if so pair them
+			if i+1 < len(lines) && lines[i+1].Kind == LineAdded {
+				pairs = append(pairs, linePair{left: &lines[i], right: &lines[i+1]})
+				i += 2
+			} else {
+				pairs = append(pairs, linePair{left: &lines[i], right: nil})
+				i++
+			}
+		case LineAdded:
+			pairs = append(pairs, linePair{left: nil, right: &lines[i]})
+			i++
+		case LineContext:
+			pairs = append(pairs, linePair{left: &lines[i], right: &lines[i]})
+			i++
+		}
+	}
+
+	return pairs
+}
+
+// -------------------------------------------------------------------------
+// Syntax Highlighting
+// -------------------------------------------------------------------------
+
+// SyntaxHighlight applies syntax highlighting to a string based on the file extension.
+func SyntaxHighlight(w io.Writer, source, fileName, formatter string, bg lipgloss.TerminalColor) error {
+	// Determine the language lexer to use
+	l := lexers.Match(fileName)
+	if l == nil {
+		l = lexers.Analyse(source)
+	}
+	if l == nil {
+		l = lexers.Fallback
+	}
+	l = chroma.Coalesce(l)
+
+	// Get the formatter
+	f := formatters.Get(formatter)
+	if f == nil {
+		f = formatters.Fallback
+	}
+
+	// Get the style
+	s := styles.Get("dracula")
+	if s == nil {
+		s = styles.Fallback
+	}
+
+	// Modify the style to use the provided background
+	s, err := s.Builder().Transform(
+		func(t chroma.StyleEntry) chroma.StyleEntry {
+			r, g, b, _ := bg.RGBA()
+			ru8 := uint8(r >> 8)
+			gu8 := uint8(g >> 8)
+			bu8 := uint8(b >> 8)
+			t.Background = chroma.NewColour(ru8, gu8, bu8)
+			return t
+		},
+	).Build()
+	if err != nil {
+		s = styles.Fallback
+	}
+
+	// Tokenize and format
+	it, err := l.Tokenise(nil, source)
+	if err != nil {
+		return err
+	}
+
+	return f.Format(w, s, it)
+}
+
+// highlightLine applies syntax highlighting to a single line.
+func highlightLine(fileName string, line string, bg lipgloss.TerminalColor) string {
+	var buf bytes.Buffer
+	err := SyntaxHighlight(&buf, line, fileName, "terminal16m", bg)
+	if err != nil {
+		return line
+	}
+	return buf.String()
+}
+
+// createStyles generates the lipgloss styles needed for rendering diffs.
+func createStyles(config StyleConfig) (removedLineStyle, addedLineStyle, contextLineStyle, lineNumberStyle lipgloss.Style) {
+	removedLineStyle = lipgloss.NewStyle().Background(config.RemovedLineBg)
+	addedLineStyle = lipgloss.NewStyle().Background(config.AddedLineBg)
+	contextLineStyle = lipgloss.NewStyle().Background(config.ContextLineBg)
+	lineNumberStyle = lipgloss.NewStyle().Foreground(config.LineNumberFg)
+
+	return
+}
+
+// renderLeftColumn formats the left side of a side-by-side diff.
+func renderLeftColumn(fileName string, dl *DiffLine, colWidth int, styles StyleConfig) string {
+	if dl == nil {
+		contextLineStyle := lipgloss.NewStyle().Background(styles.ContextLineBg)
+		return contextLineStyle.Width(colWidth).Render("")
+	}
+
+	removedLineStyle, _, contextLineStyle, lineNumberStyle := createStyles(styles)
+
+	var marker string
+	var bgStyle lipgloss.Style
+
+	switch dl.Kind {
+	case LineRemoved:
+		marker = removedLineStyle.Foreground(styles.RemovedFg).Render("-")
+		bgStyle = removedLineStyle
+		lineNumberStyle = lineNumberStyle.Foreground(styles.RemovedFg).Background(styles.RemovedLineNumberBg)
+	case LineAdded:
+		marker = "?"
+		bgStyle = contextLineStyle
+	case LineContext:
+		marker = contextLineStyle.Render(" ")
+		bgStyle = contextLineStyle
+	}
+
+	lineNum := ""
+	if dl.OldLineNo > 0 {
+		lineNum = fmt.Sprintf("%6d", dl.OldLineNo)
+	}
+
+	prefix := lineNumberStyle.Render(lineNum + " " + marker)
+	content := highlightLine(fileName, dl.Content, bgStyle.GetBackground())
+
+	if dl.Kind == LineRemoved {
+		content = bgStyle.Render(" ") + content
+	}
+
+	lineText := prefix + content
+	return bgStyle.MaxHeight(1).Width(colWidth).Render(ansi.Truncate(lineText, colWidth, "..."))
+}
+
+// renderRightColumn formats the right side of a side-by-side diff.
+func renderRightColumn(fileName string, dl *DiffLine, colWidth int, styles StyleConfig) string {
+	if dl == nil {
+		contextLineStyle := lipgloss.NewStyle().Background(styles.ContextLineBg)
+		return contextLineStyle.Width(colWidth).Render("")
+	}
+
+	_, addedLineStyle, contextLineStyle, lineNumberStyle := createStyles(styles)
+
+	var marker string
+	var bgStyle lipgloss.Style
+
+	switch dl.Kind {
+	case LineAdded:
+		marker = addedLineStyle.Foreground(styles.AddedFg).Render("+")
+		bgStyle = addedLineStyle
+		lineNumberStyle = lineNumberStyle.Foreground(styles.AddedFg).Background(styles.AddedLineNamerBg)
+	case LineRemoved:
+		marker = "?"
+		bgStyle = contextLineStyle
+	case LineContext:
+		marker = contextLineStyle.Render(" ")
+		bgStyle = contextLineStyle
+	}
+
+	lineNum := ""
+	if dl.NewLineNo > 0 {
+		lineNum = fmt.Sprintf("%6d", dl.NewLineNo)
+	}
+
+	prefix := lineNumberStyle.Render(lineNum + " " + marker)
+	content := highlightLine(fileName, dl.Content, bgStyle.GetBackground())
+
+	if dl.Kind == LineAdded {
+		content = bgStyle.Render(" ") + content
+	}
+
+	lineText := prefix + content
+	return bgStyle.MaxHeight(1).Width(colWidth).Render(ansi.Truncate(lineText, colWidth, "..."))
+}
+
+// -------------------------------------------------------------------------
+// Public API Methods
+// -------------------------------------------------------------------------
+
+// RenderSideBySideHunk formats a hunk for side-by-side display.
+func RenderSideBySideHunk(fileName string, h Hunk, opts ...SideBySideOption) string {
+	// Apply options to create the configuration
+	config := NewSideBySideConfig(opts...)
+
+	// Make a copy of the hunk so we don't modify the original
+	hunkCopy := Hunk{Lines: make([]DiffLine, len(h.Lines))}
+	copy(hunkCopy.Lines, h.Lines)
+
+	// Highlight changes within lines
+	HighlightIntralineChanges(&hunkCopy, config.Style)
+
+	// Pair lines for side-by-side display
+	pairs := pairLines(hunkCopy.Lines)
+
+	// Calculate column width
+	colWidth := config.TotalWidth / 2
+
+	var sb strings.Builder
+	for _, p := range pairs {
+		leftStr := renderLeftColumn(fileName, p.left, colWidth, config.Style)
+		rightStr := renderRightColumn(fileName, p.right, colWidth, config.Style)
+		sb.WriteString(leftStr + rightStr + "\n")
+	}
+
+	return sb.String()
+}
+
+// FormatDiff creates a side-by-side formatted view of a diff.
+func FormatDiff(diffText string, opts ...SideBySideOption) (string, error) {
+	diffResult, err := ParseUnifiedDiff(diffText)
+	if err != nil {
+		return "", err
+	}
+
+	var sb strings.Builder
+
+	config := NewSideBySideConfig(opts...)
+	for i, h := range diffResult.Hunks {
+		if i > 0 {
+			sb.WriteString(lipgloss.NewStyle().Background(config.Style.HunkLineBg).Foreground(config.Style.HunkLineFg).Width(config.TotalWidth).Render(h.Header) + "\n")
+		}
+		sb.WriteString(RenderSideBySideHunk(diffResult.OldFile, h, opts...))
+	}
+
+	return sb.String(), nil
+}
+
+// GenerateDiff creates a unified diff from two file contents.
+func GenerateDiff(beforeContent, afterContent, beforeFilename, afterFilename string, opts ...ParseOption) (string, int, int) {
+	config := NewParseConfig(opts...)
+
+	var output strings.Builder
+
+	// Ensure we handle newlines correctly
+	beforeHasNewline := len(beforeContent) > 0 && beforeContent[len(beforeContent)-1] == '\n'
+	afterHasNewline := len(afterContent) > 0 && afterContent[len(afterContent)-1] == '\n'
+
+	// Split into lines
+	beforeLines := strings.Split(beforeContent, "\n")
+	afterLines := strings.Split(afterContent, "\n")
+
+	// Remove empty trailing element from the split if the content ended with a newline
+	if beforeHasNewline && len(beforeLines) > 0 {
+		beforeLines = beforeLines[:len(beforeLines)-1]
+	}
+	if afterHasNewline && len(afterLines) > 0 {
+		afterLines = afterLines[:len(afterLines)-1]
+	}
+
+	dmp := diffmatchpatch.New()
+	dmp.DiffTimeout = 5 * time.Second
+
+	// Convert lines to characters for efficient diffing
+	lineArray1, lineArray2, lineArrays := dmp.DiffLinesToChars(beforeContent, afterContent)
+	diffs := dmp.DiffMain(lineArray1, lineArray2, false)
+	diffs = dmp.DiffCharsToLines(diffs, lineArrays)
+
+	// Default filenames if not provided
+	if beforeFilename == "" {
+		beforeFilename = "a"
+	}
+	if afterFilename == "" {
+		afterFilename = "b"
+	}
+
+	// Write diff header
+	output.WriteString(fmt.Sprintf("diff --git a/%s b/%s\n", beforeFilename, afterFilename))
+	output.WriteString(fmt.Sprintf("--- a/%s\n", beforeFilename))
+	output.WriteString(fmt.Sprintf("+++ b/%s\n", afterFilename))
+
+	line1 := 0 // Line numbers start from 0 internally
+	line2 := 0
+	additions := 0
+	deletions := 0
+
+	var hunks []string
+	var currentHunk strings.Builder
+	var hunkStartLine1, hunkStartLine2 int
+	var hunkLines1, hunkLines2 int
+	inHunk := false
+
+	contextSize := config.ContextSize
+
+	// startHunk begins recording a new hunk
+	startHunk := func(startLine1, startLine2 int) {
+		inHunk = true
+		hunkStartLine1 = startLine1
+		hunkStartLine2 = startLine2
+		hunkLines1 = 0
+		hunkLines2 = 0
+		currentHunk.Reset()
+	}
+
+	// writeHunk adds the current hunk to the hunks slice
+	writeHunk := func() {
+		if inHunk {
+			hunkHeader := fmt.Sprintf("@@ -%d,%d +%d,%d @@\n",
+				hunkStartLine1+1, hunkLines1,
+				hunkStartLine2+1, hunkLines2)
+			hunks = append(hunks, hunkHeader+currentHunk.String())
+			inHunk = false
+		}
+	}
+
+	// Process diffs to create hunks
+	pendingContext := make([]string, 0, contextSize*2)
+	var contextLines1, contextLines2 int
+
+	// Helper function to add context lines to the hunk
+	addContextToHunk := func(lines []string, count int) {
+		for i := 0; i < count; i++ {
+			if i < len(lines) {
+				currentHunk.WriteString(" " + lines[i] + "\n")
+				hunkLines1++
+				hunkLines2++
+			}
+		}
+	}
+
+	// Process diffs
+	for _, diff := range diffs {
+		lines := strings.Split(diff.Text, "\n")
+
+		// Remove empty trailing line that comes from splitting a string that ends with \n
+		if len(lines) > 0 && lines[len(lines)-1] == "" && diff.Text[len(diff.Text)-1] == '\n' {
+			lines = lines[:len(lines)-1]
+		}
+
+		switch diff.Type {
+		case diffmatchpatch.DiffEqual:
+			// If we have enough equal lines to serve as context, add them to pending
+			pendingContext = append(pendingContext, lines...)
+
+			// If pending context grows too large, trim it
+			if len(pendingContext) > contextSize*2 {
+				pendingContext = pendingContext[len(pendingContext)-contextSize*2:]
+			}
+
+			// If we're in a hunk, add the necessary context
+			if inHunk {
+				// Only add the first contextSize lines as trailing context
+				numContextLines := min(contextSize, len(lines))
+				addContextToHunk(lines[:numContextLines], numContextLines)
+
+				// If we've added enough trailing context, close the hunk
+				if numContextLines >= contextSize {
+					writeHunk()
+				}
+			}
+
+			line1 += len(lines)
+			line2 += len(lines)
+			contextLines1 += len(lines)
+			contextLines2 += len(lines)
+
+		case diffmatchpatch.DiffDelete, diffmatchpatch.DiffInsert:
+			// Start a new hunk if needed
+			if !inHunk {
+				// Determine how many context lines we can add before
+				contextBefore := min(contextSize, len(pendingContext))
+				ctxStartIdx := len(pendingContext) - contextBefore
+
+				// Calculate the correct start lines
+				startLine1 := line1 - contextLines1 + ctxStartIdx
+				startLine2 := line2 - contextLines2 + ctxStartIdx
+
+				startHunk(startLine1, startLine2)
+
+				// Add the context lines before
+				addContextToHunk(pendingContext[ctxStartIdx:], contextBefore)
+			}
+
+			// Reset context tracking when we see a diff
+			pendingContext = pendingContext[:0]
+			contextLines1 = 0
+			contextLines2 = 0
+
+			// Add the changes
+			if diff.Type == diffmatchpatch.DiffDelete {
+				for _, line := range lines {
+					currentHunk.WriteString("-" + line + "\n")
+					hunkLines1++
+					deletions++
+				}
+				line1 += len(lines)
+			} else { // DiffInsert
+				for _, line := range lines {
+					currentHunk.WriteString("+" + line + "\n")
+					hunkLines2++
+					additions++
+				}
+				line2 += len(lines)
+			}
+		}
+	}
+
+	// Write the final hunk if there's one pending
+	if inHunk {
+		writeHunk()
+	}
+
+	// Merge hunks that are close to each other (within 2*contextSize lines)
+	var mergedHunks []string
+	if len(hunks) > 0 {
+		mergedHunks = append(mergedHunks, hunks[0])
+
+		for i := 1; i < len(hunks); i++ {
+			prevHunk := mergedHunks[len(mergedHunks)-1]
+			currHunk := hunks[i]
+
+			// Extract line numbers to check proximity
+			var prevStart, prevLen, currStart, currLen int
+			fmt.Sscanf(prevHunk, "@@ -%d,%d", &prevStart, &prevLen)
+			fmt.Sscanf(currHunk, "@@ -%d,%d", &currStart, &currLen)
+
+			prevEnd := prevStart + prevLen - 1
+
+			// If hunks are close, merge them
+			if currStart-prevEnd <= contextSize*2 {
+				// Create a merged hunk - this is a simplification, real git has more complex merging logic
+				merged := mergeHunks(prevHunk, currHunk)
+				mergedHunks[len(mergedHunks)-1] = merged
+			} else {
+				mergedHunks = append(mergedHunks, currHunk)
+			}
+		}
+	}
+
+	// Write all hunks to output
+	for _, hunk := range mergedHunks {
+		output.WriteString(hunk)
+	}
+
+	// Handle "No newline at end of file" notifications
+	if !beforeHasNewline && len(beforeLines) > 0 {
+		// Find the last deletion in the diff and add the notification after it
+		lastPos := strings.LastIndex(output.String(), "\n-")
+		if lastPos != -1 {
+			// Insert the notification after the line
+			str := output.String()
+			output.Reset()
+			output.WriteString(str[:lastPos+1])
+			output.WriteString("\\ No newline at end of file\n")
+			output.WriteString(str[lastPos+1:])
+		}
+	}
+
+	if !afterHasNewline && len(afterLines) > 0 {
+		// Find the last insertion in the diff and add the notification after it
+		lastPos := strings.LastIndex(output.String(), "\n+")
+		if lastPos != -1 {
+			// Insert the notification after the line
+			str := output.String()
+			output.Reset()
+			output.WriteString(str[:lastPos+1])
+			output.WriteString("\\ No newline at end of file\n")
+			output.WriteString(str[lastPos+1:])
+		}
+	}
+
+	// Return the diff without the summary line
+	return output.String(), additions, deletions
+}
+
+// Helper function to merge two hunks
+func mergeHunks(hunk1, hunk2 string) string {
+	// This is a simplified implementation
+	// A full implementation would need to properly recalculate the hunk header
+	// and remove redundant context lines
+
+	// Extract header info from both hunks
+	var start1, len1, start2, len2 int
+	var startB1, lenB1, startB2, lenB2 int
+
+	fmt.Sscanf(hunk1, "@@ -%d,%d +%d,%d @@", &start1, &len1, &startB1, &lenB1)
+	fmt.Sscanf(hunk2, "@@ -%d,%d +%d,%d @@", &start2, &len2, &startB2, &lenB2)
+
+	// Split the hunks to get content
+	parts1 := strings.SplitN(hunk1, "\n", 2)
+	parts2 := strings.SplitN(hunk2, "\n", 2)
+
+	content1 := ""
+	content2 := ""
+
+	if len(parts1) > 1 {
+		content1 = parts1[1]
+	}
+	if len(parts2) > 1 {
+		content2 = parts2[1]
+	}
+
+	// Calculate the new header
+	newEnd := max(start1+len1-1, start2+len2-1)
+	newEndB := max(startB1+lenB1-1, startB2+lenB2-1)
+
+	newLen := newEnd - start1 + 1
+	newLenB := newEndB - startB1 + 1
+
+	newHeader := fmt.Sprintf("@@ -%d,%d +%d,%d @@", start1, newLen, startB1, newLenB)
+
+	// Combine the content, potentially with some overlap handling
+	return newHeader + "\n" + content1 + content2
+}

internal/git/diff.go 🔗

@@ -1,264 +0,0 @@
-package git
-
-import (
-	"bytes"
-	"fmt"
-	"os"
-	"os/exec"
-	"path/filepath"
-	"strings"
-	"time"
-
-	"github.com/go-git/go-git/v5"
-	"github.com/go-git/go-git/v5/plumbing/object"
-)
-
-type DiffStats struct {
-	Additions int
-	Removals  int
-}
-
-func GenerateGitDiff(filePath string, contentBefore string, contentAfter string) (string, error) {
-	tempDir, err := os.MkdirTemp("", "git-diff-temp")
-	if err != nil {
-		return "", fmt.Errorf("failed to create temp dir: %w", err)
-	}
-	defer os.RemoveAll(tempDir)
-
-	repo, err := git.PlainInit(tempDir, false)
-	if err != nil {
-		return "", fmt.Errorf("failed to initialize git repo: %w", err)
-	}
-
-	wt, err := repo.Worktree()
-	if err != nil {
-		return "", fmt.Errorf("failed to get worktree: %w", err)
-	}
-
-	fullPath := filepath.Join(tempDir, filePath)
-	if err = os.MkdirAll(filepath.Dir(fullPath), 0o755); err != nil {
-		return "", fmt.Errorf("failed to create directories: %w", err)
-	}
-	if err = os.WriteFile(fullPath, []byte(contentBefore), 0o644); err != nil {
-		return "", fmt.Errorf("failed to write 'before' content: %w", err)
-	}
-
-	_, err = wt.Add(filePath)
-	if err != nil {
-		return "", fmt.Errorf("failed to add file to git: %w", err)
-	}
-
-	beforeCommit, err := wt.Commit("Before", &git.CommitOptions{
-		Author: &object.Signature{
-			Name:  "OpenCode",
-			Email: "coder@opencode.ai",
-			When:  time.Now(),
-		},
-	})
-	if err != nil {
-		return "", fmt.Errorf("failed to commit 'before' version: %w", err)
-	}
-
-	if err = os.WriteFile(fullPath, []byte(contentAfter), 0o644); err != nil {
-		return "", fmt.Errorf("failed to write 'after' content: %w", err)
-	}
-
-	_, err = wt.Add(filePath)
-	if err != nil {
-		return "", fmt.Errorf("failed to add updated file to git: %w", err)
-	}
-
-	afterCommit, err := wt.Commit("After", &git.CommitOptions{
-		Author: &object.Signature{
-			Name:  "OpenCode",
-			Email: "coder@opencode.ai",
-			When:  time.Now(),
-		},
-	})
-	if err != nil {
-		return "", fmt.Errorf("failed to commit 'after' version: %w", err)
-	}
-
-	beforeCommitObj, err := repo.CommitObject(beforeCommit)
-	if err != nil {
-		return "", fmt.Errorf("failed to get 'before' commit: %w", err)
-	}
-
-	afterCommitObj, err := repo.CommitObject(afterCommit)
-	if err != nil {
-		return "", fmt.Errorf("failed to get 'after' commit: %w", err)
-	}
-
-	patch, err := beforeCommitObj.Patch(afterCommitObj)
-	if err != nil {
-		return "", fmt.Errorf("failed to generate patch: %w", err)
-	}
-
-	return patch.String(), nil
-}
-
-func GenerateGitDiffWithStats(filePath string, contentBefore string, contentAfter string) (string, DiffStats, error) {
-	tempDir, err := os.MkdirTemp("", "git-diff-temp")
-	if err != nil {
-		return "", DiffStats{}, fmt.Errorf("failed to create temp dir: %w", err)
-	}
-	defer os.RemoveAll(tempDir)
-
-	repo, err := git.PlainInit(tempDir, false)
-	if err != nil {
-		return "", DiffStats{}, fmt.Errorf("failed to initialize git repo: %w", err)
-	}
-
-	wt, err := repo.Worktree()
-	if err != nil {
-		return "", DiffStats{}, fmt.Errorf("failed to get worktree: %w", err)
-	}
-
-	fullPath := filepath.Join(tempDir, filePath)
-	if err = os.MkdirAll(filepath.Dir(fullPath), 0o755); err != nil {
-		return "", DiffStats{}, fmt.Errorf("failed to create directories: %w", err)
-	}
-	if err = os.WriteFile(fullPath, []byte(contentBefore), 0o644); err != nil {
-		return "", DiffStats{}, fmt.Errorf("failed to write 'before' content: %w", err)
-	}
-
-	_, err = wt.Add(filePath)
-	if err != nil {
-		return "", DiffStats{}, fmt.Errorf("failed to add file to git: %w", err)
-	}
-
-	beforeCommit, err := wt.Commit("Before", &git.CommitOptions{
-		Author: &object.Signature{
-			Name:  "OpenCode",
-			Email: "coder@opencode.ai",
-			When:  time.Now(),
-		},
-	})
-	if err != nil {
-		return "", DiffStats{}, fmt.Errorf("failed to commit 'before' version: %w", err)
-	}
-
-	if err = os.WriteFile(fullPath, []byte(contentAfter), 0o644); err != nil {
-		return "", DiffStats{}, fmt.Errorf("failed to write 'after' content: %w", err)
-	}
-
-	_, err = wt.Add(filePath)
-	if err != nil {
-		return "", DiffStats{}, fmt.Errorf("failed to add updated file to git: %w", err)
-	}
-
-	afterCommit, err := wt.Commit("After", &git.CommitOptions{
-		Author: &object.Signature{
-			Name:  "OpenCode",
-			Email: "coder@opencode.ai",
-			When:  time.Now(),
-		},
-	})
-	if err != nil {
-		return "", DiffStats{}, fmt.Errorf("failed to commit 'after' version: %w", err)
-	}
-
-	beforeCommitObj, err := repo.CommitObject(beforeCommit)
-	if err != nil {
-		return "", DiffStats{}, fmt.Errorf("failed to get 'before' commit: %w", err)
-	}
-
-	afterCommitObj, err := repo.CommitObject(afterCommit)
-	if err != nil {
-		return "", DiffStats{}, fmt.Errorf("failed to get 'after' commit: %w", err)
-	}
-
-	patch, err := beforeCommitObj.Patch(afterCommitObj)
-	if err != nil {
-		return "", DiffStats{}, fmt.Errorf("failed to generate patch: %w", err)
-	}
-
-	stats := DiffStats{}
-	for _, fileStat := range patch.Stats() {
-		stats.Additions += fileStat.Addition
-		stats.Removals += fileStat.Deletion
-	}
-
-	return patch.String(), stats, nil
-}
-
-func FormatDiff(diffText string, width int) (string, error) {
-	if isSplitDiffsAvailable() {
-		return formatWithSplitDiffs(diffText, width)
-	}
-
-	return formatSimple(diffText), nil
-}
-
-func isSplitDiffsAvailable() bool {
-	_, err := exec.LookPath("node")
-	return err == nil
-}
-
-func formatWithSplitDiffs(diffText string, width int) (string, error) {
-	args := []string{
-		"--color",
-	}
-
-	var diffCmd *exec.Cmd
-
-	if _, err := exec.LookPath("git-split-diffs-opencode"); err == nil {
-		fullArgs := append([]string{"git-split-diffs-opencode"}, args...)
-		diffCmd = exec.Command(fullArgs[0], fullArgs[1:]...)
-	} else {
-		npxArgs := append([]string{"git-split-diffs-opencode"}, args...)
-		diffCmd = exec.Command("npx", npxArgs...)
-	}
-
-	diffCmd.Env = append(os.Environ(), fmt.Sprintf("DIFF_COLUMNS=%d", width))
-
-	diffCmd.Stdin = strings.NewReader(diffText)
-
-	var out bytes.Buffer
-	diffCmd.Stdout = &out
-
-	var stderr bytes.Buffer
-	diffCmd.Stderr = &stderr
-
-	if err := diffCmd.Run(); err != nil {
-		return "", fmt.Errorf("git-split-diffs-opencode error: %w, stderr: %s", err, stderr.String())
-	}
-
-	return out.String(), nil
-}
-
-func formatSimple(diffText string) string {
-	lines := strings.Split(diffText, "\n")
-	var result strings.Builder
-
-	for _, line := range lines {
-		if len(line) == 0 {
-			result.WriteString("\n")
-			continue
-		}
-
-		switch line[0] {
-		case '+':
-			result.WriteString("\033[32m" + line + "\033[0m\n")
-		case '-':
-			result.WriteString("\033[31m" + line + "\033[0m\n")
-		case '@':
-			result.WriteString("\033[36m" + line + "\033[0m\n")
-		case 'd':
-			if strings.HasPrefix(line, "diff --git") {
-				result.WriteString("\033[1m" + line + "\033[0m\n")
-			} else {
-				result.WriteString(line + "\n")
-			}
-		default:
-			result.WriteString(line + "\n")
-		}
-	}
-
-	if !strings.HasSuffix(diffText, "\n") {
-		output := result.String()
-		return output[:len(output)-1]
-	}
-
-	return result.String()
-}

internal/llm/tools/edit.go 🔗

@@ -10,7 +10,7 @@ import (
 	"time"
 
 	"github.com/kujtimiihoxha/termai/internal/config"
-	"github.com/kujtimiihoxha/termai/internal/git"
+	"github.com/kujtimiihoxha/termai/internal/diff"
 	"github.com/kujtimiihoxha/termai/internal/lsp"
 	"github.com/kujtimiihoxha/termai/internal/permission"
 )
@@ -182,14 +182,12 @@ func (e *editTool) createNewFile(ctx context.Context, filePath, content string)
 		return ToolResponse{}, fmt.Errorf("session ID and message ID are required for creating a new file")
 	}
 
-	diff, stats, err := git.GenerateGitDiffWithStats(
-		removeWorkingDirectoryPrefix(filePath),
+	diff, additions, removals := diff.GenerateDiff(
 		"",
 		content,
+		filePath,
+		filePath,
 	)
-	if err != nil {
-		return ToolResponse{}, fmt.Errorf("failed to get file diff: %w", err)
-	}
 	p := e.permissions.Request(
 		permission.CreatePermissionRequest{
 			Path:        filepath.Dir(filePath),
@@ -218,8 +216,8 @@ func (e *editTool) createNewFile(ctx context.Context, filePath, content string)
 		NewTextResponse("File created: "+filePath),
 		EditResponseMetadata{
 			Diff:      diff,
-			Additions: stats.Additions,
-			Removals:  stats.Removals,
+			Additions: additions,
+			Removals:  removals,
 		},
 	), nil
 }
@@ -275,14 +273,12 @@ func (e *editTool) deleteContent(ctx context.Context, filePath, oldString string
 		return ToolResponse{}, fmt.Errorf("session ID and message ID are required for creating a new file")
 	}
 
-	diff, stats, err := git.GenerateGitDiffWithStats(
-		removeWorkingDirectoryPrefix(filePath),
+	diff, additions, removals := diff.GenerateDiff(
 		oldContent,
 		newContent,
+		filePath,
+		filePath,
 	)
-	if err != nil {
-		return ToolResponse{}, fmt.Errorf("failed to get file diff: %w", err)
-	}
 
 	p := e.permissions.Request(
 		permission.CreatePermissionRequest{
@@ -311,8 +307,8 @@ func (e *editTool) deleteContent(ctx context.Context, filePath, oldString string
 		NewTextResponse("Content deleted from file: "+filePath),
 		EditResponseMetadata{
 			Diff:      diff,
-			Additions: stats.Additions,
-			Removals:  stats.Removals,
+			Additions: additions,
+			Removals:  removals,
 		},
 	), nil
 }
@@ -367,15 +363,12 @@ func (e *editTool) replaceContent(ctx context.Context, filePath, oldString, newS
 	if sessionID == "" || messageID == "" {
 		return ToolResponse{}, fmt.Errorf("session ID and message ID are required for creating a new file")
 	}
-	diff, stats, err := git.GenerateGitDiffWithStats(
-		removeWorkingDirectoryPrefix(filePath),
+	diff, additions, removals := diff.GenerateDiff(
 		oldContent,
 		newContent,
+		filePath,
+		filePath,
 	)
-	if err != nil {
-		return ToolResponse{}, fmt.Errorf("failed to get file diff: %w", err)
-	}
-
 	p := e.permissions.Request(
 		permission.CreatePermissionRequest{
 			Path:        filepath.Dir(filePath),
@@ -405,7 +398,7 @@ func (e *editTool) replaceContent(ctx context.Context, filePath, oldString, newS
 		NewTextResponse("Content replaced in file: "+filePath),
 		EditResponseMetadata{
 			Diff:      diff,
-			Additions: stats.Additions,
-			Removals:  stats.Removals,
+			Additions: additions,
+			Removals:  removals,
 		}), nil
 }

internal/llm/tools/write.go 🔗

@@ -9,7 +9,7 @@ import (
 	"time"
 
 	"github.com/kujtimiihoxha/termai/internal/config"
-	"github.com/kujtimiihoxha/termai/internal/git"
+	"github.com/kujtimiihoxha/termai/internal/diff"
 	"github.com/kujtimiihoxha/termai/internal/lsp"
 	"github.com/kujtimiihoxha/termai/internal/permission"
 )
@@ -149,14 +149,13 @@ func (w *writeTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error
 	if sessionID == "" || messageID == "" {
 		return ToolResponse{}, fmt.Errorf("session_id and message_id are required")
 	}
-	diff, stats, err := git.GenerateGitDiffWithStats(
-		removeWorkingDirectoryPrefix(filePath),
+
+	diff, additions, removals := diff.GenerateDiff(
 		oldContent,
 		params.Content,
+		filePath,
+		filePath,
 	)
-	if err != nil {
-		return ToolResponse{}, fmt.Errorf("error generating diff: %w", err)
-	}
 	p := w.permissions.Request(
 		permission.CreatePermissionRequest{
 			Path:        filePath,
@@ -188,8 +187,8 @@ func (w *writeTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error
 	return WithResponseMetadata(NewTextResponse(result),
 		WriteResponseMetadata{
 			Diff:      diff,
-			Additions: stats.Additions,
-			Removals:  stats.Removals,
+			Additions: additions,
+			Removals:  removals,
 		},
 	), nil
 }

internal/tui/components/dialog/permission.go 🔗

@@ -9,7 +9,7 @@ import (
 	tea "github.com/charmbracelet/bubbletea"
 	"github.com/charmbracelet/glamour"
 	"github.com/charmbracelet/lipgloss"
-	"github.com/kujtimiihoxha/termai/internal/git"
+	"github.com/kujtimiihoxha/termai/internal/diff"
 	"github.com/kujtimiihoxha/termai/internal/llm/tools"
 	"github.com/kujtimiihoxha/termai/internal/permission"
 	"github.com/kujtimiihoxha/termai/internal/tui/components/core"
@@ -242,7 +242,7 @@ func (p *permissionDialogCmp) render() string {
 		// Calculate content height dynamically based on window size
 		maxContentHeight := p.height - lipgloss.Height(headerContent) - lipgloss.Height(form) - 2 - 2 - 1
 		p.contentViewPort.Height = maxContentHeight
-		diff, err := git.FormatDiff(pr.Diff, p.contentViewPort.Width)
+		diff, err := diff.FormatDiff(pr.Diff, diff.WithTotalWidth(p.contentViewPort.Width))
 		if err != nil {
 			diff = fmt.Sprintf("Error formatting diff: %v", err)
 		}
@@ -291,7 +291,7 @@ func (p *permissionDialogCmp) render() string {
 		// Calculate content height dynamically based on window size
 		maxContentHeight := p.height - lipgloss.Height(headerContent) - lipgloss.Height(form) - 2 - 2 - 1
 		p.contentViewPort.Height = maxContentHeight
-		diff, err := git.FormatDiff(pr.Diff, p.contentViewPort.Width)
+		diff, err := diff.FormatDiff(pr.Diff, diff.WithTotalWidth(p.contentViewPort.Width))
 		if err != nil {
 			diff = fmt.Sprintf("Error formatting diff: %v", err)
 		}