Open source AI coding agent. Designed for large projects and real world tasks.
Go aiai-agentsai-developer-toolsai-toolsclicommand-linedeveloper-toolsgitgolanggpt-4llmopenaipolyglot-programmingterminalterminal-basedterminal-ui
'diffs --ui' command for viewing pending changes in local browser UI
Dane Schneidera2e367cb3e77
diff --git a/app/cli/cmd/build.go b/app/cli/cmd/build.goindex 9f40cb10..78fd19b1 100644--- a/app/cli/cmd/build.go+++ b/app/cli/cmd/build.go@@ -65,6 +65,6 @@ func build(cmd *cobra.Command, args []string) {term.PrintCmds("", "ps", "connect", "stop")} else {fmt.Println()- term.PrintCmds("", "changes", "apply", "reject", "log")+ term.PrintCmds("", "diff", "diff --ui", "apply", "reject", "log")}}diff --git a/app/cli/cmd/connect.go b/app/cli/cmd/connect.goindex 950ddb95..346cd17b 100644--- a/app/cli/cmd/connect.go+++ b/app/cli/cmd/connect.go@@ -57,7 +57,7 @@ func connect(cmd *cobra.Command, args []string) {}fmt.Println()- term.PrintCmds("", "changes", "diff", "apply", "reject", "log")+ term.PrintCmds("", "diff", "diff --ui", "apply", "reject", "log")os.Exit(0)}()diff --git a/app/cli/cmd/diffs.go b/app/cli/cmd/diffs.goindex de95e842..b19dd1d6 100644--- a/app/cli/cmd/diffs.go+++ b/app/cli/cmd/diffs.go@@ -2,15 +2,21 @@ package cmdimport ("fmt"+ "net"+ "net/http""plandex/api""plandex/auth""plandex/lib""plandex/term"+ "plandex/ui"+ "github.com/fatih/color""github.com/spf13/cobra")// var plainTextOutput bool+var showDiffUi bool+var diffUiSideBySide boolvar diffsCmd = &cobra.Command{Use: "diff",@@ -24,6 +30,9 @@ func init() {diffsCmd.Flags().BoolVarP(&plainTextOutput, "plain", "p", false, "Output diffs in plain text with no ANSI codes")+ diffsCmd.Flags().BoolVar(&showDiffUi, "ui", false, "Show diffs in a browser UI")+ diffsCmd.Flags().BoolVarP(&diffUiSideBySide, "side-by-side", "s", false, "Show diffs UI in side-by-side view")+}func diffs(cmd *cobra.Command, args []string) {@@ -34,16 +43,89 @@ func diffs(cmd *cobra.Command, args []string) {term.OutputNoCurrentPlanErrorAndExit()}- diffs, err := api.Client.GetPlanDiffs(lib.CurrentPlanId, lib.CurrentBranch, plainTextOutput)+ diffs, err := api.Client.GetPlanDiffs(lib.CurrentPlanId, lib.CurrentBranch, plainTextOutput || showDiffUi)if err != nil {term.OutputErrorAndExit("Error getting plan diffs: %v", err)return}- if plainTextOutput {- fmt.Println(diffs)+ if showDiffUi {++ var outputFormat string+ if diffUiSideBySide {+ outputFormat = "side-by-side"+ } else {+ outputFormat = "line-by-line"+ }+ html := fmt.Sprintf(htmlTemplate, "`"+diffs+"`", outputFormat)++ // Use :0 to let the OS pick an available port+ listener, err := net.Listen("tcp", ":0")+ if err != nil {+ term.OutputErrorAndExit("Error starting server: %v", err)+ return+ }+ defer listener.Close()++ // Get the actual port chosen+ port := listener.Addr().(*net.TCPAddr).Port++ // start a web server on a likely unused port+ go func() {+ http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {+ fmt.Fprint(w, html)+ })+ http.Serve(listener, nil)+ }()++ ui.OpenURL("Showing diffs in your default browser...", fmt.Sprintf("http://localhost:%d", port))++ fmt.Println()+ color.New(term.ColorHiGreen).Println("Ctrl+C to exit")++ // wait forever+ select {}+} else {- term.PageOutput(diffs)++ if plainTextOutput {+ fmt.Println(diffs)+ } else {+ term.PageOutput(diffs)+ }}}++var htmlTemplate = `+ <!doctype html>+<html lang="en-us">+ <head>+ <meta charset="utf-8" />+ <!-- Make sure to load the highlight.js CSS file before the Diff2Html CSS file -->+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/vs.min.css" />+ <link+ rel="stylesheet"+ type="text/css"+ href="https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css"+ />+ <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"></script>+ </head>+ <script>+ const diffString = %s;++ document.addEventListener('DOMContentLoaded', function () {+ var targetElement = document.getElementById('myDiffElement');+ var configuration = {+ outputFormat: '%s'+ };+ var diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration);+ diff2htmlUi.draw();+ diff2htmlUi.highlightCode();+ });+ </script>+ <body>+ <div id="myDiffElement"></div>+ </body>+</html>+`diff --git a/app/cli/plan_exec/tell.go b/app/cli/plan_exec/tell.goindex 889f3690..ecf4a49e 100644--- a/app/cli/plan_exec/tell.go+++ b/app/cli/plan_exec/tell.go@@ -159,9 +159,9 @@ func TellPlan(fmt.Println()if tellStop {- term.PrintCmds("", "continue", "changes", "diff", "apply", "reject", "log", "rewind")+ term.PrintCmds("", "continue", "diff", "diff --ui", "apply", "reject", "log", "rewind")} else {- term.PrintCmds("", "changes", "diff", "apply", "reject", "log", "rewind")+ term.PrintCmds("", "diff", "diff --ui", "apply", "reject", "log", "rewind")}os.Exit(0)}()diff --git a/app/cli/term/help.go b/app/cli/term/help.goindex 27e4ced2..adbcdb6e 100644--- a/app/cli/term/help.go+++ b/app/cli/term/help.go@@ -10,16 +10,17 @@ import ()var CmdDesc = map[string][2]string{- "new": {"", "start a new plan"},- "rename": {"", "rename the current plan"},- "current": {"cu", "show current plan"},- "cd": {"", "set current plan by name or index"},- "load": {"l", "load files, dirs, urls, notes, images, or piped data into context"},- "tell": {"t", "describe a task, ask a question, or chat"},- "changes": {"ch", "review pending changes in a TUI"},- "diff": {"", "review pending changes in 'git diff' format"},- "diff --plain": {"", "review pending changes in 'git diff' format with no color formatting"},- "summary": {"", "show the latest summary of the current plan"},+ "new": {"", "start a new plan"},+ "rename": {"", "rename the current plan"},+ "current": {"cu", "show current plan"},+ "cd": {"", "set current plan by name or index"},+ "load": {"l", "load files, dirs, urls, notes, images, or piped data into context"},+ "tell": {"t", "describe a task, ask a question, or chat"},+ "changes": {"ch", "review pending changes in a TUI"},+ "diff": {"", "review pending changes in 'git diff' format"},+ "diff --plain": {"", "review pending changes in 'git diff' format with no color formatting"},+ "diff --ui": {"", "review pending changes in a local browser UI"},+ "summary": {"", "show the latest summary of the current plan"},// "preview": {"pv", "preview the plan in a branch"},"apply": {"ap", "apply pending changes to project files"},"reject": {"rj", "reject pending changes to one or more project files"},@@ -138,7 +139,7 @@ func PrintCustomHelp(all bool) {if all {color.New(color.Bold, color.BgMagenta, color.FgHiWhite).Fprintln(builder, " Key Commands ")- printCmds(builder, " ", []color.Attribute{color.Bold, ColorHiMagenta}, "new", "load", "tell", "changes", "diff", "apply", "reject")+ printCmds(builder, " ", []color.Attribute{color.Bold, ColorHiMagenta}, "new", "load", "tell", "diff", "diff --ui", "apply", "reject")fmt.Fprintln(builder)color.New(color.Bold, color.BgCyan, color.FgHiWhite).Fprintln(builder, " Plans ")@@ -146,7 +147,7 @@ func PrintCustomHelp(all bool) {fmt.Fprintln(builder)color.New(color.Bold, color.BgCyan, color.FgHiWhite).Fprintln(builder, " Changes ")- printCmds(builder, " ", []color.Attribute{color.Bold, ColorHiCyan}, "changes", "diff", "diff --plain", "apply", "reject")+ printCmds(builder, " ", []color.Attribute{color.Bold, ColorHiCyan}, "diff", "diff --ui", "diff --plain", "changes", "apply", "reject")fmt.Fprintln(builder)color.New(color.Bold, color.BgCyan, color.FgHiWhite).Fprintln(builder, " Context ")diff --git a/app/cli/ui/ui.go b/app/cli/ui/ui.goindex 24de8577..52e3b3ec 100644--- a/app/cli/ui/ui.go+++ b/app/cli/ui/ui.go@@ -38,17 +38,7 @@ func OpenAuthenticatedURL(msg, path string) {url := fmt.Sprintf("%s/auth/%s", appHost, encodedToken)- fmt.Printf(- "%s\n\nIf it doesn't open automatically, use this URL:\n%s\n",- color.New(term.ColorHiGreen).Sprintf(msg),- url,- )-- err = browser.OpenURL(url)- if err != nil {- fmt.Printf("Failed to open URL automatically: %v\n", err)- fmt.Println("Please open the URL manually in your browser.")- }+ OpenURL(msg, url)}func OpenUnauthenticatedCloudURL(msg, path string) {@@ -56,6 +46,11 @@ func OpenUnauthenticatedCloudURL(msg, path string) {appHost := strings.Replace(apiHost, "api.", "app.", 1)url := fmt.Sprintf("%s%s", appHost, path)+ OpenURL(msg, url)+}++func OpenURL(msg, url string) {+fmt.Printf("%s\n\nIf it doesn't open automatically, use this URL:\n%s\n",color.New(term.ColorHiGreen).Sprintf(msg),