mirror of
https://github.com/Gu1llaum-3/sshm.git
synced 2025-10-21 18:37:23 +02:00
Compare commits
1 Commits
main
...
v1.9.0-bet
Author | SHA1 | Date | |
---|---|---|---|
2e84ac002e |
@ -138,12 +138,6 @@ release:
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
📖 **Documentation:** See the updated [README](https://github.com/Gu1llaum-3/sshm/blob/main/README.md)
|
|
||||||
|
|
||||||
🐛 **Issues:** Found a bug? Open an [issue](https://github.com/Gu1llaum-3/sshm/issues)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
Released with ❤️ by [GoReleaser](https://github.com/goreleaser/goreleaser)
|
Released with ❤️ by [GoReleaser](https://github.com/goreleaser/goreleaser)
|
||||||
|
|
||||||
# Snapshot builds (for non-tag builds)
|
# Snapshot builds (for non-tag builds)
|
||||||
|
12
cmd/root.go
12
cmd/root.go
@ -140,17 +140,11 @@ 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 != "" {
|
||||||
args = append(args, "-F", configFile)
|
sshCmd = exec.Command("ssh", "-F", configFile, hostName)
|
||||||
|
} 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,17 +13,15 @@ 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
|
||||||
RemoteCommand string // Command to execute after SSH connection
|
Tags []string
|
||||||
RequestTTY string // Request TTY (yes, no, force, auto)
|
SourceFile string // Path to the config file where this host is defined
|
||||||
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
|
||||||
@ -328,14 +326,6 @@ 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) != "" {
|
||||||
@ -613,20 +603,6 @@ 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
|
||||||
@ -1044,12 +1020,6 @@ 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")
|
||||||
@ -1098,12 +1068,6 @@ 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")
|
||||||
@ -1188,12 +1152,6 @@ 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")
|
||||||
@ -1242,12 +1200,6 @@ 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")
|
||||||
@ -1742,12 +1694,6 @@ 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 != "" {
|
||||||
@ -1828,12 +1774,6 @@ 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,7 +1,6 @@
|
|||||||
package ui
|
package ui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -17,7 +16,6 @@ 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
|
||||||
@ -49,7 +47,7 @@ func NewAddForm(hostname string, styles Styles, width, height int, configFile st
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inputs := make([]textinput.Model, 10) // Increased from 9 to 10 for RequestTTY
|
inputs := make([]textinput.Model, 8)
|
||||||
|
|
||||||
// Name input
|
// Name input
|
||||||
inputs[nameInput] = textinput.New()
|
inputs[nameInput] = textinput.New()
|
||||||
@ -103,22 +101,9 @@ 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,
|
||||||
@ -126,11 +111,6 @@ func NewAddForm(hostname string, styles Styles, width, height int, configFile st
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
tabGeneral = iota
|
|
||||||
tabAdvanced
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
nameInput = iota
|
nameInput = iota
|
||||||
hostnameInput
|
hostnameInput
|
||||||
@ -138,11 +118,8 @@ const (
|
|||||||
portInput
|
portInput
|
||||||
identityInput
|
identityInput
|
||||||
proxyJumpInput
|
proxyJumpInput
|
||||||
tagsInput
|
|
||||||
// Advanced tab inputs
|
|
||||||
optionsInput
|
optionsInput
|
||||||
remoteCommandInput
|
tagsInput
|
||||||
requestTTYInput
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Messages for communication with parent model
|
// Messages for communication with parent model
|
||||||
@ -176,20 +153,36 @@ 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":
|
||||||
return m, m.handleNavigation(msg.String())
|
s := 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:
|
||||||
@ -213,104 +206,32 @@ 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")
|
||||||
|
|
||||||
// Render tabs
|
fields := []string{
|
||||||
b.WriteString(m.renderTabs())
|
"Host Name *",
|
||||||
b.WriteString("\n\n")
|
"Hostname/IP *",
|
||||||
|
"User",
|
||||||
|
"Port",
|
||||||
|
"Identity File",
|
||||||
|
"ProxyJump",
|
||||||
|
"SSH Options",
|
||||||
|
"Tags (comma-separated)",
|
||||||
|
}
|
||||||
|
|
||||||
// Render current tab content
|
for i, field := range fields {
|
||||||
switch m.currentTab {
|
b.WriteString(m.styles.FormField.Render(field))
|
||||||
case tabGeneral:
|
b.WriteString("\n")
|
||||||
b.WriteString(m.renderGeneralTab())
|
b.WriteString(m.inputs[i].View())
|
||||||
case tabAdvanced:
|
b.WriteString("\n\n")
|
||||||
b.WriteString(m.renderAdvancedTab())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.err != "" {
|
if m.err != "" {
|
||||||
@ -318,133 +239,13 @@ func (m *addFormModel) View() string {
|
|||||||
b.WriteString("\n\n")
|
b.WriteString("\n\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Help text
|
b.WriteString(m.styles.FormHelp.Render("Tab/Shift+Tab: navigate • Enter on last field: submit • Ctrl+S: save • Ctrl+C/Esc: cancel"))
|
||||||
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
|
||||||
@ -490,8 +291,6 @@ 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 == "" {
|
||||||
@ -520,16 +319,14 @@ 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),
|
||||||
RemoteCommand: remoteCommand,
|
Tags: tags,
|
||||||
RequestTTY: requestTTY,
|
|
||||||
Tags: tags,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add to config
|
// Add to config
|
||||||
|
@ -9,6 +9,7 @@ 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 (
|
||||||
@ -28,8 +29,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
|
||||||
@ -91,7 +92,7 @@ func NewEditForm(hostName string, styles Styles, width, height int, configFile s
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inputs := make([]textinput.Model, 9) // Increased from 8 to 9 for RequestTTY
|
inputs := make([]textinput.Model, 7) // Reduced from 8 since we removed nameInput
|
||||||
|
|
||||||
// Hostname input
|
// Hostname input
|
||||||
inputs[0] = textinput.New()
|
inputs[0] = textinput.New()
|
||||||
@ -146,26 +147,11 @@ 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,
|
||||||
@ -249,157 +235,6 @@ 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
|
||||||
|
|
||||||
@ -412,32 +247,50 @@ 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 "ctrl+j":
|
|
||||||
// Switch to next tab
|
|
||||||
m.currentTab = (m.currentTab + 1) % 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 "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":
|
case "tab", "shift+tab", "enter", "up", "down":
|
||||||
return m, m.handleEditNavigation(msg.String())
|
s := msg.String()
|
||||||
|
|
||||||
|
// Handle form submission
|
||||||
|
totalFields := len(m.hostInputs) + len(m.inputs)
|
||||||
|
currentGlobalIndex := m.focused
|
||||||
|
if m.focusArea == focusAreaProperties {
|
||||||
|
currentGlobalIndex = len(m.hostInputs) + m.focused
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
case "ctrl+a":
|
case "ctrl+a":
|
||||||
// Add a new host input
|
// Add a new host input
|
||||||
@ -453,10 +306,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 {
|
||||||
// Success: let the wrapper handle this
|
m.success = true
|
||||||
// In TUI mode, this will be handled by the parent
|
m.err = ""
|
||||||
// In standalone mode, the wrapper will quit
|
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
@ -479,13 +332,15 @@ func (m *editFormModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *editFormModel) View() string {
|
func (m *editFormModel) View() string {
|
||||||
// Check if terminal height is sufficient
|
|
||||||
if !m.isHeightSufficient() {
|
|
||||||
return m.renderHeightWarning()
|
|
||||||
}
|
|
||||||
|
|
||||||
var b strings.Builder
|
var b strings.Builder
|
||||||
|
|
||||||
|
if m.success {
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
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")
|
||||||
@ -522,16 +377,25 @@ 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")
|
||||||
|
|
||||||
// Render tabs for properties
|
fields := []string{
|
||||||
b.WriteString(m.renderEditTabs())
|
"Hostname/IP *",
|
||||||
b.WriteString("\n\n")
|
"User",
|
||||||
|
"Port",
|
||||||
|
"Identity File",
|
||||||
|
"Proxy Jump",
|
||||||
|
"SSH Options",
|
||||||
|
"Tags (comma-separated)",
|
||||||
|
}
|
||||||
|
|
||||||
// Render current tab content
|
for i, field := range fields {
|
||||||
switch m.currentTab {
|
fieldStyle := m.styles.FormField
|
||||||
case 0: // General
|
if m.focusArea == focusAreaProperties && m.focused == i {
|
||||||
b.WriteString(m.renderEditGeneralTab())
|
fieldStyle = m.styles.FocusedLabel
|
||||||
case 1: // Advanced
|
}
|
||||||
b.WriteString(m.renderEditAdvancedTab())
|
b.WriteString(fieldStyle.Render(field))
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(m.inputs[i].View())
|
||||||
|
b.WriteString("\n\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.err != "" {
|
if m.err != "" {
|
||||||
@ -541,10 +405,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+J/K: switch tabs • Ctrl+A: add host • Ctrl+D: delete host"))
|
b.WriteString(m.styles.FormHelp.Render("Tab/↑↓/Enter: navigate • 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+J/K: switch tabs • Ctrl+A: add host"))
|
b.WriteString(m.styles.FormHelp.Render("Tab/↑↓/Enter: navigate • 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"))
|
||||||
@ -552,102 +416,6 @@ 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
|
||||||
@ -656,10 +424,17 @@ func RunEditForm(hostName string, configFile string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
m := standaloneEditForm{editForm}
|
p := tea.NewProgram(editForm, tea.WithAltScreen())
|
||||||
p := tea.NewProgram(m, tea.WithAltScreen())
|
|
||||||
_, err = p.Run()
|
_, err = p.Run()
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if editForm.err != "" {
|
||||||
|
return fmt.Errorf(editForm.err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *editFormModel) submitEditForm() tea.Cmd {
|
func (m *editFormModel) submitEditForm() tea.Cmd {
|
||||||
@ -678,14 +453,12 @@ 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 == "" {
|
||||||
@ -718,15 +491,13 @@ 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,
|
||||||
RemoteCommand: remoteCommand,
|
Tags: tags,
|
||||||
RequestTTY: requestTTY,
|
|
||||||
Tags: tags,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
@ -33,8 +33,7 @@ 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
|
||||||
@ -98,11 +97,6 @@ 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