Browse Source

wip: refactoring tui

adamdottv 9 months ago
parent
commit
50ba0b380b
2 changed files with 32 additions and 68 deletions
  1. 12 39
      cmd/root.go
  2. 20 29
      internal/tui/tui.go

+ 12 - 39
cmd/root.go

@@ -3,7 +3,6 @@ package cmd
 import (
 	"context"
 	"fmt"
-	"io"
 	"os"
 	"sync"
 	"time"
@@ -15,7 +14,6 @@ import (
 	"github.com/spf13/cobra"
 	"github.com/sst/opencode/internal/config"
 	"github.com/sst/opencode/internal/logging"
-	"github.com/sst/opencode/internal/lsp/discovery"
 	"github.com/sst/opencode/internal/pubsub"
 	"github.com/sst/opencode/internal/tui"
 	"github.com/sst/opencode/internal/tui/app"
@@ -69,12 +67,6 @@ to assist developers in writing, debugging, and understanding code directly from
 			return err
 		}
 
-		// Run LSP auto-discovery
-		if err := discovery.IntegrateLSPServers(cwd); err != nil {
-			slog.Warn("Failed to auto-discover LSP servers", "error", err)
-			// Continue anyway, this is not a fatal error
-		}
-
 		// Create main context for the application
 		ctx, cancel := context.WithCancel(context.Background())
 		defer cancel()
@@ -92,6 +84,18 @@ to assist developers in writing, debugging, and understanding code directly from
 			tea.WithAltScreen(),
 		)
 
+		evts, err := app.Events.Event(ctx)
+		if err != nil {
+			slog.Error("Failed to subscribe to events", "error", err)
+			return err
+		}
+
+		go func() {
+			for item := range evts {
+				program.Send(item)
+			}
+		}()
+
 		// Setup the subscriptions, this will send services events to the TUI
 		ch, cancelSubs := setupSubscriptions(app, ctx)
 
@@ -122,18 +126,6 @@ to assist developers in writing, debugging, and understanding code directly from
 			}
 		}()
 
-		evts, err := app.Events.Event(ctx)
-		if err != nil {
-			slog.Error("Failed to subscribe to events", "error", err)
-			return err
-		}
-
-		go func() {
-			for item := range evts {
-				program.Send(item)
-			}
-		}()
-
 		// Cleanup function for when the program exits
 		cleanup := func() {
 			// Cancel subscriptions first
@@ -256,25 +248,6 @@ func Execute() {
 	}
 }
 
-// checkStdinPipe checks if there's data being piped into stdin
-func checkStdinPipe() (string, bool) {
-	// Check if stdin is not a terminal (i.e., it's being piped)
-	stat, _ := os.Stdin.Stat()
-	if (stat.Mode() & os.ModeCharDevice) == 0 {
-		// Read all data from stdin
-		data, err := io.ReadAll(os.Stdin)
-		if err != nil {
-			return "", false
-		}
-
-		// If we got data, return it
-		if len(data) > 0 {
-			return string(data), true
-		}
-	}
-	return "", false
-}
-
 func init() {
 	rootCmd.Flags().BoolP("help", "h", false, "Help")
 	rootCmd.Flags().BoolP("version", "v", false, "Version")

+ 20 - 29
internal/tui/tui.go

@@ -16,7 +16,6 @@ import (
 	"github.com/sst/opencode/internal/message"
 	"github.com/sst/opencode/internal/permission"
 	"github.com/sst/opencode/internal/pubsub"
-	"github.com/sst/opencode/internal/session"
 	"github.com/sst/opencode/internal/status"
 	"github.com/sst/opencode/internal/tui/components/chat"
 	"github.com/sst/opencode/internal/tui/components/core"
@@ -194,6 +193,26 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 	case spinner.TickMsg:
 		return a.updateAllPages(msg)
 
+	case client.EventSessionUpdated:
+		if msg.Properties.Info.Id == a.app.Session.Id {
+			a.app.Session = &msg.Properties.Info
+			return a.updateAllPages(state.StateUpdatedMsg{State: nil})
+		}
+
+	case client.EventMessageUpdated:
+		if msg.Properties.Info.Metadata.SessionID == a.app.Session.Id {
+			for i, m := range a.app.Messages {
+				if m.Id == msg.Properties.Info.Id {
+					a.app.Messages[i] = msg.Properties.Info
+					slog.Debug("Updated message", "message", msg.Properties.Info)
+					return a.updateAllPages(state.StateUpdatedMsg{State: nil})
+				}
+			}
+			a.app.Messages = append(a.app.Messages, msg.Properties.Info)
+			slog.Debug("Appended message", "message", msg.Properties.Info)
+			return a.updateAllPages(state.StateUpdatedMsg{State: nil})
+		}
+
 	case tea.WindowSizeMsg:
 		msg.Height -= 2 // Make space for the status bar
 		a.width, a.height = msg.Width, msg.Height
@@ -259,34 +278,6 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 		a.app.CurrentSessionOLD = msg
 		return a.updateAllPages(msg)
 
-	case pubsub.Event[session.Session]:
-		if msg.Type == session.EventSessionUpdated {
-			if a.app.CurrentSessionOLD.ID == msg.Payload.ID {
-				a.app.CurrentSessionOLD = &msg.Payload
-			}
-		}
-
-	case client.EventMessageUpdated:
-		if msg.Properties.Info.Metadata.SessionID == a.app.Session.Id {
-			for i, m := range a.app.Messages {
-				if m.Id == msg.Properties.Info.Id {
-					a.app.Messages[i] = msg.Properties.Info
-					slog.Debug("Updated message", "message", msg.Properties.Info)
-					return a.updateAllPages(state.StateUpdatedMsg{State: nil})
-				}
-			}
-
-			a.app.Messages = append(a.app.Messages, msg.Properties.Info)
-			slog.Debug("Appended message", "message", msg.Properties.Info)
-			return a.updateAllPages(state.StateUpdatedMsg{State: nil})
-		}
-
-	case client.EventSessionUpdated:
-		if msg.Properties.Info.Id == a.app.Session.Id {
-			a.app.Session = &msg.Properties.Info
-			return a.updateAllPages(state.StateUpdatedMsg{State: nil})
-		}
-
 	case dialog.CloseQuitMsg:
 		a.showQuit = false
 		return a, nil