Skip to content

Commit

Permalink
Merge pull request #8 from byawitz/feat-better-history
Browse files Browse the repository at this point in the history
feat: better history
  • Loading branch information
byawitz authored Sep 24, 2024
2 parents e7109b0 + 57baac2 commit ce5fcd3
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 44 deletions.
2 changes: 1 addition & 1 deletion cmd/ggh.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Main() {
action, value := command.Which()
switch action {
case command.InteractiveHistory:
args = history.Interactive()
args = interactive.History()
case command.InteractiveConfig:
args = interactive.Config("")
case command.InteractiveConfigWithSearch:
Expand Down
37 changes: 11 additions & 26 deletions internal/history/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import (
"encoding/json"
"fmt"
"github.com/byawitz/ggh/internal/config"
"github.com/byawitz/ggh/internal/interactive"
"github.com/byawitz/ggh/internal/ssh"
"github.com/byawitz/ggh/internal/theme"
"github.com/charmbracelet/bubbles/table"
"log"
"os"
"time"
)

Expand All @@ -34,33 +31,21 @@ func Fetch(file []byte) ([]SSHHistory, error) {
return nil, err
}

return historyList, nil
}
func Interactive() []string {
list, err := FetchWithDefaultFile()
search, err := config.ParseWithSearch("", config.GetConfigFile())

if err != nil {
log.Fatal(err)
return historyList, nil
}

if len(list) == 0 {
fmt.Println("No history found.")
os.Exit(0)
for i, history := range historyList {
for _, sshConfig := range search {
if sshConfig.Host == history.Connection.Host {
historyList[i].Connection.Name = sshConfig.Name
}
}
}

var rows []table.Row
currentTime := time.Now()
for _, history := range list {
rows = append(rows, table.Row{
history.Connection.Host,
history.Connection.Port,
history.Connection.User,
history.Connection.Key,
fmt.Sprintf("%s", readableTime(currentTime.Sub(history.Date))),
})
}
c := interactive.Select(rows, interactive.SelectHistory)
return ssh.GenerateCommandArgs(c)
return historyList, nil
}

func Print() {
Expand All @@ -82,14 +67,14 @@ func Print() {
history.Connection.Port,
history.Connection.User,
history.Connection.Key,
fmt.Sprintf("%s", readableTime(currentTime.Sub(history.Date))),
fmt.Sprintf("%s", ReadableTime(currentTime.Sub(history.Date))),
})
}

fmt.Println(theme.PrintTable(rows, theme.PrintHistory))
}

