Skip to content

Commit

Permalink
Refactor (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ernesto27 authored Jun 30, 2023
1 parent a06e8ff commit ed4b60d
Show file tree
Hide file tree
Showing 12 changed files with 555 additions and 323 deletions.
30 changes: 26 additions & 4 deletions models/container_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import (
"fmt"

"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss"
)

func NewContainerDetail(container docker.MyContainer, createTable utils.CreateTableFunc) (viewport.Model, error) {
type ContainerDetail struct {
viewport viewport.Model
}

func NewContainerDetail(container docker.MyContainer, createTable utils.CreateTableFunc) (ContainerDetail, error) {
content := getContent(container)

const width = 120
Expand All @@ -26,17 +31,34 @@ func NewContainerDetail(container docker.MyContainer, createTable utils.CreateTa
glamour.WithWordWrap(width),
)
if err != nil {
return viewport.Model{}, err
return ContainerDetail{}, err
}

str, err := renderer.Render(content)
if err != nil {
return viewport.Model{}, err
return ContainerDetail{}, err
}

vp.SetContent(str)

return vp, nil
return ContainerDetail{viewport: vp}, nil
}

func (cd ContainerDetail) View() string {
return cd.viewport.View() + helpStyle("\n ↑/↓: Navigate • Esc: back to list\n")
}

func (cd ContainerDetail) Update(msg tea.Msg, m *model) (viewport.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "esc":
m.setContainerList()
}
}

cd.viewport, _ = cd.viewport.Update(msg)
return cd.viewport, nil
}

