Browse Source

wip: refactoring tui

adamdottv 9 months ago
parent
commit
9bf024f8be
4 changed files with 68 additions and 3 deletions
  1. 1 0
      go.mod
  2. 2 0
      go.sum
  3. 7 3
      internal/tui/components/chat/sidebar.go
  4. 58 0
      internal/tui/components/qr/qr.go

+ 1 - 0
go.mod

@@ -22,6 +22,7 @@ require (
 	github.com/spf13/cobra v1.9.1
 	github.com/spf13/viper v1.20.0
 	github.com/stretchr/testify v1.10.0
+	rsc.io/qr v0.2.0
 )
 
 require golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 // indirect

+ 2 - 0
go.sum

@@ -334,3 +334,5 @@ gopkg.in/yaml.v3 v3.0.0-20191026110619-0b21df46bc1d/go.mod h1:K4uyk7z7BCEPqu6E+C
 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+rsc.io/qr v0.2.0 h1:6vBLea5/NRMVTz8V66gipeLycZMl/+UlFmk8DvqQ6WY=
+rsc.io/qr v0.2.0/go.mod h1:IF+uZjkb9fqyeF/4tlBoynqmQxUoPfWEKh921coOuXs=

+ 7 - 3
internal/tui/components/chat/sidebar.go

@@ -9,6 +9,7 @@ import (
 	"github.com/charmbracelet/lipgloss"
 	"github.com/sst/opencode/internal/config"
 	"github.com/sst/opencode/internal/tui/app"
+	"github.com/sst/opencode/internal/tui/components/qr"
 	"github.com/sst/opencode/internal/tui/state"
 	"github.com/sst/opencode/internal/tui/styles"
 	"github.com/sst/opencode/internal/tui/theme"
@@ -52,6 +53,11 @@ func (m *sidebarCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 
 func (m *sidebarCmp) View() string {
 	baseStyle := styles.BaseStyle()
+	qrcode := ""
+	if m.app.Session.ShareID != nil {
+		url := "https://dev.opencode.ai/share?id="
+		qrcode, _, _ = qr.Generate(url + m.app.Session.Id)
+	}
 
 	return baseStyle.
 		Width(m.width).
@@ -64,9 +70,7 @@ func (m *sidebarCmp) View() string {
 				" ",
 				m.sessionSection(),
 				" ",
-				lspsConfigured(m.width),
-				" ",
-				m.modifiedFiles(),
+				qrcode,
 			),
 		)
 }

+ 58 - 0
internal/tui/components/qr/qr.go

@@ -0,0 +1,58 @@
+package qr
+
+import (
+	"strings"
+
+	"github.com/charmbracelet/lipgloss"
+	"github.com/sst/opencode/internal/tui/theme"
+	"rsc.io/qr"
+)
+
+var tops_bottoms = []rune{' ', '▀', '▄', '█'}
+
+// Generate a text string to a QR code, which you can write to a terminal or file.
+func Generate(text string) (string, int, error) {
+	code, err := qr.Encode(text, qr.Level(0))
+	if err != nil {
+		return "", 0, err
+	}
+
+	t := theme.CurrentTheme()
+	if t == nil {
+		return "", 0, err
+	}
+
+	// Create lipgloss style for QR code with theme colors
+	qrStyle := lipgloss.NewStyle().
+		Foreground(t.Text()).
+		Background(t.Background())
+
+	var result strings.Builder
+
+	// content
+	for y := 0; y < code.Size-1; y += 2 {
+		var line strings.Builder
+		for x := 0; x < code.Size; x += 1 {
+			var num int8
+			if code.Black(x, y) {
+				num += 1
+			}
+			if code.Black(x, y+1) {
+				num += 2
+			}
+			line.WriteRune(tops_bottoms[num])
+		}
+		result.WriteString(qrStyle.Render(line.String()) + "\n")
+	}
+
+	// add lower border when required (only required when QR size is odd)
+	if code.Size%2 == 1 {
+		var borderLine strings.Builder
+		for range code.Size {
+			borderLine.WriteRune('▀')
+		}
+		result.WriteString(qrStyle.Render(borderLine.String()) + "\n")
+	}
+
+	return result.String(), code.Size, nil
+}