sshm/internal/ui/utils.go
Gu1llaum-3 be3dcaa1cd fix: enable editing and management of hosts from included SSH config files
• Add SourceFile field to SSHHost struct to track config file origins
• Implement FindHostInAllConfigs() to locate hosts across all config files
• Fix "host not found" errors when editing/deleting hosts from included files
• Add GetAllConfigFiles() and GetAllConfigFilesFromBase() for config discovery
• Create UpdateSSHHostV2() and DeleteSSHHostV2() for cross-file operations
• Display config file source in edit and info forms for better visibility
• Add intelligent file selector for host addition when multiple configs exist
• Support -c parameter context with proper file resolution
• Exclude .backup files from Include directive processing
• Maintain backward compatibility with existing SSH config workflows

Resolves limitation where hosts from included config files could be viewed
but not edited, deleted, or properly managed through the interface.
2025-09-05 17:04:11 +02:00

72 lines
1.7 KiB
Go

package ui
import (
"fmt"
"strings"
"time"
)
// formatTimeAgo formats a time into a readable "X time ago" string
func formatTimeAgo(t time.Time) string {
now := time.Now()
duration := now.Sub(t)
switch {
case duration < time.Minute:
seconds := int(duration.Seconds())
if seconds <= 1 {
return "1 second ago"
}
return fmt.Sprintf("%d seconds ago", seconds)
case duration < time.Hour:
minutes := int(duration.Minutes())
if minutes == 1 {
return "1 minute ago"
}
return fmt.Sprintf("%d minutes ago", minutes)
case duration < 24*time.Hour:
hours := int(duration.Hours())
if hours == 1 {
return "1 hour ago"
}
return fmt.Sprintf("%d hours ago", hours)
case duration < 7*24*time.Hour:
days := int(duration.Hours() / 24)
if days == 1 {
return "1 day ago"
}
return fmt.Sprintf("%d days ago", days)
case duration < 30*24*time.Hour:
weeks := int(duration.Hours() / (24 * 7))
if weeks == 1 {
return "1 week ago"
}
return fmt.Sprintf("%d weeks ago", weeks)
case duration < 365*24*time.Hour:
months := int(duration.Hours() / (24 * 30))
if months == 1 {
return "1 month ago"
}
return fmt.Sprintf("%d months ago", months)
default:
years := int(duration.Hours() / (24 * 365))
if years == 1 {
return "1 year ago"
}
return fmt.Sprintf("%d years ago", years)
}
}
// formatConfigFile formats a config file path for display
func formatConfigFile(filePath string) string {
if filePath == "" {
return "Unknown"
}
// Show just the filename and parent directory for readability
parts := strings.Split(filePath, "/")
if len(parts) >= 2 {
return fmt.Sprintf(".../%s/%s", parts[len(parts)-2], parts[len(parts)-1])
}
return filePath
}