func readableTime(d time.Duration) string {
func ReadableTime(d time.Duration) string {
if d.Seconds() < 60 {
return fmt.Sprintf("%d seconds ago", int(d.Seconds()))
}
Expand Down
34 changes: 32 additions & 2 deletions internal/history/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/byawitz/ggh/internal/config"
"github.com/charmbracelet/bubbles/table"
"os"
"slices"
"strings"
Expand All @@ -14,7 +15,6 @@ func AddHistoryFromArgs(args []string) {
if len(args) == 1 && !strings.Contains(args[0], "@") {
localConfig, err := config.GetConfig(args[0])
if err != nil || localConfig.Name == "" {
fmt.Printf("couldn't fetch %s from config file, error:%v.\n", args[0], err)
return
}

Expand Down Expand Up @@ -70,6 +70,33 @@ func AddHistory(c config.SSHConfig) {
}
}

func RemoveByIP(row table.Row) {
list, err := Fetch(getFile())

if err != nil {
fmt.Println("error getting ggh file")
return
}

ip := row[1]

saving := make([]SSHHistory, 0, len(list)-1)

for _, item := range list {
if item.Connection.Host == ip {
continue
}

saving = append(saving, item)
}

err = saveFile(SSHHistory{}, saving)
if err != nil {
panic("error saving ggh file")
}

}

func saveFile(n SSHHistory, l []SSHHistory) error {
file := getFileLocation()
fileContent := stringify(n, l)
Expand All @@ -89,7 +116,10 @@ func stringify(n SSHHistory, l []SSHHistory) string {
}
}

history = append(history, n)
if n.Connection.Host != "" {
history = append(history, n)
}

history = append(history, l...)
content, err := json.Marshal(history)

Expand Down
31 changes: 31 additions & 0 deletions internal/interactive/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package interactive
import (
"fmt"
"github.com/byawitz/ggh/internal/config"
"github.com/byawitz/ggh/internal/history"
"github.com/byawitz/ggh/internal/ssh"
"github.com/charmbracelet/bubbles/table"
"log"
"os"
"time"
)

func Config(value string) []string {
Expand All @@ -28,3 +31,31 @@ func Config(value string) []string {
c := Select(rows, SelectConfig)
return ssh.GenerateCommandArgs(c)
}

func History() []string {
list, err := history.FetchWithDefaultFile()

if err != nil {
log.Fatal(err)
}

if len(list) == 0 {
fmt.Println("No history found.")
os.Exit(0)
}

var rows []table.Row
currentTime := time.Now()
for _, historyItem := range list {
rows = append(rows, table.Row{
historyItem.Connection.Name,
historyItem.Connection.Host,
historyItem.Connection.Port,
historyItem.Connection.User,
historyItem.Connection.Key,
fmt.Sprintf("%s", history.ReadableTime(currentTime.Sub(historyItem.Date))),
})
}
c := Select(rows, SelectHistory)
return ssh.GenerateCommandArgs(c)
}
79 changes: 64 additions & 15 deletions internal/interactive/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package interactive
import (
"fmt"
"github.com/byawitz/ggh/internal/config"
"github.com/byawitz/ggh/internal/history"
"github.com/byawitz/ggh/internal/theme"
"math"
"os"
"slices"
"strings"

"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -33,6 +36,14 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "d":
history.RemoveByIP(m.table.SelectedRow())

rows := slices.Delete(m.table.Rows(), m.table.Cursor(), m.table.Cursor()+1)
m.table.SetRows(rows)

m.table, cmd = m.table.Update("") // Overrides default `d` behavior
return m, cmd
case "q", "ctrl+c", "esc":
m.exit = true
return m, tea.Quit
Expand All @@ -46,28 +57,19 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func setConfig(row table.Row, what Selecting) config.SSHConfig {
if what == SelectConfig {
return config.SSHConfig{
Host: row[1],
Port: row[2],
User: row[3],
Key: row[4],
}
}

return config.SSHConfig{
Host: row[0],
Port: row[1],
User: row[2],
Key: row[3],
Host: row[1],
Port: row[2],
User: row[3],
Key: row[4],
}
}

func (m model) View() string {
if m.choice.Host != "" || m.exit {
return ""
}
return theme.BaseStyle.Render(m.table.View()) + "\n " + m.table.HelpView() + "\n"
return theme.BaseStyle.Render(m.table.View()) + "\n " + m.HelpView() + "\n"
}

func Select(rows []table.Row, what Selecting) config.SSHConfig {
Expand All @@ -84,8 +86,9 @@ func Select(rows []table.Row, what Selecting) config.SSHConfig {

if what == SelectHistory {
columns = append(columns, []table.Column{
{Title: "Name", Width: 10},
{Title: "Host", Width: 15},
{Title: "Port", Width: 10},
{Title: "Port", Width: 4},
{Title: "User", Width: 10},
{Title: "Key", Width: 10},
{Title: "Last login", Width: 15},
Expand Down Expand Up @@ -123,3 +126,49 @@ func Select(rows []table.Row, what Selecting) config.SSHConfig {

return config.SSHConfig{}
}
func (m model) HelpView() string {

km := table.DefaultKeyMap()

var b strings.Builder

b.WriteString(generateHelpBlock(km.LineUp.Help().Key, km.LineUp.Help().Desc, true))
b.WriteString(generateHelpBlock(km.LineDown.Help().Key, km.LineDown.Help().Desc, true))

if m.what == SelectHistory {
b.WriteString(generateHelpBlock("d", "delete", true))
}

b.WriteString(generateHelpBlock("q/esc", "quit", false))

return b.String()
}

func generateHelpBlock(key, desc string, withSep bool) string {
keyStyle := lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{
Light: "#909090",
Dark: "#626262",
})

descStyle := lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{
Light: "#B2B2B2",
Dark: "#4A4A4A",
})

sepStyle := lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{
Light: "#DDDADA",
Dark: "#3C3C3C",
})

sep := sepStyle.Inline(true).Render(" • ")

str := keyStyle.Inline(true).Render(key) +
" " +
descStyle.Inline(true).Render(desc)

if withSep {
str += sep
}

return str
}

0 comments on commit ce5fcd3

Please sign in to comment.