Skip to content

Commit 6ec850e

Browse files
committed
fix(state): State's local varriables are now private
Signed-off-by: dark0dave <dark0dave@mykolab.com>
1 parent 66f6a4d commit 6ec850e

6 files changed

Lines changed: 35 additions & 28 deletions

File tree

cmd/cmds.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@ type ContentMsg string
77
type PathMsg string
88
type TitleMsg string
99

10-
func sendSelectedFile(areapath string) tea.Cmd {
10+
func SendSelectedFile(areapath string) tea.Cmd {
1111
return func() tea.Msg {
1212
return SelectedFilePath(areapath)
1313
}
1414
}
1515

16-
func sendContentCmd(content string) tea.Cmd {
16+
func SendContentCmd(content string) tea.Cmd {
1717
return func() tea.Msg {
1818
return ContentMsg(content)
1919
}
2020
}
2121

22-
func sendPathCmd(path string) tea.Cmd {
22+
func SendPathCmd(path string) tea.Cmd {
2323
return func() tea.Msg {
2424
return PathMsg(path)
2525
}
2626
}
2727

28-
func sendTitleCmd(content string) tea.Cmd {
28+
func SendTitleCmd(content string) tea.Cmd {
2929
return func() tea.Msg {
3030
return TitleMsg(content)
3131
}

cmd/directory-picker.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"errors"
5+
"os"
56
"strings"
67
"time"
78

@@ -62,8 +63,14 @@ func (d directoryPicker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
6263
case tea.KeyMsg:
6364
switch msg.String() {
6465
case "e":
66+
fileInfo, err := os.Stat(d.selectedFile)
67+
if fileInfo.IsDir() != d.filepicker.DirAllowed || err != nil {
68+
d.err = errors.New(d.selectedFile + " is not valid.")
69+
d.selectedFile = ""
70+
return d, clearErrorAfter(2 * time.Second)
71+
}
6572
if d.selectedFile != "" {
66-
return state.SetAndGetNextCommand(d), tea.Sequence(sendPathCmd(d.startingDir), sendSelectedFile(d.selectedFile))
73+
return state.SetAndGetNextCommand(d), tea.Sequence(SendPathCmd(d.startingDir), SendSelectedFile(d.selectedFile))
6774
}
6875
case "q", "esc":
6976
return state.PreviousCommand(), nil

cmd/initial.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import (
66
list "github.com/charmbracelet/bubbles/list"
77
tea "github.com/charmbracelet/bubbletea"
88
"github.com/charmbracelet/lipgloss"
9+
"github.com/dark0dave/infinity_dialog/pkg/nav"
910
)
1011

1112
var (
12-
state = NewState()
13+
state = nav.NewState()
1314
docStyle = lipgloss.NewStyle().Margin(1, 2)
1415
width = 0
1516
height = 0
@@ -70,13 +71,13 @@ func (i initial) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
7071
t := NewTree()
7172
v := NewFileView()
7273
state.SetNextCommand(d).SetNextCommand(f).SetNextCommand(t).SetNextCommand(v)
73-
return state.SetAndGetNextCommand(i), sendSelectedFile(current_path)
74+
return state.SetAndGetNextCommand(i), SendSelectedFile(current_path)
7475
case "Discover":
7576
d := NewDirectoryPicker(true, "Select a Mod Directory")
7677
l := NewList()
7778
f := NewFileView()
7879
state.SetNextCommand(d).SetNextCommand(l).SetNextCommand(f)
79-
return state.SetAndGetNextCommand(i), sendSelectedFile(current_path)
80+
return state.SetAndGetNextCommand(i), SendSelectedFile(current_path)
8081
}
8182
}
8283
}

cmd/list-varriables.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ func (l listVariables) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
130130
content += s
131131
}
132132
title := strings.Join(l.table.SelectedRow()[:3], " ")
133-
state.setCurrentCommand(l)
134-
return state.NextCommand(), tea.Sequence(sendTitleCmd(title), sendContentCmd(content))
133+
return state.SetAndGetNextCommand(l), tea.Sequence(SendTitleCmd(title), SendContentCmd(content))
135134
}
136135
}
137136
var cmd tea.Cmd

cmd/tree.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ func (n nested) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
8585
case "e", "enter":
8686
nodes := n.tree.Nodes()
8787
node, _ := getSelected(&n, &nodes, 0)
88-
return state.SetAndGetNextCommand(n), sendPathCmd(node.Desc)
88+
return state.SetAndGetNextCommand(n), SendPathCmd(node.Desc)
8989
case "q", "esc":
90-
return state.next.SetAndGetPreviousCommand(n), nil
90+
return state.SetAndGetPreviousCommand(n), nil
9191
case "ctrl+c", "ctrl+d":
9292
return n, tea.Quit
9393
}

cmd/state.go renamed to pkg/nav/state.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
package cmd
1+
package nav
22

33
import tea "github.com/charmbracelet/bubbletea"
44

55
var position = 0
66

7-
type State struct {
7+
type state struct {
88
model tea.Model
9-
next *State
10-
previous *State
9+
next *state
10+
previous *state
1111
}
1212

13-
func NewState() *State {
14-
return &State{}
13+
func NewState() *state {
14+
return &state{}
1515
}
1616

17-
func (s *State) setCurrentCommand(m tea.Model) {
17+
func (s *state) setCurrentCommand(m tea.Model) {
1818
if s.model == nil {
19-
*s = State{
19+
*s = state{
2020
model: m,
2121
next: s.next,
2222
previous: s.previous,
@@ -30,7 +30,7 @@ func (s *State) setCurrentCommand(m tea.Model) {
3030
s.model = m
3131
}
3232

33-
func (s *State) NextCommand() tea.Model {
33+
func (s *state) NextCommand() tea.Model {
3434
position += 1
3535
for range position {
3636
if s.next != nil {
@@ -41,9 +41,9 @@ func (s *State) NextCommand() tea.Model {
4141
return m
4242
}
4343

44-
func (s *State) SetNextCommand(m tea.Model) *State {
44+
func (s *state) SetNextCommand(m tea.Model) *state {
4545
if s.next == nil {
46-
s.next = &State{
46+
s.next = &state{
4747
model: m,
4848
next: nil,
4949
previous: s,
@@ -54,12 +54,12 @@ func (s *State) SetNextCommand(m tea.Model) *State {
5454
}
5555
}
5656

57-
func (s *State) SetAndGetNextCommand(m tea.Model) tea.Model {
57+
func (s *state) SetAndGetNextCommand(m tea.Model) tea.Model {
5858
s.setCurrentCommand(m)
5959
return s.NextCommand()
6060
}
6161

62-
func (s *State) PreviousCommand() tea.Model {
62+
func (s *state) PreviousCommand() tea.Model {
6363
position -= 1
6464
for range position {
6565
if s.next != nil {
@@ -70,9 +70,9 @@ func (s *State) PreviousCommand() tea.Model {
7070
return m
7171
}
7272

73-
func (s *State) SetPreviousCommand(m tea.Model) *State {
73+
func (s *state) SetPreviousCommand(m tea.Model) *state {
7474
if s.previous == nil {
75-
s.previous = &State{
75+
s.previous = &state{
7676
model: m,
7777
next: s,
7878
previous: nil,
@@ -83,7 +83,7 @@ func (s *State) SetPreviousCommand(m tea.Model) *State {
8383
}
8484
}
8585

86-
func (s *State) SetAndGetPreviousCommand(m tea.Model) tea.Model {
86+
func (s *state) SetAndGetPreviousCommand(m tea.Model) tea.Model {
8787
s.setCurrentCommand(m)
8888
return s.PreviousCommand()
8989
}

0 commit comments

Comments
 (0)