mirror of
https://github.com/Gu1llaum-3/sshm.git
synced 2025-10-19 01:17:20 +02:00
Compare commits
2 Commits
12d97270f0
...
825c534ebe
Author | SHA1 | Date | |
---|---|---|---|
825c534ebe | |||
c1457af73a |
12
cmd/root.go
12
cmd/root.go
@ -140,11 +140,17 @@ func connectToHost(hostName string) {
|
|||||||
fmt.Printf("Connecting to %s...\n", hostName)
|
fmt.Printf("Connecting to %s...\n", hostName)
|
||||||
|
|
||||||
var sshCmd *exec.Cmd
|
var sshCmd *exec.Cmd
|
||||||
|
var args []string
|
||||||
|
|
||||||
if configFile != "" {
|
if configFile != "" {
|
||||||
sshCmd = exec.Command("ssh", "-F", configFile, hostName)
|
args = append(args, "-F", configFile)
|
||||||
} else {
|
|
||||||
sshCmd = exec.Command("ssh", hostName)
|
|
||||||
}
|
}
|
||||||
|
args = append(args, hostName)
|
||||||
|
|
||||||
|
// Note: We don't add RemoteCommand here because if it's configured in SSH config,
|
||||||
|
// SSH will handle it automatically. Adding it as a command line argument would conflict.
|
||||||
|
|
||||||
|
sshCmd = exec.Command("ssh", args...)
|
||||||
|
|
||||||
// Set up the command to use the same stdin, stdout, and stderr as the parent process
|
// Set up the command to use the same stdin, stdout, and stderr as the parent process
|
||||||
sshCmd.Stdin = os.Stdin
|
sshCmd.Stdin = os.Stdin
|
||||||
|
@ -13,15 +13,17 @@ import (
|
|||||||
|
|
||||||
// SSHHost represents an SSH host configuration
|
// SSHHost represents an SSH host configuration
|
||||||
type SSHHost struct {
|
type SSHHost struct {
|
||||||
Name string
|
Name string
|
||||||
Hostname string
|
Hostname string
|
||||||
User string
|
User string
|
||||||
Port string
|
Port string
|
||||||
Identity string
|
Identity string
|
||||||
ProxyJump string
|
ProxyJump string
|
||||||
Options string
|
Options string
|
||||||
Tags []string
|
RemoteCommand string // Command to execute after SSH connection
|
||||||
SourceFile string // Path to the config file where this host is defined
|
RequestTTY string // Request TTY (yes, no, force, auto)
|
||||||
|
Tags []string
|
||||||
|
SourceFile string // Path to the config file where this host is defined
|
||||||
|
|
||||||
// Temporary field to handle multiple aliases during parsing
|
// Temporary field to handle multiple aliases during parsing
|
||||||
aliasNames []string `json:"-"` // Do not serialize this field
|
aliasNames []string `json:"-"` // Do not serialize this field
|
||||||
@ -326,6 +328,14 @@ func parseSSHConfigFileWithProcessedFiles(configPath string, processedFiles map[
|
|||||||
if currentHost != nil {
|
if currentHost != nil {
|
||||||
currentHost.ProxyJump = value
|
currentHost.ProxyJump = value
|
||||||
}
|
}
|
||||||
|
case "remotecommand":
|
||||||
|
if currentHost != nil {
|
||||||
|
currentHost.RemoteCommand = value
|
||||||
|
}
|
||||||
|
case "requesttty":
|
||||||
|
if currentHost != nil {
|
||||||
|
currentHost.RequestTTY = value
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
// Handle other SSH options
|
// Handle other SSH options
|
||||||
if currentHost != nil && strings.TrimSpace(line) != "" {
|
if currentHost != nil && strings.TrimSpace(line) != "" {
|
||||||
@ -603,6 +613,20 @@ func AddSSHHostToFile(host SSHHost, configPath string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if host.RemoteCommand != "" {
|
||||||
|
_, err = file.WriteString(fmt.Sprintf(" RemoteCommand %s\n", host.RemoteCommand))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if host.RequestTTY != "" {
|
||||||
|
_, err = file.WriteString(fmt.Sprintf(" RequestTTY %s\n", host.RequestTTY))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Write SSH options
|
// Write SSH options
|
||||||
if host.Options != "" {
|
if host.Options != "" {
|
||||||
// Split options by newlines and write each one
|
// Split options by newlines and write each one
|
||||||
@ -1020,6 +1044,12 @@ func UpdateSSHHostInFile(oldName string, newHost SSHHost, configPath string) err
|
|||||||
if newHost.ProxyJump != "" {
|
if newHost.ProxyJump != "" {
|
||||||
newLines = append(newLines, " ProxyJump "+newHost.ProxyJump)
|
newLines = append(newLines, " ProxyJump "+newHost.ProxyJump)
|
||||||
}
|
}
|
||||||
|
if newHost.RemoteCommand != "" {
|
||||||
|
newLines = append(newLines, " RemoteCommand "+newHost.RemoteCommand)
|
||||||
|
}
|
||||||
|
if newHost.RequestTTY != "" {
|
||||||
|
newLines = append(newLines, " RequestTTY "+newHost.RequestTTY)
|
||||||
|
}
|
||||||
// Write SSH options
|
// Write SSH options
|
||||||
if newHost.Options != "" {
|
if newHost.Options != "" {
|
||||||
options := strings.Split(newHost.Options, "\n")
|
options := strings.Split(newHost.Options, "\n")
|
||||||
@ -1068,6 +1098,12 @@ func UpdateSSHHostInFile(oldName string, newHost SSHHost, configPath string) err
|
|||||||
if newHost.ProxyJump != "" {
|
if newHost.ProxyJump != "" {
|
||||||
newLines = append(newLines, " ProxyJump "+newHost.ProxyJump)
|
newLines = append(newLines, " ProxyJump "+newHost.ProxyJump)
|
||||||
}
|
}
|
||||||
|
if newHost.RemoteCommand != "" {
|
||||||
|
newLines = append(newLines, " RemoteCommand "+newHost.RemoteCommand)
|
||||||
|
}
|
||||||
|
if newHost.RequestTTY != "" {
|
||||||
|
newLines = append(newLines, " RequestTTY "+newHost.RequestTTY)
|
||||||
|
}
|
||||||
// Write SSH options
|
// Write SSH options
|
||||||
if newHost.Options != "" {
|
if newHost.Options != "" {
|
||||||
options := strings.Split(newHost.Options, "\n")
|
options := strings.Split(newHost.Options, "\n")
|
||||||
@ -1152,6 +1188,12 @@ func UpdateSSHHostInFile(oldName string, newHost SSHHost, configPath string) err
|
|||||||
if newHost.ProxyJump != "" {
|
if newHost.ProxyJump != "" {
|
||||||
newLines = append(newLines, " ProxyJump "+newHost.ProxyJump)
|
newLines = append(newLines, " ProxyJump "+newHost.ProxyJump)
|
||||||
}
|
}
|
||||||
|
if newHost.RemoteCommand != "" {
|
||||||
|
newLines = append(newLines, " RemoteCommand "+newHost.RemoteCommand)
|
||||||
|
}
|
||||||
|
if newHost.RequestTTY != "" {
|
||||||
|
newLines = append(newLines, " RequestTTY "+newHost.RequestTTY)
|
||||||
|
}
|
||||||
// Write SSH options
|
// Write SSH options
|
||||||
if newHost.Options != "" {
|
if newHost.Options != "" {
|
||||||
options := strings.Split(newHost.Options, "\n")
|
options := strings.Split(newHost.Options, "\n")
|
||||||
@ -1200,6 +1242,12 @@ func UpdateSSHHostInFile(oldName string, newHost SSHHost, configPath string) err
|
|||||||
if newHost.ProxyJump != "" {
|
if newHost.ProxyJump != "" {
|
||||||
newLines = append(newLines, " ProxyJump "+newHost.ProxyJump)
|
newLines = append(newLines, " ProxyJump "+newHost.ProxyJump)
|
||||||
}
|
}
|
||||||
|
if newHost.RemoteCommand != "" {
|
||||||
|
newLines = append(newLines, " RemoteCommand "+newHost.RemoteCommand)
|
||||||
|
}
|
||||||
|
if newHost.RequestTTY != "" {
|
||||||
|
newLines = append(newLines, " RequestTTY "+newHost.RequestTTY)
|
||||||
|
}
|
||||||
// Write SSH options
|
// Write SSH options
|
||||||
if newHost.Options != "" {
|
if newHost.Options != "" {
|
||||||
options := strings.Split(newHost.Options, "\n")
|
options := strings.Split(newHost.Options, "\n")
|
||||||
@ -1694,6 +1742,12 @@ func UpdateMultiHostBlock(originalHosts, newHosts []string, commonProperties SSH
|
|||||||
if commonProperties.ProxyJump != "" {
|
if commonProperties.ProxyJump != "" {
|
||||||
newLines = append(newLines, " ProxyJump "+commonProperties.ProxyJump)
|
newLines = append(newLines, " ProxyJump "+commonProperties.ProxyJump)
|
||||||
}
|
}
|
||||||
|
if commonProperties.RemoteCommand != "" {
|
||||||
|
newLines = append(newLines, " RemoteCommand "+commonProperties.RemoteCommand)
|
||||||
|
}
|
||||||
|
if commonProperties.RequestTTY != "" {
|
||||||
|
newLines = append(newLines, " RequestTTY "+commonProperties.RequestTTY)
|
||||||
|
}
|
||||||
|
|
||||||
// Write SSH options
|
// Write SSH options
|
||||||
if commonProperties.Options != "" {
|
if commonProperties.Options != "" {
|
||||||
@ -1774,6 +1828,12 @@ func UpdateMultiHostBlock(originalHosts, newHosts []string, commonProperties SSH
|
|||||||
if commonProperties.ProxyJump != "" {
|
if commonProperties.ProxyJump != "" {
|
||||||
newLines = append(newLines, " ProxyJump "+commonProperties.ProxyJump)
|
newLines = append(newLines, " ProxyJump "+commonProperties.ProxyJump)
|
||||||
}
|
}
|
||||||
|
if commonProperties.RemoteCommand != "" {
|
||||||
|
newLines = append(newLines, " RemoteCommand "+commonProperties.RemoteCommand)
|
||||||
|
}
|
||||||
|
if commonProperties.RequestTTY != "" {
|
||||||
|
newLines = append(newLines, " RequestTTY "+commonProperties.RequestTTY)
|
||||||
|
}
|
||||||
|
|
||||||
// Write SSH options
|
// Write SSH options
|
||||||
if commonProperties.Options != "" {
|
if commonProperties.Options != "" {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package ui
|
package ui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -16,6 +17,7 @@ import (
|
|||||||
type addFormModel struct {
|
type addFormModel struct {
|
||||||
inputs []textinput.Model
|
inputs []textinput.Model
|
||||||
focused int
|
focused int
|
||||||
|
currentTab int // 0 = General, 1 = Advanced
|
||||||
err string
|
err string
|
||||||
styles Styles
|
styles Styles
|
||||||
success bool
|
success bool
|
||||||
@ -47,7 +49,7 @@ func NewAddForm(hostname string, styles Styles, width, height int, configFile st
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inputs := make([]textinput.Model, 8)
|
inputs := make([]textinput.Model, 10) // Increased from 9 to 10 for RequestTTY
|
||||||
|
|
||||||
// Name input
|
// Name input
|
||||||
inputs[nameInput] = textinput.New()
|
inputs[nameInput] = textinput.New()
|
||||||
@ -101,9 +103,22 @@ func NewAddForm(hostname string, styles Styles, width, height int, configFile st
|
|||||||
inputs[tagsInput].CharLimit = 200
|
inputs[tagsInput].CharLimit = 200
|
||||||
inputs[tagsInput].Width = 50
|
inputs[tagsInput].Width = 50
|
||||||
|
|
||||||
|
// Remote Command input
|
||||||
|
inputs[remoteCommandInput] = textinput.New()
|
||||||
|
inputs[remoteCommandInput].Placeholder = "ls -la, htop, bash"
|
||||||
|
inputs[remoteCommandInput].CharLimit = 300
|
||||||
|
inputs[remoteCommandInput].Width = 70
|
||||||
|
|
||||||
|
// RequestTTY input
|
||||||
|
inputs[requestTTYInput] = textinput.New()
|
||||||
|
inputs[requestTTYInput].Placeholder = "yes, no, force, auto"
|
||||||
|
inputs[requestTTYInput].CharLimit = 10
|
||||||
|
inputs[requestTTYInput].Width = 30
|
||||||
|
|
||||||
return &addFormModel{
|
return &addFormModel{
|
||||||
inputs: inputs,
|
inputs: inputs,
|
||||||
focused: nameInput,
|
focused: nameInput,
|
||||||
|
currentTab: tabGeneral, // Start on General tab
|
||||||
styles: styles,
|
styles: styles,
|
||||||
width: width,
|
width: width,
|
||||||
height: height,
|
height: height,
|
||||||
@ -111,6 +126,11 @@ func NewAddForm(hostname string, styles Styles, width, height int, configFile st
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
tabGeneral = iota
|
||||||
|
tabAdvanced
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
nameInput = iota
|
nameInput = iota
|
||||||
hostnameInput
|
hostnameInput
|
||||||
@ -118,8 +138,11 @@ const (
|
|||||||
portInput
|
portInput
|
||||||
identityInput
|
identityInput
|
||||||
proxyJumpInput
|
proxyJumpInput
|
||||||
optionsInput
|
|
||||||
tagsInput
|
tagsInput
|
||||||
|
// Advanced tab inputs
|
||||||
|
optionsInput
|
||||||
|
remoteCommandInput
|
||||||
|
requestTTYInput
|
||||||
)
|
)
|
||||||
|
|
||||||
// Messages for communication with parent model
|
// Messages for communication with parent model
|
||||||
@ -153,36 +176,20 @@ func (m *addFormModel) Update(msg tea.Msg) (*addFormModel, tea.Cmd) {
|
|||||||
// Allow submission from any field with Ctrl+S (Save)
|
// Allow submission from any field with Ctrl+S (Save)
|
||||||
return m, m.submitForm()
|
return m, m.submitForm()
|
||||||
|
|
||||||
|
case "ctrl+j":
|
||||||
|
// Switch to next tab
|
||||||
|
m.currentTab = (m.currentTab + 1) % 2
|
||||||
|
m.focused = m.getFirstInputForTab(m.currentTab)
|
||||||
|
return m, m.updateFocus()
|
||||||
|
|
||||||
|
case "ctrl+k":
|
||||||
|
// Switch to previous tab
|
||||||
|
m.currentTab = (m.currentTab - 1 + 2) % 2
|
||||||
|
m.focused = m.getFirstInputForTab(m.currentTab)
|
||||||
|
return m, m.updateFocus()
|
||||||
|
|
||||||
case "tab", "shift+tab", "enter", "up", "down":
|
case "tab", "shift+tab", "enter", "up", "down":
|
||||||
s := msg.String()
|
return m, m.handleNavigation(msg.String())
|
||||||
|
|
||||||
// Handle form submission
|
|
||||||
if s == "enter" && m.focused == len(m.inputs)-1 {
|
|
||||||
return m, m.submitForm()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cycle inputs
|
|
||||||
if s == "up" || s == "shift+tab" {
|
|
||||||
m.focused--
|
|
||||||
} else {
|
|
||||||
m.focused++
|
|
||||||
}
|
|
||||||
|
|
||||||
if m.focused > len(m.inputs)-1 {
|
|
||||||
m.focused = 0
|
|
||||||
} else if m.focused < 0 {
|
|
||||||
m.focused = len(m.inputs) - 1
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range m.inputs {
|
|
||||||
if i == m.focused {
|
|
||||||
cmds = append(cmds, m.inputs[i].Focus())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
m.inputs[i].Blur()
|
|
||||||
}
|
|
||||||
|
|
||||||
return m, tea.Batch(cmds...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case addFormSubmitMsg:
|
case addFormSubmitMsg:
|
||||||
@ -206,32 +213,104 @@ func (m *addFormModel) Update(msg tea.Msg) (*addFormModel, tea.Cmd) {
|
|||||||
return m, tea.Batch(cmds...)
|
return m, tea.Batch(cmds...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getFirstInputForTab returns the first input index for a given tab
|
||||||
|
func (m *addFormModel) getFirstInputForTab(tab int) int {
|
||||||
|
switch tab {
|
||||||
|
case tabGeneral:
|
||||||
|
return nameInput
|
||||||
|
case tabAdvanced:
|
||||||
|
return optionsInput
|
||||||
|
default:
|
||||||
|
return nameInput
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// getInputsForCurrentTab returns the input indices for the current tab
|
||||||
|
func (m *addFormModel) getInputsForCurrentTab() []int {
|
||||||
|
switch m.currentTab {
|
||||||
|
case tabGeneral:
|
||||||
|
return []int{nameInput, hostnameInput, userInput, portInput, identityInput, proxyJumpInput, tagsInput}
|
||||||
|
case tabAdvanced:
|
||||||
|
return []int{optionsInput, remoteCommandInput, requestTTYInput}
|
||||||
|
default:
|
||||||
|
return []int{nameInput, hostnameInput, userInput, portInput, identityInput, proxyJumpInput, tagsInput}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// updateFocus updates focus for inputs
|
||||||
|
func (m *addFormModel) updateFocus() tea.Cmd {
|
||||||
|
var cmds []tea.Cmd
|
||||||
|
for i := range m.inputs {
|
||||||
|
if i == m.focused {
|
||||||
|
cmds = append(cmds, m.inputs[i].Focus())
|
||||||
|
} else {
|
||||||
|
m.inputs[i].Blur()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tea.Batch(cmds...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleNavigation handles tab/arrow navigation within the current tab
|
||||||
|
func (m *addFormModel) handleNavigation(key string) tea.Cmd {
|
||||||
|
currentTabInputs := m.getInputsForCurrentTab()
|
||||||
|
|
||||||
|
// Find current position within the tab
|
||||||
|
currentPos := 0
|
||||||
|
for i, input := range currentTabInputs {
|
||||||
|
if input == m.focused {
|
||||||
|
currentPos = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle form submission on last field of Advanced tab
|
||||||
|
if key == "enter" && m.currentTab == tabAdvanced && currentPos == len(currentTabInputs)-1 {
|
||||||
|
return m.submitForm()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Navigate within current tab
|
||||||
|
if key == "up" || key == "shift+tab" {
|
||||||
|
currentPos--
|
||||||
|
} else {
|
||||||
|
currentPos++
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wrap around within current tab
|
||||||
|
if currentPos >= len(currentTabInputs) {
|
||||||
|
currentPos = 0
|
||||||
|
} else if currentPos < 0 {
|
||||||
|
currentPos = len(currentTabInputs) - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
m.focused = currentTabInputs[currentPos]
|
||||||
|
return m.updateFocus()
|
||||||
|
}
|
||||||
|
|
||||||
func (m *addFormModel) View() string {
|
func (m *addFormModel) View() string {
|
||||||
if m.success {
|
if m.success {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if terminal height is sufficient
|
||||||
|
if !m.isHeightSufficient() {
|
||||||
|
return m.renderHeightWarning()
|
||||||
|
}
|
||||||
|
|
||||||
var b strings.Builder
|
var b strings.Builder
|
||||||
|
|
||||||
b.WriteString(m.styles.FormTitle.Render("Add SSH Host Configuration"))
|
b.WriteString(m.styles.FormTitle.Render("Add SSH Host Configuration"))
|
||||||
b.WriteString("\n\n")
|
b.WriteString("\n\n")
|
||||||
|
|
||||||
fields := []string{
|
// Render tabs
|
||||||
"Host Name *",
|
b.WriteString(m.renderTabs())
|
||||||
"Hostname/IP *",
|
b.WriteString("\n\n")
|
||||||
"User",
|
|
||||||
"Port",
|
|
||||||
"Identity File",
|
|
||||||
"ProxyJump",
|
|
||||||
"SSH Options",
|
|
||||||
"Tags (comma-separated)",
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, field := range fields {
|
// Render current tab content
|
||||||
b.WriteString(m.styles.FormField.Render(field))
|
switch m.currentTab {
|
||||||
b.WriteString("\n")
|
case tabGeneral:
|
||||||
b.WriteString(m.inputs[i].View())
|
b.WriteString(m.renderGeneralTab())
|
||||||
b.WriteString("\n\n")
|
case tabAdvanced:
|
||||||
|
b.WriteString(m.renderAdvancedTab())
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.err != "" {
|
if m.err != "" {
|
||||||
@ -239,13 +318,133 @@ func (m *addFormModel) View() string {
|
|||||||
b.WriteString("\n\n")
|
b.WriteString("\n\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
b.WriteString(m.styles.FormHelp.Render("Tab/Shift+Tab: navigate • Enter on last field: submit • Ctrl+S: save • Ctrl+C/Esc: cancel"))
|
// Help text
|
||||||
|
b.WriteString(m.styles.FormHelp.Render("Tab/Shift+Tab: navigate • Ctrl+J/K: switch tabs"))
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(m.styles.FormHelp.Render("Enter on last field: submit • Ctrl+S: save • Ctrl+C/Esc: cancel"))
|
||||||
b.WriteString("\n")
|
b.WriteString("\n")
|
||||||
b.WriteString(m.styles.FormHelp.Render("* Required fields"))
|
b.WriteString(m.styles.FormHelp.Render("* Required fields"))
|
||||||
|
|
||||||
return b.String()
|
return b.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getMinimumHeight calculates the minimum height needed to display the form
|
||||||
|
func (m *addFormModel) getMinimumHeight() int {
|
||||||
|
// Title: 1 line + 2 newlines = 3
|
||||||
|
titleLines := 3
|
||||||
|
// Tabs: 1 line + 2 newlines = 3
|
||||||
|
tabLines := 3
|
||||||
|
// Fields in current tab
|
||||||
|
var fieldsCount int
|
||||||
|
if m.currentTab == tabGeneral {
|
||||||
|
fieldsCount = 7 // 7 fields in general tab
|
||||||
|
} else {
|
||||||
|
fieldsCount = 3 // 3 fields in advanced tab
|
||||||
|
}
|
||||||
|
// Each field: label (1) + input (1) + spacing (2) = 4 lines per field, but let's be more conservative
|
||||||
|
fieldsLines := fieldsCount * 3 // Reduced from 4 to 3
|
||||||
|
// Help text: 3 lines
|
||||||
|
helpLines := 3
|
||||||
|
// Error message space when needed: 2 lines
|
||||||
|
errorLines := 0 // Only count when there's actually an error
|
||||||
|
if m.err != "" {
|
||||||
|
errorLines = 2
|
||||||
|
}
|
||||||
|
|
||||||
|
return titleLines + tabLines + fieldsLines + helpLines + errorLines + 1 // +1 minimal safety margin
|
||||||
|
}
|
||||||
|
|
||||||
|
// isHeightSufficient checks if the current terminal height is sufficient
|
||||||
|
func (m *addFormModel) isHeightSufficient() bool {
|
||||||
|
return m.height >= m.getMinimumHeight()
|
||||||
|
}
|
||||||
|
|
||||||
|
// renderHeightWarning renders a warning message when height is insufficient
|
||||||
|
func (m *addFormModel) renderHeightWarning() string {
|
||||||
|
required := m.getMinimumHeight()
|
||||||
|
current := m.height
|
||||||
|
|
||||||
|
warning := m.styles.ErrorText.Render("⚠️ Terminal height is too small!")
|
||||||
|
details := m.styles.FormField.Render(fmt.Sprintf("Current: %d lines, Required: %d lines", current, required))
|
||||||
|
instruction := m.styles.FormHelp.Render("Please resize your terminal window and try again.")
|
||||||
|
instruction2 := m.styles.FormHelp.Render("Press Ctrl+C to cancel or resize terminal window.")
|
||||||
|
|
||||||
|
return warning + "\n\n" + details + "\n\n" + instruction + "\n" + instruction2
|
||||||
|
}
|
||||||
|
|
||||||
|
// renderTabs renders the tab headers
|
||||||
|
func (m *addFormModel) renderTabs() string {
|
||||||
|
var generalTab, advancedTab string
|
||||||
|
|
||||||
|
if m.currentTab == tabGeneral {
|
||||||
|
generalTab = m.styles.FocusedLabel.Render("[ General ]")
|
||||||
|
advancedTab = m.styles.FormField.Render(" Advanced ")
|
||||||
|
} else {
|
||||||
|
generalTab = m.styles.FormField.Render(" General ")
|
||||||
|
advancedTab = m.styles.FocusedLabel.Render("[ Advanced ]")
|
||||||
|
}
|
||||||
|
|
||||||
|
return generalTab + " " + advancedTab
|
||||||
|
}
|
||||||
|
|
||||||
|
// renderGeneralTab renders the general tab content
|
||||||
|
func (m *addFormModel) renderGeneralTab() string {
|
||||||
|
var b strings.Builder
|
||||||
|
|
||||||
|
fields := []struct {
|
||||||
|
index int
|
||||||
|
label string
|
||||||
|
}{
|
||||||
|
{nameInput, "Host Name *"},
|
||||||
|
{hostnameInput, "Hostname/IP *"},
|
||||||
|
{userInput, "User"},
|
||||||
|
{portInput, "Port"},
|
||||||
|
{identityInput, "Identity File"},
|
||||||
|
{proxyJumpInput, "ProxyJump"},
|
||||||
|
{tagsInput, "Tags (comma-separated)"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, field := range fields {
|
||||||
|
fieldStyle := m.styles.FormField
|
||||||
|
if m.focused == field.index {
|
||||||
|
fieldStyle = m.styles.FocusedLabel
|
||||||
|
}
|
||||||
|
b.WriteString(fieldStyle.Render(field.label))
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(m.inputs[field.index].View())
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// renderAdvancedTab renders the advanced tab content
|
||||||
|
func (m *addFormModel) renderAdvancedTab() string {
|
||||||
|
var b strings.Builder
|
||||||
|
|
||||||
|
fields := []struct {
|
||||||
|
index int
|
||||||
|
label string
|
||||||
|
}{
|
||||||
|
{optionsInput, "SSH Options"},
|
||||||
|
{remoteCommandInput, "Remote Command"},
|
||||||
|
{requestTTYInput, "Request TTY"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, field := range fields {
|
||||||
|
fieldStyle := m.styles.FormField
|
||||||
|
if m.focused == field.index {
|
||||||
|
fieldStyle = m.styles.FocusedLabel
|
||||||
|
}
|
||||||
|
b.WriteString(fieldStyle.Render(field.label))
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(m.inputs[field.index].View())
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
// Standalone wrapper for add form
|
// Standalone wrapper for add form
|
||||||
type standaloneAddForm struct {
|
type standaloneAddForm struct {
|
||||||
*addFormModel
|
*addFormModel
|
||||||
@ -291,6 +490,8 @@ func (m *addFormModel) submitForm() tea.Cmd {
|
|||||||
identity := strings.TrimSpace(m.inputs[identityInput].Value())
|
identity := strings.TrimSpace(m.inputs[identityInput].Value())
|
||||||
proxyJump := strings.TrimSpace(m.inputs[proxyJumpInput].Value())
|
proxyJump := strings.TrimSpace(m.inputs[proxyJumpInput].Value())
|
||||||
options := strings.TrimSpace(m.inputs[optionsInput].Value())
|
options := strings.TrimSpace(m.inputs[optionsInput].Value())
|
||||||
|
remoteCommand := strings.TrimSpace(m.inputs[remoteCommandInput].Value())
|
||||||
|
requestTTY := strings.TrimSpace(m.inputs[requestTTYInput].Value())
|
||||||
|
|
||||||
// Set defaults
|
// Set defaults
|
||||||
if user == "" {
|
if user == "" {
|
||||||
@ -319,14 +520,16 @@ func (m *addFormModel) submitForm() tea.Cmd {
|
|||||||
|
|
||||||
// Create host configuration
|
// Create host configuration
|
||||||
host := config.SSHHost{
|
host := config.SSHHost{
|
||||||
Name: name,
|
Name: name,
|
||||||
Hostname: hostname,
|
Hostname: hostname,
|
||||||
User: user,
|
User: user,
|
||||||
Port: port,
|
Port: port,
|
||||||
Identity: identity,
|
Identity: identity,
|
||||||
ProxyJump: proxyJump,
|
ProxyJump: proxyJump,
|
||||||
Options: config.ParseSSHOptionsFromCommand(options),
|
Options: config.ParseSSHOptionsFromCommand(options),
|
||||||
Tags: tags,
|
RemoteCommand: remoteCommand,
|
||||||
|
RequestTTY: requestTTY,
|
||||||
|
Tags: tags,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add to config
|
// Add to config
|
||||||
|
@ -9,7 +9,6 @@ import (
|
|||||||
|
|
||||||
"github.com/charmbracelet/bubbles/textinput"
|
"github.com/charmbracelet/bubbles/textinput"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -29,8 +28,8 @@ type editFormModel struct {
|
|||||||
inputs []textinput.Model
|
inputs []textinput.Model
|
||||||
focusArea int // 0=hosts, 1=properties
|
focusArea int // 0=hosts, 1=properties
|
||||||
focused int
|
focused int
|
||||||
|
currentTab int // 0=General, 1=Advanced (only applies when focusArea == focusAreaProperties)
|
||||||
err string
|
err string
|
||||||
success bool
|
|
||||||
styles Styles
|
styles Styles
|
||||||
originalName string
|
originalName string
|
||||||
originalHosts []string // Store original host names for multi-host detection
|
originalHosts []string // Store original host names for multi-host detection
|
||||||
@ -92,7 +91,7 @@ func NewEditForm(hostName string, styles Styles, width, height int, configFile s
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inputs := make([]textinput.Model, 7) // Reduced from 8 since we removed nameInput
|
inputs := make([]textinput.Model, 9) // Increased from 8 to 9 for RequestTTY
|
||||||
|
|
||||||
// Hostname input
|
// Hostname input
|
||||||
inputs[0] = textinput.New()
|
inputs[0] = textinput.New()
|
||||||
@ -147,11 +146,26 @@ func NewEditForm(hostName string, styles Styles, width, height int, configFile s
|
|||||||
inputs[6].SetValue(strings.Join(host.Tags, ", "))
|
inputs[6].SetValue(strings.Join(host.Tags, ", "))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remote Command input
|
||||||
|
inputs[7] = textinput.New()
|
||||||
|
inputs[7].Placeholder = "ls -la, htop, bash"
|
||||||
|
inputs[7].CharLimit = 300
|
||||||
|
inputs[7].Width = 70
|
||||||
|
inputs[7].SetValue(host.RemoteCommand)
|
||||||
|
|
||||||
|
// RequestTTY input
|
||||||
|
inputs[8] = textinput.New()
|
||||||
|
inputs[8].Placeholder = "yes, no, force, auto"
|
||||||
|
inputs[8].CharLimit = 10
|
||||||
|
inputs[8].Width = 30
|
||||||
|
inputs[8].SetValue(host.RequestTTY)
|
||||||
|
|
||||||
return &editFormModel{
|
return &editFormModel{
|
||||||
hostInputs: hostInputs,
|
hostInputs: hostInputs,
|
||||||
inputs: inputs,
|
inputs: inputs,
|
||||||
focusArea: focusAreaHosts, // Start with hosts focused for multi-host editing
|
focusArea: focusAreaHosts, // Start with hosts focused for multi-host editing
|
||||||
focused: 0,
|
focused: 0,
|
||||||
|
currentTab: 0, // Start on General tab
|
||||||
originalName: hostName,
|
originalName: hostName,
|
||||||
originalHosts: hostNames,
|
originalHosts: hostNames,
|
||||||
host: host,
|
host: host,
|
||||||
@ -235,6 +249,157 @@ func (m *editFormModel) updateFocus() tea.Cmd {
|
|||||||
return textinput.Blink
|
return textinput.Blink
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getPropertiesForCurrentTab returns the property input indices for the current tab
|
||||||
|
func (m *editFormModel) getPropertiesForCurrentTab() []int {
|
||||||
|
switch m.currentTab {
|
||||||
|
case 0: // General
|
||||||
|
return []int{0, 1, 2, 3, 4, 6} // hostname, user, port, identity, proxyjump, tags
|
||||||
|
case 1: // Advanced
|
||||||
|
return []int{5, 7, 8} // options, remotecommand, requesttty
|
||||||
|
default:
|
||||||
|
return []int{0, 1, 2, 3, 4, 6}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// getFirstPropertyForTab returns the first property index for a given tab
|
||||||
|
func (m *editFormModel) getFirstPropertyForTab(tab int) int {
|
||||||
|
properties := []int{0, 1, 2, 3, 4, 6} // General tab
|
||||||
|
if tab == 1 {
|
||||||
|
properties = []int{5, 7, 8} // Advanced tab
|
||||||
|
}
|
||||||
|
if len(properties) > 0 {
|
||||||
|
return properties[0]
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleEditNavigation handles navigation in the edit form with tab support
|
||||||
|
func (m *editFormModel) handleEditNavigation(key string) tea.Cmd {
|
||||||
|
if m.focusArea == focusAreaHosts {
|
||||||
|
// Navigate in hosts area
|
||||||
|
if key == "up" || key == "shift+tab" {
|
||||||
|
m.focused--
|
||||||
|
} else {
|
||||||
|
m.focused++
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.focused >= len(m.hostInputs) {
|
||||||
|
// Move to properties area, keep current tab
|
||||||
|
m.focusArea = focusAreaProperties
|
||||||
|
// Keep the current tab instead of forcing it to 0
|
||||||
|
m.focused = m.getFirstPropertyForTab(m.currentTab)
|
||||||
|
} else if m.focused < 0 {
|
||||||
|
m.focused = len(m.hostInputs) - 1
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Navigate in properties area within current tab
|
||||||
|
currentTabProperties := m.getPropertiesForCurrentTab()
|
||||||
|
|
||||||
|
// Find current position within the tab
|
||||||
|
currentPos := 0
|
||||||
|
for i, prop := range currentTabProperties {
|
||||||
|
if prop == m.focused {
|
||||||
|
currentPos = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle form submission on last field of Advanced tab
|
||||||
|
if key == "enter" && m.currentTab == 1 && currentPos == len(currentTabProperties)-1 {
|
||||||
|
return m.submitEditForm()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Navigate within current tab
|
||||||
|
if key == "up" || key == "shift+tab" {
|
||||||
|
currentPos--
|
||||||
|
} else {
|
||||||
|
currentPos++
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle transitions between areas and tabs
|
||||||
|
if currentPos >= len(currentTabProperties) {
|
||||||
|
// Move to next area/tab
|
||||||
|
if m.currentTab == 0 {
|
||||||
|
// Move to advanced tab
|
||||||
|
m.currentTab = 1
|
||||||
|
m.focused = m.getFirstPropertyForTab(1)
|
||||||
|
} else {
|
||||||
|
// Move back to hosts area
|
||||||
|
m.focusArea = focusAreaHosts
|
||||||
|
m.focused = 0
|
||||||
|
}
|
||||||
|
} else if currentPos < 0 {
|
||||||
|
// Move to previous area/tab
|
||||||
|
if m.currentTab == 1 {
|
||||||
|
// Move to general tab
|
||||||
|
m.currentTab = 0
|
||||||
|
properties := m.getPropertiesForCurrentTab()
|
||||||
|
m.focused = properties[len(properties)-1]
|
||||||
|
} else {
|
||||||
|
// Move to hosts area
|
||||||
|
m.focusArea = focusAreaHosts
|
||||||
|
m.focused = len(m.hostInputs) - 1
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
m.focused = currentTabProperties[currentPos]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.updateFocus()
|
||||||
|
}
|
||||||
|
|
||||||
|
// getMinimumHeight calculates the minimum height needed to display the edit form
|
||||||
|
func (m *editFormModel) getMinimumHeight() int {
|
||||||
|
// Title: 1 line + 2 newlines = 3
|
||||||
|
titleLines := 3
|
||||||
|
// Config file info: 1 line + 2 newlines = 3
|
||||||
|
configLines := 3
|
||||||
|
// Host Names section: title (1) + spacing (2) = 3
|
||||||
|
hostSectionLines := 3
|
||||||
|
// Host inputs: number of hosts * 3 lines each (reduced from 4)
|
||||||
|
hostLines := len(m.hostInputs) * 3
|
||||||
|
// Properties section: title (1) + spacing (2) = 3
|
||||||
|
propertiesSectionLines := 3
|
||||||
|
// Tabs: 1 line + 2 newlines = 3
|
||||||
|
tabLines := 3
|
||||||
|
// Fields in current tab
|
||||||
|
var fieldsCount int
|
||||||
|
if m.currentTab == 0 {
|
||||||
|
fieldsCount = 6 // 6 fields in general tab
|
||||||
|
} else {
|
||||||
|
fieldsCount = 3 // 3 fields in advanced tab
|
||||||
|
}
|
||||||
|
// Each field: reduced from 4 to 3 lines per field
|
||||||
|
fieldsLines := fieldsCount * 3
|
||||||
|
// Help text: 3 lines
|
||||||
|
helpLines := 3
|
||||||
|
// Error message space when needed: 2 lines
|
||||||
|
errorLines := 0 // Only count when there's actually an error
|
||||||
|
if m.err != "" {
|
||||||
|
errorLines = 2
|
||||||
|
}
|
||||||
|
|
||||||
|
return titleLines + configLines + hostSectionLines + hostLines + propertiesSectionLines + tabLines + fieldsLines + helpLines + errorLines + 1 // +1 minimal safety margin
|
||||||
|
}
|
||||||
|
|
||||||
|
// isHeightSufficient checks if the current terminal height is sufficient
|
||||||
|
func (m *editFormModel) isHeightSufficient() bool {
|
||||||
|
return m.height >= m.getMinimumHeight()
|
||||||
|
}
|
||||||
|
|
||||||
|
// renderHeightWarning renders a warning message when height is insufficient
|
||||||
|
func (m *editFormModel) renderHeightWarning() string {
|
||||||
|
required := m.getMinimumHeight()
|
||||||
|
current := m.height
|
||||||
|
|
||||||
|
warning := m.styles.ErrorText.Render("⚠️ Terminal height is too small!")
|
||||||
|
details := m.styles.FormField.Render(fmt.Sprintf("Current: %d lines, Required: %d lines", current, required))
|
||||||
|
instruction := m.styles.FormHelp.Render("Please resize your terminal window and try again.")
|
||||||
|
instruction2 := m.styles.FormHelp.Render("Press Ctrl+C to cancel or resize terminal window.")
|
||||||
|
|
||||||
|
return warning + "\n\n" + details + "\n\n" + instruction + "\n" + instruction2
|
||||||
|
}
|
||||||
|
|
||||||
func (m *editFormModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
func (m *editFormModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
var cmds []tea.Cmd
|
var cmds []tea.Cmd
|
||||||
|
|
||||||
@ -247,51 +412,33 @@ func (m *editFormModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
case "ctrl+c", "esc":
|
case "ctrl+c", "esc":
|
||||||
m.err = ""
|
m.err = ""
|
||||||
m.success = false
|
|
||||||
return m, func() tea.Msg { return editFormCancelMsg{} }
|
return m, func() tea.Msg { return editFormCancelMsg{} }
|
||||||
|
|
||||||
case "ctrl+s":
|
case "ctrl+s":
|
||||||
// Allow submission from any field with Ctrl+S (Save)
|
// Allow submission from any field with Ctrl+S (Save)
|
||||||
return m, m.submitEditForm()
|
return m, m.submitEditForm()
|
||||||
|
|
||||||
case "tab", "shift+tab", "enter", "up", "down":
|
case "ctrl+j":
|
||||||
s := msg.String()
|
// Switch to next tab
|
||||||
|
m.currentTab = (m.currentTab + 1) % 2
|
||||||
// Handle form submission
|
// If we're in hosts area, stay there. If in properties, go to the first field of the new tab
|
||||||
totalFields := len(m.hostInputs) + len(m.inputs)
|
|
||||||
currentGlobalIndex := m.focused
|
|
||||||
if m.focusArea == focusAreaProperties {
|
if m.focusArea == focusAreaProperties {
|
||||||
currentGlobalIndex = len(m.hostInputs) + m.focused
|
m.focused = m.getFirstPropertyForTab(m.currentTab)
|
||||||
}
|
}
|
||||||
|
|
||||||
if s == "enter" && currentGlobalIndex == totalFields-1 {
|
|
||||||
return m, m.submitEditForm()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cycle inputs
|
|
||||||
if s == "up" || s == "shift+tab" {
|
|
||||||
currentGlobalIndex--
|
|
||||||
} else {
|
|
||||||
currentGlobalIndex++
|
|
||||||
}
|
|
||||||
|
|
||||||
if currentGlobalIndex >= totalFields {
|
|
||||||
currentGlobalIndex = 0
|
|
||||||
} else if currentGlobalIndex < 0 {
|
|
||||||
currentGlobalIndex = totalFields - 1
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update focus area and focused index based on global index
|
|
||||||
if currentGlobalIndex < len(m.hostInputs) {
|
|
||||||
m.focusArea = focusAreaHosts
|
|
||||||
m.focused = currentGlobalIndex
|
|
||||||
} else {
|
|
||||||
m.focusArea = focusAreaProperties
|
|
||||||
m.focused = currentGlobalIndex - len(m.hostInputs)
|
|
||||||
}
|
|
||||||
|
|
||||||
return m, m.updateFocus()
|
return m, m.updateFocus()
|
||||||
|
|
||||||
|
case "ctrl+k":
|
||||||
|
// Switch to previous tab
|
||||||
|
m.currentTab = (m.currentTab - 1 + 2) % 2
|
||||||
|
// If we're in hosts area, stay there. If in properties, go to the first field of the new tab
|
||||||
|
if m.focusArea == focusAreaProperties {
|
||||||
|
m.focused = m.getFirstPropertyForTab(m.currentTab)
|
||||||
|
}
|
||||||
|
return m, m.updateFocus()
|
||||||
|
|
||||||
|
case "tab", "shift+tab", "enter", "up", "down":
|
||||||
|
return m, m.handleEditNavigation(msg.String())
|
||||||
|
|
||||||
case "ctrl+a":
|
case "ctrl+a":
|
||||||
// Add a new host input
|
// Add a new host input
|
||||||
return m, m.addHostInput()
|
return m, m.addHostInput()
|
||||||
@ -306,10 +453,10 @@ func (m *editFormModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
case editFormSubmitMsg:
|
case editFormSubmitMsg:
|
||||||
if msg.err != nil {
|
if msg.err != nil {
|
||||||
m.err = msg.err.Error()
|
m.err = msg.err.Error()
|
||||||
m.success = false
|
|
||||||
} else {
|
} else {
|
||||||
m.success = true
|
// Success: let the wrapper handle this
|
||||||
m.err = ""
|
// In TUI mode, this will be handled by the parent
|
||||||
|
// In standalone mode, the wrapper will quit
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
@ -332,15 +479,13 @@ func (m *editFormModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *editFormModel) View() string {
|
func (m *editFormModel) View() string {
|
||||||
var b strings.Builder
|
// Check if terminal height is sufficient
|
||||||
|
if !m.isHeightSufficient() {
|
||||||
if m.success {
|
return m.renderHeightWarning()
|
||||||
b.WriteString(m.styles.FormField.Foreground(lipgloss.Color("#10B981")).Render("✓ Host updated successfully!"))
|
|
||||||
b.WriteString("\n\n")
|
|
||||||
b.WriteString(m.styles.FormHelp.Render("Press Ctrl+C or Esc to go back"))
|
|
||||||
return b.String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var b strings.Builder
|
||||||
|
|
||||||
if m.err != "" {
|
if m.err != "" {
|
||||||
b.WriteString(m.styles.Error.Render("Error: " + m.err))
|
b.WriteString(m.styles.Error.Render("Error: " + m.err))
|
||||||
b.WriteString("\n\n")
|
b.WriteString("\n\n")
|
||||||
@ -377,25 +522,16 @@ func (m *editFormModel) View() string {
|
|||||||
b.WriteString(m.styles.FormTitle.Render("Common Properties"))
|
b.WriteString(m.styles.FormTitle.Render("Common Properties"))
|
||||||
b.WriteString("\n\n")
|
b.WriteString("\n\n")
|
||||||
|
|
||||||
fields := []string{
|
// Render tabs for properties
|
||||||
"Hostname/IP *",
|
b.WriteString(m.renderEditTabs())
|
||||||
"User",
|
b.WriteString("\n\n")
|
||||||
"Port",
|
|
||||||
"Identity File",
|
|
||||||
"Proxy Jump",
|
|
||||||
"SSH Options",
|
|
||||||
"Tags (comma-separated)",
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, field := range fields {
|
// Render current tab content
|
||||||
fieldStyle := m.styles.FormField
|
switch m.currentTab {
|
||||||
if m.focusArea == focusAreaProperties && m.focused == i {
|
case 0: // General
|
||||||
fieldStyle = m.styles.FocusedLabel
|
b.WriteString(m.renderEditGeneralTab())
|
||||||
}
|
case 1: // Advanced
|
||||||
b.WriteString(fieldStyle.Render(field))
|
b.WriteString(m.renderEditAdvancedTab())
|
||||||
b.WriteString("\n")
|
|
||||||
b.WriteString(m.inputs[i].View())
|
|
||||||
b.WriteString("\n\n")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.err != "" {
|
if m.err != "" {
|
||||||
@ -405,10 +541,10 @@ func (m *editFormModel) View() string {
|
|||||||
|
|
||||||
// Show different help based on number of hosts
|
// Show different help based on number of hosts
|
||||||
if len(m.hostInputs) > 1 {
|
if len(m.hostInputs) > 1 {
|
||||||
b.WriteString(m.styles.FormHelp.Render("Tab/↑↓/Enter: navigate • Ctrl+A: add host • Ctrl+D: delete host"))
|
b.WriteString(m.styles.FormHelp.Render("Tab/↑↓/Enter: navigate • Ctrl+J/K: switch tabs • Ctrl+A: add host • Ctrl+D: delete host"))
|
||||||
b.WriteString("\n")
|
b.WriteString("\n")
|
||||||
} else {
|
} else {
|
||||||
b.WriteString(m.styles.FormHelp.Render("Tab/↑↓/Enter: navigate • Ctrl+A: add host"))
|
b.WriteString(m.styles.FormHelp.Render("Tab/↑↓/Enter: navigate • Ctrl+J/K: switch tabs • Ctrl+A: add host"))
|
||||||
b.WriteString("\n")
|
b.WriteString("\n")
|
||||||
}
|
}
|
||||||
b.WriteString(m.styles.FormHelp.Render("Ctrl+S: save • Ctrl+C/Esc: cancel • * Required fields"))
|
b.WriteString(m.styles.FormHelp.Render("Ctrl+S: save • Ctrl+C/Esc: cancel • * Required fields"))
|
||||||
@ -416,6 +552,102 @@ func (m *editFormModel) View() string {
|
|||||||
return b.String()
|
return b.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// renderEditTabs renders the tab headers for properties
|
||||||
|
func (m *editFormModel) renderEditTabs() string {
|
||||||
|
var generalTab, advancedTab string
|
||||||
|
|
||||||
|
if m.currentTab == 0 {
|
||||||
|
generalTab = m.styles.FocusedLabel.Render("[ General ]")
|
||||||
|
advancedTab = m.styles.FormField.Render(" Advanced ")
|
||||||
|
} else {
|
||||||
|
generalTab = m.styles.FormField.Render(" General ")
|
||||||
|
advancedTab = m.styles.FocusedLabel.Render("[ Advanced ]")
|
||||||
|
}
|
||||||
|
|
||||||
|
return generalTab + " " + advancedTab
|
||||||
|
}
|
||||||
|
|
||||||
|
// renderEditGeneralTab renders the general tab content for properties
|
||||||
|
func (m *editFormModel) renderEditGeneralTab() string {
|
||||||
|
var b strings.Builder
|
||||||
|
|
||||||
|
fields := []struct {
|
||||||
|
index int
|
||||||
|
label string
|
||||||
|
}{
|
||||||
|
{0, "Hostname/IP *"},
|
||||||
|
{1, "User"},
|
||||||
|
{2, "Port"},
|
||||||
|
{3, "Identity File"},
|
||||||
|
{4, "Proxy Jump"},
|
||||||
|
{6, "Tags (comma-separated)"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, field := range fields {
|
||||||
|
fieldStyle := m.styles.FormField
|
||||||
|
if m.focusArea == focusAreaProperties && m.focused == field.index {
|
||||||
|
fieldStyle = m.styles.FocusedLabel
|
||||||
|
}
|
||||||
|
b.WriteString(fieldStyle.Render(field.label))
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(m.inputs[field.index].View())
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// renderEditAdvancedTab renders the advanced tab content for properties
|
||||||
|
func (m *editFormModel) renderEditAdvancedTab() string {
|
||||||
|
var b strings.Builder
|
||||||
|
|
||||||
|
fields := []struct {
|
||||||
|
index int
|
||||||
|
label string
|
||||||
|
}{
|
||||||
|
{5, "SSH Options"},
|
||||||
|
{7, "Remote Command"},
|
||||||
|
{8, "Request TTY"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, field := range fields {
|
||||||
|
fieldStyle := m.styles.FormField
|
||||||
|
if m.focusArea == focusAreaProperties && m.focused == field.index {
|
||||||
|
fieldStyle = m.styles.FocusedLabel
|
||||||
|
}
|
||||||
|
b.WriteString(fieldStyle.Render(field.label))
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(m.inputs[field.index].View())
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Standalone wrapper for edit form
|
||||||
|
type standaloneEditForm struct {
|
||||||
|
*editFormModel
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m standaloneEditForm) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
|
switch msg := msg.(type) {
|
||||||
|
case editFormSubmitMsg:
|
||||||
|
if msg.err != nil {
|
||||||
|
m.editFormModel.err = msg.err.Error()
|
||||||
|
return m, nil
|
||||||
|
} else {
|
||||||
|
// Success: quit the program
|
||||||
|
return m, tea.Quit
|
||||||
|
}
|
||||||
|
case editFormCancelMsg:
|
||||||
|
return m, tea.Quit
|
||||||
|
}
|
||||||
|
|
||||||
|
newForm, cmd := m.editFormModel.Update(msg)
|
||||||
|
m.editFormModel = newForm.(*editFormModel)
|
||||||
|
return m, cmd
|
||||||
|
}
|
||||||
|
|
||||||
// RunEditForm runs the edit form as a standalone program
|
// RunEditForm runs the edit form as a standalone program
|
||||||
func RunEditForm(hostName string, configFile string) error {
|
func RunEditForm(hostName string, configFile string) error {
|
||||||
styles := NewStyles(80) // Default width
|
styles := NewStyles(80) // Default width
|
||||||
@ -424,17 +656,10 @@ func RunEditForm(hostName string, configFile string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
p := tea.NewProgram(editForm, tea.WithAltScreen())
|
m := standaloneEditForm{editForm}
|
||||||
|
p := tea.NewProgram(m, tea.WithAltScreen())
|
||||||
_, err = p.Run()
|
_, err = p.Run()
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if editForm.err != "" {
|
|
||||||
return fmt.Errorf(editForm.err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *editFormModel) submitEditForm() tea.Cmd {
|
func (m *editFormModel) submitEditForm() tea.Cmd {
|
||||||
@ -453,12 +678,14 @@ func (m *editFormModel) submitEditForm() tea.Cmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get property values using direct indices
|
// Get property values using direct indices
|
||||||
hostname := strings.TrimSpace(m.inputs[0].Value()) // hostnameInput
|
hostname := strings.TrimSpace(m.inputs[0].Value()) // hostnameInput
|
||||||
user := strings.TrimSpace(m.inputs[1].Value()) // userInput
|
user := strings.TrimSpace(m.inputs[1].Value()) // userInput
|
||||||
port := strings.TrimSpace(m.inputs[2].Value()) // portInput
|
port := strings.TrimSpace(m.inputs[2].Value()) // portInput
|
||||||
identity := strings.TrimSpace(m.inputs[3].Value()) // identityInput
|
identity := strings.TrimSpace(m.inputs[3].Value()) // identityInput
|
||||||
proxyJump := strings.TrimSpace(m.inputs[4].Value()) // proxyJumpInput
|
proxyJump := strings.TrimSpace(m.inputs[4].Value()) // proxyJumpInput
|
||||||
options := strings.TrimSpace(m.inputs[5].Value()) // optionsInput
|
options := strings.TrimSpace(m.inputs[5].Value()) // optionsInput
|
||||||
|
remoteCommand := strings.TrimSpace(m.inputs[7].Value()) // remoteCommandInput
|
||||||
|
requestTTY := strings.TrimSpace(m.inputs[8].Value()) // requestTTYInput
|
||||||
|
|
||||||
// Set defaults
|
// Set defaults
|
||||||
if port == "" {
|
if port == "" {
|
||||||
@ -491,13 +718,15 @@ func (m *editFormModel) submitEditForm() tea.Cmd {
|
|||||||
|
|
||||||
// Create the common host configuration
|
// Create the common host configuration
|
||||||
commonHost := config.SSHHost{
|
commonHost := config.SSHHost{
|
||||||
Hostname: hostname,
|
Hostname: hostname,
|
||||||
User: user,
|
User: user,
|
||||||
Port: port,
|
Port: port,
|
||||||
Identity: identity,
|
Identity: identity,
|
||||||
ProxyJump: proxyJump,
|
ProxyJump: proxyJump,
|
||||||
Options: options,
|
Options: options,
|
||||||
Tags: tags,
|
RemoteCommand: remoteCommand,
|
||||||
|
RequestTTY: requestTTY,
|
||||||
|
Tags: tags,
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
@ -33,7 +33,8 @@ type Styles struct {
|
|||||||
HelpText lipgloss.Style
|
HelpText lipgloss.Style
|
||||||
|
|
||||||
// Error and confirmation styles
|
// Error and confirmation styles
|
||||||
Error lipgloss.Style
|
Error lipgloss.Style
|
||||||
|
ErrorText lipgloss.Style
|
||||||
|
|
||||||
// Form styles (for add/edit forms)
|
// Form styles (for add/edit forms)
|
||||||
FormTitle lipgloss.Style
|
FormTitle lipgloss.Style
|
||||||
@ -97,6 +98,11 @@ func NewStyles(width int) Styles {
|
|||||||
BorderForeground(lipgloss.Color(ErrorColor)).
|
BorderForeground(lipgloss.Color(ErrorColor)).
|
||||||
Padding(1, 2),
|
Padding(1, 2),
|
||||||
|
|
||||||
|
// Error text style (no border, just red text)
|
||||||
|
ErrorText: lipgloss.NewStyle().
|
||||||
|
Foreground(lipgloss.Color(ErrorColor)).
|
||||||
|
Bold(true),
|
||||||
|
|
||||||
// Form styles
|
// Form styles
|
||||||
FormTitle: lipgloss.NewStyle().
|
FormTitle: lipgloss.NewStyle().
|
||||||
Foreground(lipgloss.Color("#FFFDF5")).
|
Foreground(lipgloss.Color("#FFFDF5")).
|
||||||
|
Loading…
x
Reference in New Issue
Block a user