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)
|
||||
|
||||
var sshCmd *exec.Cmd
|
||||
var args []string
|
||||
|
||||
if configFile != "" {
|
||||
sshCmd = exec.Command("ssh", "-F", configFile, hostName)
|
||||
} else {
|
||||
sshCmd = exec.Command("ssh", hostName)
|
||||
args = append(args, "-F", configFile)
|
||||
}
|
||||
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
|
||||
sshCmd.Stdin = os.Stdin
|
||||
|
@ -13,15 +13,17 @@ import (
|
||||
|
||||
// SSHHost represents an SSH host configuration
|
||||
type SSHHost struct {
|
||||
Name string
|
||||
Hostname string
|
||||
User string
|
||||
Port string
|
||||
Identity string
|
||||
ProxyJump string
|
||||
Options string
|
||||
Tags []string
|
||||
SourceFile string // Path to the config file where this host is defined
|
||||
Name string
|
||||
Hostname string
|
||||
User string
|
||||
Port string
|
||||
Identity string
|
||||
ProxyJump string
|
||||
Options string
|
||||
RemoteCommand string // Command to execute after SSH connection
|
||||
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
|
||||
aliasNames []string `json:"-"` // Do not serialize this field
|
||||
@ -326,6 +328,14 @@ func parseSSHConfigFileWithProcessedFiles(configPath string, processedFiles map[
|
||||
if currentHost != nil {
|
||||
currentHost.ProxyJump = value
|
||||
}
|
||||
case "remotecommand":
|
||||
if currentHost != nil {
|
||||
currentHost.RemoteCommand = value
|
||||
}
|
||||
case "requesttty":
|
||||
if currentHost != nil {
|
||||
currentHost.RequestTTY = value
|
||||
}
|
||||
default:
|
||||
// Handle other SSH options
|
||||
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
|
||||
if host.Options != "" {
|
||||
// Split options by newlines and write each one
|
||||
@ -1020,6 +1044,12 @@ func UpdateSSHHostInFile(oldName string, newHost SSHHost, configPath string) err
|
||||
if 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
|
||||
if newHost.Options != "" {
|
||||
options := strings.Split(newHost.Options, "\n")
|
||||
@ -1068,6 +1098,12 @@ func UpdateSSHHostInFile(oldName string, newHost SSHHost, configPath string) err
|
||||
if 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
|
||||
if newHost.Options != "" {
|
||||
options := strings.Split(newHost.Options, "\n")
|
||||
@ -1152,6 +1188,12 @@ func UpdateSSHHostInFile(oldName string, newHost SSHHost, configPath string) err
|
||||
if 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
|
||||
if newHost.Options != "" {
|
||||
options := strings.Split(newHost.Options, "\n")
|
||||
@ -1200,6 +1242,12 @@ func UpdateSSHHostInFile(oldName string, newHost SSHHost, configPath string) err
|
||||
if 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
|
||||
if newHost.Options != "" {
|
||||
options := strings.Split(newHost.Options, "\n")
|
||||
@ -1694,6 +1742,12 @@ func UpdateMultiHostBlock(originalHosts, newHosts []string, commonProperties SSH
|
||||
if 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
|
||||
if commonProperties.Options != "" {
|
||||
@ -1774,6 +1828,12 @@ func UpdateMultiHostBlock(originalHosts, newHosts []string, commonProperties SSH
|
||||
if 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
|
||||
if commonProperties.Options != "" {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
@ -16,6 +17,7 @@ import (
|
||||
type addFormModel struct {
|
||||
inputs []textinput.Model
|
||||
focused int
|
||||
currentTab int // 0 = General, 1 = Advanced
|
||||
err string
|
||||
styles Styles
|
||||
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
|
||||
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].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{
|
||||
inputs: inputs,
|
||||
focused: nameInput,
|
||||
currentTab: tabGeneral, // Start on General tab
|
||||
styles: styles,
|
||||
width: width,
|
||||
height: height,
|
||||
@ -111,6 +126,11 @@ func NewAddForm(hostname string, styles Styles, width, height int, configFile st
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
tabGeneral = iota
|
||||
tabAdvanced
|
||||
)
|
||||
|
||||
const (
|
||||
nameInput = iota
|
||||
hostnameInput
|
||||
@ -118,8 +138,11 @@ const (
|
||||
portInput
|
||||
identityInput
|
||||
proxyJumpInput
|
||||
optionsInput
|
||||
tagsInput
|
||||
// Advanced tab inputs
|
||||
optionsInput
|
||||
remoteCommandInput
|
||||
requestTTYInput
|
||||
)
|
||||
|
||||
// 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)
|
||||
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":
|
||||
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...)
|
||||
return m, m.handleNavigation(msg.String())
|
||||
}
|
||||
|
||||
case addFormSubmitMsg:
|
||||
@ -206,32 +213,104 @@ func (m *addFormModel) Update(msg tea.Msg) (*addFormModel, tea.Cmd) {
|
||||
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 {
|
||||
if m.success {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Check if terminal height is sufficient
|
||||
if !m.isHeightSufficient() {
|
||||
return m.renderHeightWarning()
|
||||
}
|
||||
|
||||
var b strings.Builder
|
||||
|
||||
b.WriteString(m.styles.FormTitle.Render("Add SSH Host Configuration"))
|
||||
b.WriteString("\n\n")
|
||||
|
||||
fields := []string{
|
||||
"Host Name *",
|
||||
"Hostname/IP *",
|
||||
"User",
|
||||
"Port",
|
||||
"Identity File",
|
||||
"ProxyJump",
|
||||
"SSH Options",
|
||||
"Tags (comma-separated)",
|
||||
}
|
||||
// Render tabs
|
||||
b.WriteString(m.renderTabs())
|
||||
b.WriteString("\n\n")
|
||||
|
||||
for i, field := range fields {
|
||||
b.WriteString(m.styles.FormField.Render(field))
|
||||
b.WriteString("\n")
|
||||
b.WriteString(m.inputs[i].View())
|
||||
b.WriteString("\n\n")
|
||||
// Render current tab content
|
||||
switch m.currentTab {
|
||||
case tabGeneral:
|
||||
b.WriteString(m.renderGeneralTab())
|
||||
case tabAdvanced:
|
||||
b.WriteString(m.renderAdvancedTab())
|
||||
}
|
||||
|
||||
if m.err != "" {
|
||||
@ -239,13 +318,133 @@ func (m *addFormModel) View() string {
|
||||
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(m.styles.FormHelp.Render("* Required fields"))
|
||||
|
||||
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
|
||||
type standaloneAddForm struct {
|
||||
*addFormModel
|
||||
@ -291,6 +490,8 @@ func (m *addFormModel) submitForm() tea.Cmd {
|
||||
identity := strings.TrimSpace(m.inputs[identityInput].Value())
|
||||
proxyJump := strings.TrimSpace(m.inputs[proxyJumpInput].Value())
|
||||
options := strings.TrimSpace(m.inputs[optionsInput].Value())
|
||||
remoteCommand := strings.TrimSpace(m.inputs[remoteCommandInput].Value())
|
||||
requestTTY := strings.TrimSpace(m.inputs[requestTTYInput].Value())
|
||||
|
||||
// Set defaults
|
||||
if user == "" {
|
||||
@ -319,14 +520,16 @@ func (m *addFormModel) submitForm() tea.Cmd {
|
||||
|
||||
// Create host configuration
|
||||
host := config.SSHHost{
|
||||
Name: name,
|
||||
Hostname: hostname,
|
||||
User: user,
|
||||
Port: port,
|
||||
Identity: identity,
|
||||
ProxyJump: proxyJump,
|
||||
Options: config.ParseSSHOptionsFromCommand(options),
|
||||
Tags: tags,
|
||||
Name: name,
|
||||
Hostname: hostname,
|
||||
User: user,
|
||||
Port: port,
|
||||
Identity: identity,
|
||||
ProxyJump: proxyJump,
|
||||
Options: config.ParseSSHOptionsFromCommand(options),
|
||||
RemoteCommand: remoteCommand,
|
||||
RequestTTY: requestTTY,
|
||||
Tags: tags,
|
||||
}
|
||||
|
||||
// Add to config
|
||||
|
@ -9,7 +9,6 @@ import (
|
||||
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -29,8 +28,8 @@ type editFormModel struct {
|
||||
inputs []textinput.Model
|
||||
focusArea int // 0=hosts, 1=properties
|
||||
focused int
|
||||
currentTab int // 0=General, 1=Advanced (only applies when focusArea == focusAreaProperties)
|
||||
err string
|
||||
success bool
|
||||
styles Styles
|
||||
originalName string
|
||||
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
|
||||
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, ", "))
|
||||
}
|
||||
|
||||
// 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{
|
||||
hostInputs: hostInputs,
|
||||
inputs: inputs,
|
||||
focusArea: focusAreaHosts, // Start with hosts focused for multi-host editing
|
||||
focused: 0,
|
||||
currentTab: 0, // Start on General tab
|
||||
originalName: hostName,
|
||||
originalHosts: hostNames,
|
||||
host: host,
|
||||
@ -235,6 +249,157 @@ func (m *editFormModel) updateFocus() tea.Cmd {
|
||||
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) {
|
||||
var cmds []tea.Cmd
|
||||
|
||||
@ -247,51 +412,33 @@ func (m *editFormModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg.String() {
|
||||
case "ctrl+c", "esc":
|
||||
m.err = ""
|
||||
m.success = false
|
||||
return m, func() tea.Msg { return editFormCancelMsg{} }
|
||||
|
||||
case "ctrl+s":
|
||||
// Allow submission from any field with Ctrl+S (Save)
|
||||
return m, m.submitEditForm()
|
||||
|
||||
case "tab", "shift+tab", "enter", "up", "down":
|
||||
s := msg.String()
|
||||
|
||||
// Handle form submission
|
||||
totalFields := len(m.hostInputs) + len(m.inputs)
|
||||
currentGlobalIndex := m.focused
|
||||
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 {
|
||||
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()
|
||||
|
||||
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":
|
||||
// Add a new host input
|
||||
return m, m.addHostInput()
|
||||
@ -306,10 +453,10 @@ func (m *editFormModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
case editFormSubmitMsg:
|
||||
if msg.err != nil {
|
||||
m.err = msg.err.Error()
|
||||
m.success = false
|
||||
} else {
|
||||
m.success = true
|
||||
m.err = ""
|
||||
// Success: let the wrapper handle this
|
||||
// In TUI mode, this will be handled by the parent
|
||||
// In standalone mode, the wrapper will quit
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
@ -332,15 +479,13 @@ func (m *editFormModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
}
|
||||
|
||||
func (m *editFormModel) View() string {
|
||||
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()
|
||||
// Check if terminal height is sufficient
|
||||
if !m.isHeightSufficient() {
|
||||
return m.renderHeightWarning()
|
||||
}
|
||||
|
||||
var b strings.Builder
|
||||
|
||||
if m.err != "" {
|
||||
b.WriteString(m.styles.Error.Render("Error: " + m.err))
|
||||
b.WriteString("\n\n")
|
||||
@ -377,25 +522,16 @@ func (m *editFormModel) View() string {
|
||||
b.WriteString(m.styles.FormTitle.Render("Common Properties"))
|
||||
b.WriteString("\n\n")
|
||||
|
||||
fields := []string{
|
||||
"Hostname/IP *",
|
||||
"User",
|
||||
"Port",
|
||||
"Identity File",
|
||||
"Proxy Jump",
|
||||
"SSH Options",
|
||||
"Tags (comma-separated)",
|
||||
}
|
||||
// Render tabs for properties
|
||||
b.WriteString(m.renderEditTabs())
|
||||
b.WriteString("\n\n")
|
||||
|
||||
for i, field := range fields {
|
||||
fieldStyle := m.styles.FormField
|
||||
if m.focusArea == focusAreaProperties && m.focused == i {
|
||||
fieldStyle = m.styles.FocusedLabel
|
||||
}
|
||||
b.WriteString(fieldStyle.Render(field))
|
||||
b.WriteString("\n")
|
||||
b.WriteString(m.inputs[i].View())
|
||||
b.WriteString("\n\n")
|
||||
// Render current tab content
|
||||
switch m.currentTab {
|
||||
case 0: // General
|
||||
b.WriteString(m.renderEditGeneralTab())
|
||||
case 1: // Advanced
|
||||
b.WriteString(m.renderEditAdvancedTab())
|
||||
}
|
||||
|
||||
if m.err != "" {
|
||||
@ -405,10 +541,10 @@ func (m *editFormModel) View() string {
|
||||
|
||||
// Show different help based on number of hosts
|
||||
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")
|
||||
} 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(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()
|
||||
}
|
||||
|
||||
// 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
|
||||
func RunEditForm(hostName string, configFile string) error {
|
||||
styles := NewStyles(80) // Default width
|
||||
@ -424,17 +656,10 @@ func RunEditForm(hostName string, configFile string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
p := tea.NewProgram(editForm, tea.WithAltScreen())
|
||||
m := standaloneEditForm{editForm}
|
||||
p := tea.NewProgram(m, tea.WithAltScreen())
|
||||
_, err = p.Run()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if editForm.err != "" {
|
||||
return fmt.Errorf(editForm.err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *editFormModel) submitEditForm() tea.Cmd {
|
||||
@ -453,12 +678,14 @@ func (m *editFormModel) submitEditForm() tea.Cmd {
|
||||
}
|
||||
|
||||
// Get property values using direct indices
|
||||
hostname := strings.TrimSpace(m.inputs[0].Value()) // hostnameInput
|
||||
user := strings.TrimSpace(m.inputs[1].Value()) // userInput
|
||||
port := strings.TrimSpace(m.inputs[2].Value()) // portInput
|
||||
identity := strings.TrimSpace(m.inputs[3].Value()) // identityInput
|
||||
proxyJump := strings.TrimSpace(m.inputs[4].Value()) // proxyJumpInput
|
||||
options := strings.TrimSpace(m.inputs[5].Value()) // optionsInput
|
||||
hostname := strings.TrimSpace(m.inputs[0].Value()) // hostnameInput
|
||||
user := strings.TrimSpace(m.inputs[1].Value()) // userInput
|
||||
port := strings.TrimSpace(m.inputs[2].Value()) // portInput
|
||||
identity := strings.TrimSpace(m.inputs[3].Value()) // identityInput
|
||||
proxyJump := strings.TrimSpace(m.inputs[4].Value()) // proxyJumpInput
|
||||
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
|
||||
if port == "" {
|
||||
@ -491,13 +718,15 @@ func (m *editFormModel) submitEditForm() tea.Cmd {
|
||||
|
||||
// Create the common host configuration
|
||||
commonHost := config.SSHHost{
|
||||
Hostname: hostname,
|
||||
User: user,
|
||||
Port: port,
|
||||
Identity: identity,
|
||||
ProxyJump: proxyJump,
|
||||
Options: options,
|
||||
Tags: tags,
|
||||
Hostname: hostname,
|
||||
User: user,
|
||||
Port: port,
|
||||
Identity: identity,
|
||||
ProxyJump: proxyJump,
|
||||
Options: options,
|
||||
RemoteCommand: remoteCommand,
|
||||
RequestTTY: requestTTY,
|
||||
Tags: tags,
|
||||
}
|
||||
|
||||
var err error
|
||||
|
@ -33,7 +33,8 @@ type Styles struct {
|
||||
HelpText lipgloss.Style
|
||||
|
||||
// Error and confirmation styles
|
||||
Error lipgloss.Style
|
||||
Error lipgloss.Style
|
||||
ErrorText lipgloss.Style
|
||||
|
||||
// Form styles (for add/edit forms)
|
||||
FormTitle lipgloss.Style
|
||||
@ -97,6 +98,11 @@ func NewStyles(width int) Styles {
|
||||
BorderForeground(lipgloss.Color(ErrorColor)).
|
||||
Padding(1, 2),
|
||||
|
||||
// Error text style (no border, just red text)
|
||||
ErrorText: lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color(ErrorColor)).
|
||||
Bold(true),
|
||||
|
||||
// Form styles
|
||||
FormTitle: lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color("#FFFDF5")).
|
||||
|
Loading…
x
Reference in New Issue
Block a user