Skip to content

Commit

Permalink
fix network search
Browse files Browse the repository at this point in the history
  • Loading branch information
ernesto27 committed Jul 2, 2023
1 parent 3fda2dd commit b81acdc
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
8 changes: 4 additions & 4 deletions models/image_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ func NewImageOptions(container string, image string) ImageOptions {
}

func (o ImageOptions) Update(msg tea.Msg, m *model) (ImageOptions, tea.Cmd) {
if m.currentModel != MImageOptions {
return o, nil
}

switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "enter":
if m.currentModel != MImageOptions {
return o, nil
}

errAction := false
option := m.imageOptions.Choices[m.imageOptions.Cursor]

Expand Down
14 changes: 5 additions & 9 deletions models/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type model struct {
imageSearch ImageSearch
imageOptions ImageOptions
networkList NetworkList
networkSearch Search
networkSearch NetworkSearch
networkDetail viewport.Model
ready bool
currentModel currentModel
Expand All @@ -66,7 +66,7 @@ func NewModel(dockerClient *docker.Docker, version string) *model {
dockerClient: dockerClient,
containerSearch: NewContainerSearch(),
imageSearch: NewImageSearch(),
networkSearch: NewSearch(),
networkSearch: NewNetworkSearch(),
currentModel: MContainerList,
dockerVersion: version,
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.ClearScreen
}

if m.currentModel == MNetworkDetail {
if m.currentModel == MNetworkDetail || m.currentModel == MNetworkSearch {
m.currentModel = MNetworkList
return m, tea.ClearScreen
}
Expand Down Expand Up @@ -157,7 +157,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.imageDetail, _ = m.imageDetail.Update(msg)

m.networkList.table, _ = m.networkList.Update(msg, &m)
m.networkSearch.textInput, _ = m.networkSearch.textInput.Update(msg)
m.networkSearch, _ = m.networkSearch.Update(msg, &m)

cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
Expand Down Expand Up @@ -208,11 +208,7 @@ func (m model) View() string {
case MNetworkList:
return m.networkList.View(commands, m.dockerVersion)
case MNetworkSearch:
return fmt.Sprintf(
"Search network by name\n\n%s\n\n%s",
m.networkSearch.textInput.View(),
"(esc to back)",
) + "\n"
return m.networkSearch.View()
case MNetworkDetail:
return m.networkDetail.View()

Expand Down
3 changes: 3 additions & 0 deletions models/network_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func (cl NetworkList) Update(msg tea.Msg, m *model) (table.Model, tea.Cmd) {
}
m.networkDetail = nd
m.currentModel = MNetworkDetail
case "ctrl+f":
m.networkSearch.textInput.SetValue("")
m.currentModel = MNetworkSearch
}
}

Expand Down
44 changes: 44 additions & 0 deletions models/network_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 NetworkSearch struct {
Search
}

func NewNetworkSearch() NetworkSearch {
return NetworkSearch{
NewSearch(),
}
}

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

func (ns NetworkSearch) Update(msg tea.Msg, m *model) (NetworkSearch, tea.Cmd) {
if m.currentModel != MNetworkSearch {
return ns, nil
}

switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "enter":
value := ns.textInput.Value()
m.networkList = NewNetworkList(m.dockerClient.Networks, value)
m.currentModel = MNetworkList
}
}

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

0 comments on commit b81acdc

Please sign in to comment.