Files
lazyssh/internal/adapters/ui/tui.go
T
Adem BaccaraandGitHub b7fb586e73 feat(parser): refactor ssh_config parser and writer to preserve unmanaged fields, comments, and directives (#45)
This PR introduces a major refactor of the SSH config parsing and writing logic. The new implementation is more robust and secure, ensuring that only the intended changes are applied while preserving the original file’s structure.

Key changes

- Lossless parsing/writing: Preserve unmanaged fields (e.g., `ProxyJump`), comments, and directives such as `Include` and `Match`.

- Library update: Switched to [github.com/kevinburke/ssh_config](https://github.com/kevinburke/ssh_config)
 as the base parser, with a custom fork to support required modifications https://github.com/adembc/ssh_config.

- Backup policy: Before any modification, create a backup of the SSH config file. Maintain up to 10 backups (configurable in the future) and automatically delete older ones.

- IdentityFile handling: Parse IdentityFile as an array instead of a single string, with improved update logic.

- Bug fix: Resolve issue where tags could not be fully removed.

- Multiple aliases: Support defining and managing multiple aliases for a single server.

- Testability: Code has been refactored with testability in mind. Follow-up PRs will include dedicated tests.
2025-09-10 23:24:50 +01:00

147 lines
3.7 KiB
Go

// 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.
package ui
import (
"github.com/gdamore/tcell/v2"
"go.uber.org/zap"
"github.com/Adembc/lazyssh/internal/core/ports"
"github.com/rivo/tview"
)
type App interface {
Run() error
}
type tui struct {
logger *zap.SugaredLogger
version string
commit string
app *tview.Application
serverService ports.ServerService
header *AppHeader
searchBar *SearchBar
hintBar *tview.TextView
serverList *ServerList
details *ServerDetails
statusBar *tview.TextView
root *tview.Flex
left *tview.Flex
content *tview.Flex
sortMode SortMode
searchVisible bool
}
func NewTUI(logger *zap.SugaredLogger, ss ports.ServerService, version, commit string) App {
return &tui{
logger: logger,
app: tview.NewApplication(),
serverService: ss,
version: version,
commit: commit,
}
}
func (t *tui) Run() error {
defer func() {
if r := recover(); r != nil {
t.logger.Errorw("panic recovered", "error", r)
}
}()
t.app.EnableMouse(true)
t.initializeTheme().buildComponents().buildLayout().bindEvents().loadInitialData()
t.app.SetRoot(t.root, true)
t.logger.Infow("starting TUI application", "version", t.version, "commit", t.commit)
if err := t.app.Run(); err != nil {
t.logger.Errorw("application run error", "error", err)
return err
}
return nil
}
func (t *tui) initializeTheme() *tui {
tview.Styles.PrimitiveBackgroundColor = tcell.Color232
tview.Styles.ContrastBackgroundColor = tcell.Color235
tview.Styles.BorderColor = tcell.Color238
tview.Styles.TitleColor = tcell.Color250
tview.Styles.PrimaryTextColor = tcell.Color252
tview.Styles.TertiaryTextColor = tcell.Color245
tview.Styles.SecondaryTextColor = tcell.Color245
tview.Styles.GraphicsColor = tcell.Color238
return t
}
func (t *tui) buildComponents() *tui {
t.header = NewAppHeader(t.version, t.commit, RepoURL)
t.searchBar = NewSearchBar().
OnSearch(t.handleSearchInput).
OnEscape(t.hideSearchBar)
t.hintBar = NewHintBar()
t.serverList = NewServerList().
OnSelectionChange(t.handleServerSelectionChange)
t.details = NewServerDetails()
t.statusBar = NewStatusBar()
// default sort mode
t.sortMode = SortByAliasAsc
return t
}
func (t *tui) buildLayout() *tui {
t.left = tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(t.hintBar, 1, 0, false).
AddItem(t.serverList, 0, 1, true)
right := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(t.details, 0, 1, false)
t.content = tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(t.left, 0, 3, true).
AddItem(right, 0, 2, false)
t.root = tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(t.header, 2, 0, false).
AddItem(t.content, 0, 1, true).
AddItem(t.statusBar, 1, 0, false)
return t
}
func (t *tui) bindEvents() *tui {
t.root.SetInputCapture(t.handleGlobalKeys)
return t
}
func (t *tui) loadInitialData() *tui {
servers, _ := t.serverService.ListServers("")
sortServersForUI(servers, t.sortMode)
t.updateListTitle()
t.serverList.UpdateServers(servers)
return t
}
func (t *tui) updateListTitle() {
if t.serverList != nil {
t.serverList.SetTitle("Servers — Sort: " + t.sortMode.String())
}
}