2025-08-17 17:54:21 +01:00
|
|
|
// Copyright 2025.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2025-08-17 11:09:20 +01:00
|
|
|
package ui
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2025-08-27 16:56:42 +01:00
|
|
|
"strings"
|
2025-08-17 17:54:21 +01:00
|
|
|
|
2025-08-17 11:09:20 +01:00
|
|
|
"github.com/Adembc/lazyssh/internal/core/domain"
|
|
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
|
"github.com/rivo/tview"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ServerDetails struct {
|
|
|
|
|
*tview.TextView
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewServerDetails() *ServerDetails {
|
|
|
|
|
details := &ServerDetails{
|
|
|
|
|
TextView: tview.NewTextView(),
|
|
|
|
|
}
|
|
|
|
|
details.build()
|
|
|
|
|
return details
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sd *ServerDetails) build() {
|
2025-08-17 17:54:21 +01:00
|
|
|
sd.TextView.SetDynamicColors(true).
|
2025-08-17 11:09:20 +01:00
|
|
|
SetWrap(true).
|
|
|
|
|
SetBorder(true).
|
|
|
|
|
SetTitle("Details").
|
|
|
|
|
SetBorderColor(tcell.Color238).
|
|
|
|
|
SetTitleColor(tcell.Color250)
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-27 16:56:42 +01:00
|
|
|
// renderTagChips builds colored tag chips for details view.
|
|
|
|
|
func renderTagChips(tags []string) string {
|
|
|
|
|
if len(tags) == 0 {
|
|
|
|
|
return "-"
|
|
|
|
|
}
|
|
|
|
|
chips := make([]string, 0, len(tags))
|
|
|
|
|
for _, t := range tags {
|
|
|
|
|
chips = append(chips, fmt.Sprintf("[black:#5FAFFF] %s [-:-:-]", t))
|
|
|
|
|
}
|
|
|
|
|
return strings.Join(chips, " ")
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-17 11:09:20 +01:00
|
|
|
func (sd *ServerDetails) UpdateServer(server domain.Server) {
|
2025-08-27 16:56:42 +01:00
|
|
|
lastSeen := server.LastSeen.Format("2006-01-02 15:04:05")
|
|
|
|
|
if server.LastSeen.IsZero() {
|
|
|
|
|
lastSeen = "Never"
|
|
|
|
|
}
|
|
|
|
|
serverKey := server.Key
|
|
|
|
|
if serverKey == "" {
|
|
|
|
|
serverKey = "(default: ~/.ssh/id_{rsa,ed25519,ecdsa})"
|
|
|
|
|
}
|
|
|
|
|
pinnedStr := "true"
|
|
|
|
|
if server.PinnedAt.IsZero() {
|
|
|
|
|
pinnedStr = "false"
|
|
|
|
|
}
|
|
|
|
|
tagsText := renderTagChips(server.Tags)
|
2025-08-17 11:09:20 +01:00
|
|
|
text := fmt.Sprintf(
|
2025-08-27 16:56:42 +01:00
|
|
|
"[::b]%s[-]\n\nHost: [white]%s[-]\nUser: [white]%s[-]\nPort: [white]%d[-]\nKey: [white]%s[-]\nTags: %s\nPinned: [white]%s[-]\nLast SSH: %s\nSSH Count: [white]%d[-]\n\n[::b]Commands:[-]\n Enter: SSH connect\n c: Copy SSH command\n g: Ping server\n r: Refresh list\n a: Add new server\n e: Edit entry\n t: Edit tags\n d: Delete entry\n p: Pin/Unpin",
|
2025-08-17 11:09:20 +01:00
|
|
|
server.Alias, server.Host, server.User, server.Port,
|
2025-08-27 16:56:42 +01:00
|
|
|
serverKey, tagsText, pinnedStr,
|
|
|
|
|
lastSeen, server.SSHCount)
|
2025-08-17 17:54:21 +01:00
|
|
|
sd.TextView.SetText(text)
|
2025-08-17 11:09:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sd *ServerDetails) ShowEmpty() {
|
2025-08-17 17:54:21 +01:00
|
|
|
sd.TextView.SetText("No servers match the current filter.")
|
2025-08-17 11:09:20 +01:00
|
|
|
}
|