This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package interaction
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
func (m *model) comingSoonUpdate(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
m.showingComingSoon = false
|
||||
return m, tea.Batch(tea.ClearScreen, textinput.Blink)
|
||||
}
|
||||
|
||||
func (m *model) comingSoonView() string {
|
||||
isCompact := shouldUseCompactLayout(m.width, 60)
|
||||
|
||||
var boxPadding int
|
||||
var boxMargin int
|
||||
if isCompact {
|
||||
boxPadding = 1
|
||||
boxMargin = 1
|
||||
} else {
|
||||
boxPadding = 3
|
||||
boxMargin = 2
|
||||
}
|
||||
|
||||
titleStyle := lipgloss.NewStyle().
|
||||
Bold(true).
|
||||
Foreground(lipgloss.Color("#7D56F4")).
|
||||
PaddingTop(1).
|
||||
PaddingBottom(1)
|
||||
|
||||
messageBoxWidth := getResponsiveWidth(m.width, 10, 30, 60)
|
||||
messageBoxStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color("#FAFAFA")).
|
||||
Background(lipgloss.Color("#1A1A2E")).
|
||||
Bold(true).
|
||||
Border(lipgloss.RoundedBorder()).
|
||||
BorderForeground(lipgloss.Color("#7D56F4")).
|
||||
Padding(1, boxPadding).
|
||||
MarginTop(boxMargin).
|
||||
MarginBottom(boxMargin).
|
||||
Width(messageBoxWidth).
|
||||
Align(lipgloss.Center)
|
||||
|
||||
helpStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color("#666666")).
|
||||
Italic(true).
|
||||
MarginTop(1)
|
||||
|
||||
var b strings.Builder
|
||||
b.WriteString("\n\n")
|
||||
|
||||
var title string
|
||||
if shouldUseCompactLayout(m.width, 40) {
|
||||
title = "Coming Soon"
|
||||
} else {
|
||||
title = "⏳ Coming Soon"
|
||||
}
|
||||
b.WriteString(titleStyle.Render(title))
|
||||
b.WriteString("\n\n")
|
||||
|
||||
var message string
|
||||
if shouldUseCompactLayout(m.width, 50) {
|
||||
message = "Coming soon!\nStay tuned."
|
||||
} else {
|
||||
message = "🚀 This feature is coming very soon!\n Stay tuned for updates."
|
||||
}
|
||||
b.WriteString(messageBoxStyle.Render(message))
|
||||
b.WriteString("\n\n")
|
||||
|
||||
var helpText string
|
||||
if shouldUseCompactLayout(m.width, 60) {
|
||||
helpText = "Press any key..."
|
||||
} else {
|
||||
helpText = "This message will disappear in 5 seconds or press any key..."
|
||||
}
|
||||
b.WriteString(helpStyle.Render(helpText))
|
||||
|
||||
return b.String()
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package interaction
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
func (m *model) handleCommandSelection(item commandItem) (tea.Model, tea.Cmd) {
|
||||
switch item.name {
|
||||
case "slug":
|
||||
m.showingCommands = false
|
||||
m.editingSlug = true
|
||||
m.slugInput.SetValue(m.interaction.slug.String())
|
||||
m.slugInput.Focus()
|
||||
return m, tea.Batch(tea.ClearScreen, textinput.Blink)
|
||||
case "tunnel-type":
|
||||
m.showingCommands = false
|
||||
m.showingComingSoon = true
|
||||
return m, tea.Batch(tickCmd(5*time.Second), tea.ClearScreen, textinput.Blink)
|
||||
default:
|
||||
m.showingCommands = false
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m *model) commandsUpdate(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
var cmd tea.Cmd
|
||||
|
||||
switch {
|
||||
case key.Matches(msg, m.keymap.quit), msg.String() == "esc":
|
||||
m.showingCommands = false
|
||||
return m, tea.Batch(tea.ClearScreen, textinput.Blink)
|
||||
case msg.String() == "enter":
|
||||
selectedItem := m.commandList.SelectedItem()
|
||||
if selectedItem != nil {
|
||||
item := selectedItem.(commandItem)
|
||||
return m.handleCommandSelection(item)
|
||||
}
|
||||
}
|
||||
m.commandList, cmd = m.commandList.Update(msg)
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
func (m *model) commandsView() string {
|
||||
isCompact := shouldUseCompactLayout(m.width, 60)
|
||||
|
||||
titleStyle := lipgloss.NewStyle().
|
||||
Bold(true).
|
||||
Foreground(lipgloss.Color("#7D56F4")).
|
||||
PaddingTop(1).
|
||||
PaddingBottom(1)
|
||||
|
||||
helpStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color("#666666")).
|
||||
Italic(true).
|
||||
MarginTop(1)
|
||||
|
||||
var b strings.Builder
|
||||
b.WriteString("\n")
|
||||
|
||||
var title string
|
||||
if shouldUseCompactLayout(m.width, 40) {
|
||||
title = "Commands"
|
||||
} else {
|
||||
title = "⚡ Commands"
|
||||
}
|
||||
b.WriteString(titleStyle.Render(title))
|
||||
b.WriteString("\n\n")
|
||||
b.WriteString(m.commandList.View())
|
||||
b.WriteString("\n")
|
||||
|
||||
var helpText string
|
||||
if isCompact {
|
||||
helpText = "↑/↓ Nav • Enter Select • Esc Cancel"
|
||||
} else {
|
||||
helpText = "↑/↓ Navigate • Enter Select • Esc Cancel"
|
||||
}
|
||||
b.WriteString(helpStyle.Render(helpText))
|
||||
|
||||
return b.String()
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
package interaction
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
func (m *model) dashboardUpdate(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
switch {
|
||||
case key.Matches(msg, m.keymap.quit):
|
||||
m.quitting = true
|
||||
return m, tea.Batch(tea.ClearScreen, textinput.Blink, tea.Quit)
|
||||
case key.Matches(msg, m.keymap.command):
|
||||
m.showingCommands = true
|
||||
return m, tea.Batch(tea.ClearScreen, textinput.Blink)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m *model) dashboardView() string {
|
||||
isCompact := shouldUseCompactLayout(m.width, BreakpointLarge)
|
||||
|
||||
var b strings.Builder
|
||||
b.WriteString(m.renderHeader(isCompact))
|
||||
b.WriteString(m.renderUserInfo(isCompact))
|
||||
b.WriteString(m.renderQuickActions(isCompact))
|
||||
b.WriteString(m.renderFooter(isCompact))
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *model) renderHeader(isCompact bool) string {
|
||||
var b strings.Builder
|
||||
|
||||
asciiArtMargin := getMarginValue(isCompact, 0, 1)
|
||||
asciiArtStyle := lipgloss.NewStyle().
|
||||
Bold(true).
|
||||
Foreground(lipgloss.Color(ColorPrimary)).
|
||||
MarginBottom(asciiArtMargin)
|
||||
|
||||
b.WriteString(asciiArtStyle.Render(m.getASCIIArt()))
|
||||
b.WriteString("\n")
|
||||
|
||||
if !shouldUseCompactLayout(m.width, BreakpointSmall) {
|
||||
b.WriteString(m.renderSubtitle())
|
||||
} else {
|
||||
b.WriteString("\n")
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *model) getASCIIArt() string {
|
||||
if shouldUseCompactLayout(m.width, BreakpointTiny) {
|
||||
return "TUNNEL PLS"
|
||||
}
|
||||
|
||||
if shouldUseCompactLayout(m.width, BreakpointLarge) {
|
||||
return `
|
||||
▀█▀ █ █ █▄ █ █▄ █ ██▀ █ ▄▀▀ █ ▄▀▀
|
||||
█ ▀▄█ █ ▀█ █ ▀█ █▄▄ █▄▄ ▄█▀ █▄▄ ▄█▀`
|
||||
}
|
||||
|
||||
return `
|
||||
████████╗██╗ ██╗███╗ ██╗███╗ ██╗███████╗██╗ ██████╗ ██╗ ███████╗
|
||||
╚══██╔══╝██║ ██║████╗ ██║████╗ ██║██╔════╝██║ ██╔══██╗██║ ██╔════╝
|
||||
██║ ██║ ██║██╔██╗ ██║██╔██╗ ██║█████╗ ██║ ██████╔╝██║ ███████╗
|
||||
██║ ██║ ██║██║╚██╗██║██║╚██╗██║██╔══╝ ██║ ██╔═══╝ ██║ ╚════██║
|
||||
██║ ╚██████╔╝██║ ╚████║██║ ╚████║███████╗███████╗ ██║ ███████╗███████║
|
||||
╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═══╝╚══════╝╚══════╝ ╚═╝ ╚══════╝╚══════╝`
|
||||
}
|
||||
|
||||
func (m *model) renderSubtitle() string {
|
||||
subtitleStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color(ColorGray)).
|
||||
Italic(true)
|
||||
|
||||
urlStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color(ColorPrimary)).
|
||||
Underline(true).
|
||||
Italic(true)
|
||||
|
||||
return subtitleStyle.Render("Secure tunnel service by Bagas • ") +
|
||||
urlStyle.Render("https://fossy.my.id") + "\n\n"
|
||||
}
|
||||
|
||||
func (m *model) renderUserInfo(isCompact bool) string {
|
||||
boxMaxWidth := getResponsiveWidth(m.width, 10, 40, 80)
|
||||
boxPadding := getMarginValue(isCompact, 1, 2)
|
||||
boxMargin := getMarginValue(isCompact, 1, 2)
|
||||
|
||||
responsiveInfoBox := lipgloss.NewStyle().
|
||||
Border(lipgloss.RoundedBorder()).
|
||||
BorderForeground(lipgloss.Color(ColorPrimary)).
|
||||
Padding(1, boxPadding).
|
||||
MarginTop(boxMargin).
|
||||
MarginBottom(boxMargin).
|
||||
Width(boxMaxWidth)
|
||||
|
||||
infoContent := m.getUserInfoContent(isCompact)
|
||||
return responsiveInfoBox.Render(infoContent) + "\n"
|
||||
}
|
||||
|
||||
func (m *model) getUserInfoContent(isCompact bool) string {
|
||||
userInfoStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color(ColorWhite)).
|
||||
Bold(true)
|
||||
|
||||
sectionHeaderStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color(ColorGray)).
|
||||
Bold(true)
|
||||
|
||||
addressStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color(ColorWhite))
|
||||
|
||||
urlBoxStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color(ColorSecondary)).
|
||||
Bold(true).
|
||||
Italic(true)
|
||||
|
||||
authenticatedUser := m.interaction.user
|
||||
tunnelURL := urlBoxStyle.Render(m.getTunnelURL())
|
||||
|
||||
if isCompact {
|
||||
return fmt.Sprintf("👤 %s\n\n%s\n%s",
|
||||
userInfoStyle.Render(authenticatedUser),
|
||||
sectionHeaderStyle.Render("🌐 FORWARDING ADDRESS:"),
|
||||
addressStyle.Render(fmt.Sprintf(" %s", tunnelURL)))
|
||||
}
|
||||
|
||||
return fmt.Sprintf("👤 Authenticated as: %s\n\n%s\n %s",
|
||||
userInfoStyle.Render(authenticatedUser),
|
||||
sectionHeaderStyle.Render("🌐 FORWARDING ADDRESS:"),
|
||||
addressStyle.Render(tunnelURL))
|
||||
}
|
||||
|
||||
func (m *model) renderQuickActions(isCompact bool) string {
|
||||
var b strings.Builder
|
||||
|
||||
titleStyle := lipgloss.NewStyle().
|
||||
Bold(true).
|
||||
Foreground(lipgloss.Color(ColorPrimary)).
|
||||
PaddingTop(1)
|
||||
|
||||
b.WriteString(titleStyle.Render(m.getQuickActionsTitle()))
|
||||
b.WriteString("\n")
|
||||
|
||||
featureMargin := getMarginValue(isCompact, 1, 2)
|
||||
featureStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color(ColorWhite)).
|
||||
MarginLeft(featureMargin)
|
||||
|
||||
keyHintStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color(ColorPrimary)).
|
||||
Bold(true)
|
||||
|
||||
commands := m.getActionCommands(keyHintStyle)
|
||||
b.WriteString(featureStyle.Render(commands.commandsText))
|
||||
b.WriteString("\n")
|
||||
b.WriteString(featureStyle.Render(commands.quitText))
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *model) getQuickActionsTitle() string {
|
||||
if shouldUseCompactLayout(m.width, BreakpointTiny) {
|
||||
return "Actions"
|
||||
}
|
||||
if shouldUseCompactLayout(m.width, BreakpointLarge) {
|
||||
return "Quick Actions"
|
||||
}
|
||||
return "✨ Quick Actions"
|
||||
}
|
||||
|
||||
type actionCommands struct {
|
||||
commandsText string
|
||||
quitText string
|
||||
}
|
||||
|
||||
func (m *model) getActionCommands(keyHintStyle lipgloss.Style) actionCommands {
|
||||
if shouldUseCompactLayout(m.width, BreakpointSmall) {
|
||||
return actionCommands{
|
||||
commandsText: fmt.Sprintf(" %s Commands", keyHintStyle.Render("[C]")),
|
||||
quitText: fmt.Sprintf(" %s Quit", keyHintStyle.Render("[Q]")),
|
||||
}
|
||||
}
|
||||
|
||||
return actionCommands{
|
||||
commandsText: fmt.Sprintf(" %s Open commands menu", keyHintStyle.Render("[C]")),
|
||||
quitText: fmt.Sprintf(" %s Quit application", keyHintStyle.Render("[Q]")),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *model) renderFooter(isCompact bool) string {
|
||||
if isCompact {
|
||||
return ""
|
||||
}
|
||||
|
||||
footerStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color(ColorDarkGray)).
|
||||
Italic(true)
|
||||
|
||||
return "\n\n" + footerStyle.Render("Press 'C' to customize your tunnel settings")
|
||||
}
|
||||
|
||||
func getMarginValue(isCompact bool, compactValue, normalValue int) int {
|
||||
if isCompact {
|
||||
return compactValue
|
||||
}
|
||||
return normalValue
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
package interaction
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"sync"
|
||||
"tunnel_pls/internal/config"
|
||||
"tunnel_pls/internal/random"
|
||||
"tunnel_pls/internal/session/slug"
|
||||
"tunnel_pls/types"
|
||||
|
||||
"github.com/charmbracelet/bubbles/help"
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
"github.com/charmbracelet/bubbles/list"
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/muesli/termenv"
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
type Interaction interface {
|
||||
Mode() types.InteractiveMode
|
||||
SetChannel(channel ssh.Channel)
|
||||
SetMode(m types.InteractiveMode)
|
||||
SetWH(w, h int)
|
||||
Start()
|
||||
Redraw()
|
||||
Send(message string) error
|
||||
}
|
||||
|
||||
type SessionRegistry interface {
|
||||
Update(user string, oldKey, newKey types.SessionKey) error
|
||||
}
|
||||
|
||||
type Forwarder interface {
|
||||
Close() error
|
||||
TunnelType() types.TunnelType
|
||||
ForwardedPort() uint16
|
||||
}
|
||||
|
||||
type CloseFunc func() error
|
||||
type interaction struct {
|
||||
randomizer random.Random
|
||||
config config.Config
|
||||
channel ssh.Channel
|
||||
slug slug.Slug
|
||||
forwarder Forwarder
|
||||
closeFunc CloseFunc
|
||||
user string
|
||||
sessionRegistry SessionRegistry
|
||||
program *tea.Program
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
mode types.InteractiveMode
|
||||
programMu sync.Mutex
|
||||
}
|
||||
|
||||
func (i *interaction) SetMode(m types.InteractiveMode) {
|
||||
i.mode = m
|
||||
}
|
||||
|
||||
func (i *interaction) Mode() types.InteractiveMode {
|
||||
return i.mode
|
||||
}
|
||||
|
||||
func (i *interaction) Send(message string) error {
|
||||
if i.channel != nil {
|
||||
_, err := i.channel.Write([]byte(message))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (i *interaction) SetWH(w, h int) {
|
||||
if i.program != nil {
|
||||
i.program.Send(tea.WindowSizeMsg{
|
||||
Width: w,
|
||||
Height: h,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func New(randomizer random.Random, config config.Config, slug slug.Slug, forwarder Forwarder, sessionRegistry SessionRegistry, user string, closeFunc CloseFunc) Interaction {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
return &interaction{
|
||||
randomizer: randomizer,
|
||||
config: config,
|
||||
channel: nil,
|
||||
slug: slug,
|
||||
forwarder: forwarder,
|
||||
closeFunc: closeFunc,
|
||||
user: user,
|
||||
sessionRegistry: sessionRegistry,
|
||||
program: nil,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
}
|
||||
}
|
||||
|
||||
func (i *interaction) SetChannel(channel ssh.Channel) {
|
||||
i.channel = channel
|
||||
}
|
||||
|
||||
func (i *interaction) Stop() {
|
||||
if i.cancel != nil {
|
||||
i.cancel()
|
||||
}
|
||||
|
||||
i.programMu.Lock()
|
||||
defer i.programMu.Unlock()
|
||||
|
||||
if i.program != nil {
|
||||
i.program.Kill()
|
||||
i.program = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
|
||||
switch msg := msg.(type) {
|
||||
case tickMsg:
|
||||
m.showingComingSoon = false
|
||||
return m, tea.Batch(tea.ClearScreen, textinput.Blink)
|
||||
|
||||
case tea.WindowSizeMsg:
|
||||
m.width = msg.Width
|
||||
m.height = msg.Height
|
||||
m.commandList.SetWidth(msg.Width)
|
||||
m.commandList.SetHeight(msg.Height - 4)
|
||||
|
||||
if msg.Width < 80 {
|
||||
m.slugInput.Width = msg.Width - 10
|
||||
} else {
|
||||
m.slugInput.Width = 50
|
||||
}
|
||||
return m, nil
|
||||
|
||||
case tea.QuitMsg:
|
||||
m.quitting = true
|
||||
return m, tea.Batch(tea.ClearScreen, textinput.Blink, tea.Quit)
|
||||
|
||||
case tea.KeyMsg:
|
||||
if m.showingComingSoon {
|
||||
return m.comingSoonUpdate(msg)
|
||||
}
|
||||
|
||||
if m.editingSlug {
|
||||
return m.slugUpdate(msg)
|
||||
}
|
||||
|
||||
if m.showingCommands {
|
||||
return m.commandsUpdate(msg)
|
||||
}
|
||||
|
||||
return m.dashboardUpdate(msg)
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (i *interaction) Redraw() {
|
||||
if i.program != nil {
|
||||
i.program.Send(tea.ClearScreen())
|
||||
}
|
||||
}
|
||||
|
||||
func (m *model) View() string {
|
||||
if m.quitting {
|
||||
return ""
|
||||
}
|
||||
|
||||
if m.showingComingSoon {
|
||||
return m.comingSoonView()
|
||||
}
|
||||
|
||||
if m.editingSlug {
|
||||
return m.slugView()
|
||||
}
|
||||
|
||||
if m.showingCommands {
|
||||
return m.commandsView()
|
||||
}
|
||||
|
||||
return m.dashboardView()
|
||||
}
|
||||
|
||||
func (i *interaction) Start() {
|
||||
if i.mode == types.InteractiveModeHEADLESS {
|
||||
return
|
||||
}
|
||||
lipgloss.SetColorProfile(termenv.TrueColor)
|
||||
|
||||
protocol := "http"
|
||||
if i.config.TLSEnabled() {
|
||||
protocol = "https"
|
||||
}
|
||||
|
||||
tunnelType := i.forwarder.TunnelType()
|
||||
port := i.forwarder.ForwardedPort()
|
||||
|
||||
items := []list.Item{
|
||||
commandItem{name: "slug", desc: "Set custom subdomain"},
|
||||
commandItem{name: "tunnel-type", desc: "Change tunnel type (Coming Soon)"},
|
||||
}
|
||||
|
||||
delegate := list.NewDefaultDelegate()
|
||||
delegate.ShowDescription = true
|
||||
delegate.SetHeight(2)
|
||||
|
||||
commandList := list.New(items, delegate, 80, 20)
|
||||
commandList.Title = "Select a command"
|
||||
commandList.SetShowStatusBar(false)
|
||||
commandList.SetFilteringEnabled(false)
|
||||
commandList.SetShowHelp(false)
|
||||
|
||||
ti := textinput.New()
|
||||
ti.Placeholder = "my-custom-slug"
|
||||
ti.CharLimit = 20
|
||||
ti.Width = 50
|
||||
|
||||
m := &model{
|
||||
randomizer: i.randomizer,
|
||||
domain: i.config.Domain(),
|
||||
protocol: protocol,
|
||||
tunnelType: tunnelType,
|
||||
port: port,
|
||||
commandList: commandList,
|
||||
slugInput: ti,
|
||||
interaction: i,
|
||||
keymap: keymap{
|
||||
quit: key.NewBinding(
|
||||
key.WithKeys("q", "ctrl+c"),
|
||||
key.WithHelp("q", "quit"),
|
||||
),
|
||||
command: key.NewBinding(
|
||||
key.WithKeys("c"),
|
||||
key.WithHelp("c", "commands"),
|
||||
),
|
||||
random: key.NewBinding(
|
||||
key.WithKeys("ctrl+r"),
|
||||
key.WithHelp("ctrl+r", "random"),
|
||||
),
|
||||
},
|
||||
help: help.New(),
|
||||
}
|
||||
|
||||
i.programMu.Lock()
|
||||
i.program = tea.NewProgram(
|
||||
m,
|
||||
tea.WithInput(i.channel),
|
||||
tea.WithOutput(i.channel),
|
||||
tea.WithAltScreen(),
|
||||
tea.WithMouseCellMotion(),
|
||||
tea.WithoutSignals(),
|
||||
tea.WithoutSignalHandler(),
|
||||
tea.WithFPS(30),
|
||||
)
|
||||
i.programMu.Unlock()
|
||||
|
||||
_, err := i.program.Run()
|
||||
if err != nil {
|
||||
log.Printf("Cannot close tea: %s \n", err)
|
||||
}
|
||||
|
||||
i.programMu.Lock()
|
||||
if i.program != nil {
|
||||
i.program.Kill()
|
||||
i.program = nil
|
||||
}
|
||||
i.programMu.Unlock()
|
||||
|
||||
if i.closeFunc != nil {
|
||||
_ = i.closeFunc()
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,116 @@
|
||||
package interaction
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"tunnel_pls/internal/random"
|
||||
"tunnel_pls/types"
|
||||
|
||||
"github.com/charmbracelet/bubbles/help"
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
"github.com/charmbracelet/bubbles/list"
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
type commandItem struct {
|
||||
name string
|
||||
desc string
|
||||
}
|
||||
|
||||
func (i commandItem) FilterValue() string { return i.name }
|
||||
func (i commandItem) Title() string { return i.name }
|
||||
func (i commandItem) Description() string { return i.desc }
|
||||
|
||||
type model struct {
|
||||
randomizer random.Random
|
||||
domain string
|
||||
protocol string
|
||||
tunnelType types.TunnelType
|
||||
port uint16
|
||||
keymap keymap
|
||||
help help.Model
|
||||
quitting bool
|
||||
showingCommands bool
|
||||
editingSlug bool
|
||||
showingComingSoon bool
|
||||
commandList list.Model
|
||||
slugInput textinput.Model
|
||||
slugError string
|
||||
interaction *interaction
|
||||
width int
|
||||
height int
|
||||
}
|
||||
|
||||
const (
|
||||
ColorPrimary = "#7D56F4"
|
||||
ColorSecondary = "#04B575"
|
||||
ColorGray = "#888888"
|
||||
ColorDarkGray = "#666666"
|
||||
ColorWhite = "#FAFAFA"
|
||||
ColorError = "#FF0000"
|
||||
ColorErrorBg = "#3D0000"
|
||||
ColorWarning = "#FFA500"
|
||||
ColorWarningBg = "#3D2000"
|
||||
)
|
||||
|
||||
const (
|
||||
BreakpointTiny = 50
|
||||
BreakpointSmall = 60
|
||||
BreakpointMedium = 70
|
||||
BreakpointLarge = 85
|
||||
)
|
||||
|
||||
func (m *model) getTunnelURL() string {
|
||||
if m.tunnelType == types.TunnelTypeHTTP {
|
||||
return buildURL(m.protocol, m.interaction.slug.String(), m.domain)
|
||||
}
|
||||
return fmt.Sprintf("tcp://%s:%d", m.domain, m.port)
|
||||
}
|
||||
|
||||
type keymap struct {
|
||||
quit key.Binding
|
||||
command key.Binding
|
||||
random key.Binding
|
||||
}
|
||||
|
||||
type tickMsg time.Time
|
||||
|
||||
func (m *model) Init() tea.Cmd {
|
||||
return tea.Batch(textinput.Blink, tea.WindowSize())
|
||||
}
|
||||
|
||||
func getResponsiveWidth(screenWidth, padding, minWidth, maxWidth int) int {
|
||||
width := screenWidth - padding
|
||||
if width > maxWidth {
|
||||
width = maxWidth
|
||||
}
|
||||
if width < minWidth {
|
||||
width = minWidth
|
||||
}
|
||||
return width
|
||||
}
|
||||
|
||||
func shouldUseCompactLayout(width int, threshold int) bool {
|
||||
return width < threshold
|
||||
}
|
||||
|
||||
func truncateString(s string, maxLength int) string {
|
||||
if len(s) <= maxLength {
|
||||
return s
|
||||
}
|
||||
if maxLength < 4 {
|
||||
return s[:maxLength]
|
||||
}
|
||||
return s[:maxLength-3] + "..."
|
||||
}
|
||||
|
||||
func tickCmd(d time.Duration) tea.Cmd {
|
||||
return tea.Tick(d, func(t time.Time) tea.Msg {
|
||||
return tickMsg(t)
|
||||
})
|
||||
}
|
||||
|
||||
func buildURL(protocol, subdomain, domain string) string {
|
||||
return fmt.Sprintf("%s://%s.%s", protocol, subdomain, domain)
|
||||
}
|
||||
@@ -0,0 +1,265 @@
|
||||
package interaction
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"tunnel_pls/types"
|
||||
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
func (m *model) slugUpdate(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
var cmd tea.Cmd
|
||||
|
||||
if m.tunnelType != types.TunnelTypeHTTP {
|
||||
m.editingSlug = false
|
||||
m.slugError = ""
|
||||
return m, tea.Batch(tea.ClearScreen, textinput.Blink)
|
||||
}
|
||||
|
||||
switch msg.String() {
|
||||
case "esc", "ctrl+c":
|
||||
m.editingSlug = false
|
||||
m.slugError = ""
|
||||
return m, tea.Batch(tea.ClearScreen, textinput.Blink)
|
||||
case "enter":
|
||||
inputValue := m.slugInput.Value()
|
||||
if err := m.interaction.sessionRegistry.Update(m.interaction.user, types.SessionKey{
|
||||
Id: m.interaction.slug.String(),
|
||||
Type: types.TunnelTypeHTTP,
|
||||
}, types.SessionKey{
|
||||
Id: inputValue,
|
||||
Type: types.TunnelTypeHTTP,
|
||||
}); err != nil {
|
||||
m.slugError = err.Error()
|
||||
return m, nil
|
||||
}
|
||||
m.editingSlug = false
|
||||
m.slugError = ""
|
||||
return m, tea.Batch(tea.ClearScreen, textinput.Blink)
|
||||
default:
|
||||
if key.Matches(msg, m.keymap.random) {
|
||||
newSubdomain, err := m.randomizer.String(20)
|
||||
if err != nil {
|
||||
return m, cmd
|
||||
}
|
||||
m.slugInput.SetValue(newSubdomain)
|
||||
}
|
||||
m.slugError = ""
|
||||
m.slugInput, cmd = m.slugInput.Update(msg)
|
||||
return m, cmd
|
||||
}
|
||||
}
|
||||
|
||||
func (m *model) slugView() string {
|
||||
isCompact := shouldUseCompactLayout(m.width, BreakpointMedium)
|
||||
isVeryCompact := shouldUseCompactLayout(m.width, BreakpointTiny)
|
||||
|
||||
var b strings.Builder
|
||||
b.WriteString(m.renderSlugTitle(isVeryCompact))
|
||||
|
||||
if m.tunnelType != types.TunnelTypeHTTP {
|
||||
b.WriteString(m.renderTCPWarning(isVeryCompact, isCompact))
|
||||
return b.String()
|
||||
}
|
||||
|
||||
b.WriteString(m.renderSlugRules(isVeryCompact, isCompact))
|
||||
b.WriteString(m.renderSlugInstruction(isVeryCompact))
|
||||
b.WriteString(m.renderSlugInput(isVeryCompact, isCompact))
|
||||
b.WriteString(m.renderSlugPreview(isVeryCompact))
|
||||
b.WriteString(m.renderSlugHelp(isVeryCompact))
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *model) renderSlugTitle(isVeryCompact bool) string {
|
||||
titleStyle := lipgloss.NewStyle().
|
||||
Bold(true).
|
||||
Foreground(lipgloss.Color(ColorPrimary)).
|
||||
PaddingTop(1).
|
||||
PaddingBottom(1)
|
||||
|
||||
title := "🔧 Edit Subdomain"
|
||||
if isVeryCompact {
|
||||
title = "Edit Subdomain"
|
||||
}
|
||||
|
||||
return titleStyle.Render(title) + "\n\n"
|
||||
}
|
||||
|
||||
func (m *model) renderTCPWarning(isVeryCompact, isCompact bool) string {
|
||||
boxPadding := getPaddingValue(isVeryCompact, isCompact)
|
||||
boxMargin := getMarginValue(isCompact, 1, 2)
|
||||
warningBoxWidth := getResponsiveWidth(m.width, 10, 30, 60)
|
||||
|
||||
warningBoxStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color(ColorWarning)).
|
||||
Background(lipgloss.Color(ColorWarningBg)).
|
||||
Bold(true).
|
||||
Border(lipgloss.RoundedBorder()).
|
||||
BorderForeground(lipgloss.Color(ColorWarning)).
|
||||
Padding(1, boxPadding).
|
||||
MarginTop(boxMargin).
|
||||
MarginBottom(boxMargin).
|
||||
Width(warningBoxWidth)
|
||||
|
||||
helpStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color(ColorDarkGray)).
|
||||
Italic(true).
|
||||
MarginTop(1)
|
||||
|
||||
warningText := m.getTCPWarningText(isVeryCompact)
|
||||
helpText := m.getTCPHelpText(isVeryCompact)
|
||||
|
||||
var b strings.Builder
|
||||
b.WriteString(warningBoxStyle.Render(warningText))
|
||||
b.WriteString("\n\n")
|
||||
b.WriteString(helpStyle.Render(helpText))
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *model) getTCPWarningText(isVeryCompact bool) string {
|
||||
if isVeryCompact {
|
||||
return "⚠️ TCP tunnels don't support custom subdomains."
|
||||
}
|
||||
return "⚠️ TCP tunnels cannot have custom subdomains. Only HTTP/HTTPS tunnels support subdomain customization."
|
||||
}
|
||||
|
||||
func (m *model) getTCPHelpText(isVeryCompact bool) string {
|
||||
if isVeryCompact {
|
||||
return "Press any key to go back"
|
||||
}
|
||||
return "Press Enter or Esc to go back"
|
||||
}
|
||||
|
||||
func (m *model) renderSlugRules(isVeryCompact, isCompact bool) string {
|
||||
boxPadding := getPaddingValue(isVeryCompact, isCompact)
|
||||
rulesBoxWidth := getResponsiveWidth(m.width, 10, 30, 60)
|
||||
|
||||
rulesBoxStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color(ColorWhite)).
|
||||
Border(lipgloss.RoundedBorder()).
|
||||
BorderForeground(lipgloss.Color(ColorPrimary)).
|
||||
Padding(0, boxPadding).
|
||||
MarginTop(1).
|
||||
MarginBottom(1).
|
||||
Width(rulesBoxWidth)
|
||||
|
||||
rulesContent := m.getRulesContent(isVeryCompact, isCompact)
|
||||
return rulesBoxStyle.Render(rulesContent) + "\n"
|
||||
}
|
||||
|
||||
func (m *model) getRulesContent(isVeryCompact, isCompact bool) string {
|
||||
if isVeryCompact {
|
||||
return "Rules:\n3-20 chars\na-z, 0-9, -\nNo leading/trailing -"
|
||||
}
|
||||
|
||||
if isCompact {
|
||||
return "📋 Rules:\n • 3-20 chars\n • a-z, 0-9, -\n • No leading/trailing -"
|
||||
}
|
||||
|
||||
return "📋 Rules: \n\t• 3-20 chars \n\t• a-z, 0-9, - \n\t• No leading/trailing -"
|
||||
}
|
||||
|
||||
func (m *model) renderSlugInstruction(isVeryCompact bool) string {
|
||||
instructionStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color(ColorWhite)).
|
||||
MarginTop(1)
|
||||
|
||||
instruction := "Enter your custom subdomain:"
|
||||
if isVeryCompact {
|
||||
instruction = "Custom subdomain:"
|
||||
}
|
||||
|
||||
return instructionStyle.Render(instruction) + "\n"
|
||||
}
|
||||
|
||||
func (m *model) renderSlugInput(isVeryCompact, isCompact bool) string {
|
||||
boxPadding := getPaddingValue(isVeryCompact, isCompact)
|
||||
boxMargin := getMarginValue(isCompact, 1, 2)
|
||||
|
||||
if m.slugError != "" {
|
||||
return m.renderErrorInput(boxPadding, boxMargin)
|
||||
}
|
||||
|
||||
return m.renderNormalInput(boxPadding, boxMargin)
|
||||
}
|
||||
|
||||
func (m *model) renderErrorInput(boxPadding, boxMargin int) string {
|
||||
errorInputBoxStyle := lipgloss.NewStyle().
|
||||
Border(lipgloss.RoundedBorder()).
|
||||
BorderForeground(lipgloss.Color(ColorError)).
|
||||
Padding(1, boxPadding).
|
||||
MarginTop(boxMargin).
|
||||
MarginBottom(1)
|
||||
|
||||
errorBoxStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color(ColorError)).
|
||||
Background(lipgloss.Color(ColorErrorBg)).
|
||||
Bold(true).
|
||||
Border(lipgloss.RoundedBorder()).
|
||||
BorderForeground(lipgloss.Color(ColorError)).
|
||||
Padding(0, boxPadding).
|
||||
MarginTop(1).
|
||||
MarginBottom(1)
|
||||
|
||||
var b strings.Builder
|
||||
b.WriteString(errorInputBoxStyle.Render(m.slugInput.View()))
|
||||
b.WriteString("\n")
|
||||
b.WriteString(errorBoxStyle.Render("❌ " + m.slugError))
|
||||
b.WriteString("\n")
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *model) renderNormalInput(boxPadding, boxMargin int) string {
|
||||
inputBoxStyle := lipgloss.NewStyle().
|
||||
Border(lipgloss.RoundedBorder()).
|
||||
BorderForeground(lipgloss.Color(ColorPrimary)).
|
||||
Padding(1, boxPadding).
|
||||
MarginTop(boxMargin).
|
||||
MarginBottom(boxMargin)
|
||||
|
||||
return inputBoxStyle.Render(m.slugInput.View()) + "\n"
|
||||
}
|
||||
|
||||
func (m *model) renderSlugPreview(isVeryCompact bool) string {
|
||||
previewURL := buildURL(m.protocol, m.slugInput.Value(), m.domain)
|
||||
previewWidth := getResponsiveWidth(m.width, 10, 30, 80)
|
||||
|
||||
if isVeryCompact {
|
||||
previewURL = truncateString(previewURL, previewWidth-10)
|
||||
}
|
||||
|
||||
previewStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color(ColorSecondary)).
|
||||
Italic(true).
|
||||
Width(previewWidth)
|
||||
|
||||
return previewStyle.Render(fmt.Sprintf("Preview: %s", previewURL)) + "\n"
|
||||
}
|
||||
|
||||
func (m *model) renderSlugHelp(isVeryCompact bool) string {
|
||||
helpStyle := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color(ColorDarkGray)).
|
||||
Italic(true).
|
||||
MarginTop(1)
|
||||
|
||||
helpText := "Press Enter to save • CTRL+R for random • Esc to cancel"
|
||||
if isVeryCompact {
|
||||
helpText = "Enter: save • CTRL+R: random • Esc: cancel"
|
||||
}
|
||||
|
||||
return helpStyle.Render(helpText)
|
||||
}
|
||||
|
||||
func getPaddingValue(isVeryCompact, isCompact bool) int {
|
||||
if isVeryCompact || isCompact {
|
||||
return 1
|
||||
}
|
||||
return 2
|
||||
}
|
||||
Reference in New Issue
Block a user