func getContent(container docker.MyContainer) string {
Expand Down
78 changes: 76 additions & 2 deletions models/container_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package models

import (
"dockerniceui/docker"
"dockerniceui/utils"
"fmt"
"sort"
"strings"

"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

Expand All @@ -15,7 +17,11 @@ const (
exited = "exited"
)

func NewContainerList(rows []table.Row) table.Model {
type ContainerList struct {
table table.Model
}

func NewContainerList(rows []table.Row) ContainerList {
columns := []table.Column{
{Title: "ID", Width: 20},
{Title: "Container", Width: 30},
Expand Down Expand Up @@ -45,7 +51,75 @@ func NewContainerList(rows []table.Row) table.Model {
Bold(false)
t.SetStyles(s)

return t
return ContainerList{table: t}
}

func (cl ContainerList) View(commands string, dockerVersion string) string {
return baseStyle.Render(cl.table.View()) + helpStyle("\n DockerVersion: "+dockerVersion+" \n"+commands)
}

func (cl ContainerList) Update(msg tea.Msg, m *model) (table.Model, tea.Cmd) {
cl.table, _ = cl.table.Update(msg)
if m.currentModel != MContainerList {
return cl.table, nil
}

switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "enter":
container, err := m.dockerClient.GetContainerByName(m.containerList.table.SelectedRow()[1])
if err != nil {
fmt.Println(err)
}
vp, err := NewContainerDetail(container, utils.CreateTable)
if err != nil {
fmt.Println(err)
}

m.containerDetail = vp
m.currentModel = MContainerDetail
return cl.table, nil
case "ctrl+f":
m.containerSearch.textInput.SetValue("")
m.currentModel = MContainerSearch
case "ctrl+o":
ov := NewContainerOptions(m.containerList.table.SelectedRow()[1], m.containerList.table.SelectedRow()[2])
m.containerOptions = ov
m.currentModel = MContainerOptions
m.ContainerID = m.containerList.table.SelectedRow()[0]
case "ctrl+l":
containerLogs, err := m.dockerClient.ContainerLogs(m.containerList.table.SelectedRow()[0])
if err != nil {
panic(err)
}

headerHeight := lipgloss.Height(HeaderView(m.containerLogs.pager, m.containerList.table.SelectedRow()[1]))
lv := NewContainerLogs(m.widthScreen, m.heightScreen, containerLogs, headerHeight)
lv.container = m.containerList.table.SelectedRow()[1]
lv.image = m.containerList.table.SelectedRow()[2]
m.containerLogs = lv
m.currentModel = MContainerLogs
case "ctrl+b":
images, err := m.dockerClient.ImageList()
if err != nil {
fmt.Println(err)
}

m.imageList = NewImageList(images, "")
m.currentModel = MImageList
case "ctrl+n":
networks, err := m.dockerClient.NetworkList()
if err != nil {
fmt.Println(err)
}

m.networkList = NewNetworkList(networks, "")
m.currentModel = MNetworkList
}
}

return cl.table, nil
}

func GetContainerRows(containerList []docker.MyContainer, query string) []table.Row {
Expand Down
12 changes: 10 additions & 2 deletions models/container_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ var (
}()
)

func NewContainerLogs(width int, height int, logs string, headerHeight int) viewport.Model {
type LogsView struct {
pager viewport.Model
container string
image string
}

func NewContainerLogs(width int, height int, logs string, headerHeight int) LogsView {
p := viewport.New(width, height)
p.YPosition = headerHeight + 1
p.SetContent(logs)
return p
return LogsView{
pager: p,
}
}

func HeaderView(pager viewport.Model, text string) string {
Expand Down
90 changes: 65 additions & 25 deletions models/container_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,31 @@ package models
import (
"fmt"
"strings"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

type Options struct {
Cursor int
Choice string
Choices []string
Container string
Image string
type ContainerOptions struct {
Options
}

const (
Stop = "Stop"
Start = "Start"
Remove = "Remove"
Restart = "Restart"
)

func NewContainerOptions(container string, image string) Options {
var choices []string
if container != "" {
choices = []string{Stop, Start, Remove, Restart}
} else {
choices = []string{Remove}
}
func NewContainerOptions(container string, image string) ContainerOptions {
choices := []string{Stop, Start, Remove, Restart}

return Options{
Choices: choices,
Container: container,
Image: image,
return ContainerOptions{
Options{
Cursor: 0,
Choice: "",
Choices: choices,
Container: container,
Image: image,
},
}
}

func (o Options) View() string {
func (o ContainerOptions) View() string {
s := strings.Builder{}

var style = lipgloss.NewStyle().
Expand Down Expand Up @@ -68,3 +58,53 @@ func (o Options) View() string {

return style.Render(options) + s.String()
}

func (o ContainerOptions) Update(msg tea.Msg, m *model) (ContainerOptions, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "enter":
if m.currentModel != MContainerOptions {
return o, nil
}

errAction := false
switch m.containerOptions.Choices[m.containerOptions.Cursor] {
case Stop:
err := m.dockerClient.ContainerStop(m.ContainerID)
if err != nil {
fmt.Println(err)
errAction = true
}
time.Sleep(1 * time.Second)
case Start:
err := m.dockerClient.ContainerStart(m.ContainerID)
if err != nil {
fmt.Println(err)
errAction = true
}
case Remove:
err := m.dockerClient.ContainerRemove(m.ContainerID)
if err != nil {
fmt.Println(err)
errAction = true
}
case Restart:
err := m.dockerClient.ContainerRestart(m.ContainerID)
if err != nil {
fmt.Println(err)
errAction = true
}
}

if !errAction {
m.setContainerList()
m.currentModel = MContainerList
return o, tea.ClearScreen
}

}
}

return o, nil
}
44 changes: 44 additions & 0 deletions models/container_search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package models

import (
"fmt"

tea "github.com/charmbracelet/bubbletea"
)

type ContainerSearch struct {
Search
}

func NewContainerSearch() ContainerSearch {
return ContainerSearch{
NewSearch(),
}
}

func (cs ContainerSearch) View() string {
return fmt.Sprintf(
"Search container by name\n\n%s\n\n%s",
cs.textInput.View(),
"(esc to back)",
) + "\n"
}

func (cs ContainerSearch) Update(msg tea.Msg, m *model) (ContainerSearch, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "enter":
if m.currentModel != MContainerSearch {
return cs, nil
}
value := cs.textInput.Value()
t := NewContainerList(GetContainerRows(m.dockerClient.Containers, value))
m.containerList = t
m.currentModel = MContainerList
}
}

cs.textInput, _ = cs.textInput.Update(msg)
return cs, nil
}
Loading

0 comments on commit ed4b60d

Please sign in to comment.