Skip to content

Commit a2345fe

Browse files
authored
Merge pull request #4 from dark0dave/feature/backtrace
feat(back): Add backtracing
2 parents f1831b1 + 08bf82d commit a2345fe

7 files changed

Lines changed: 244 additions & 110 deletions

File tree

cmd/cmds.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cmd
2+
3+
import tea "github.com/charmbracelet/bubbletea"
4+
5+
type SelectedFilePath string
6+
type ContentMsg string
7+
type PathMsg string
8+
type TitleMsg string
9+
10+
func sendSelectedFile(areapath string) tea.Cmd {
11+
return func() tea.Msg {
12+
return SelectedFilePath(areapath)
13+
}
14+
}
15+
16+
func sendContentCmd(content string) tea.Cmd {
17+
return func() tea.Msg {
18+
return ContentMsg(content)
19+
}
20+
}
21+
22+
func sendPathCmd(path string) tea.Cmd {
23+
return func() tea.Msg {
24+
return PathMsg(path)
25+
}
26+
}
27+
28+
func sendTitleCmd(content string) tea.Cmd {
29+
return func() tea.Msg {
30+
return TitleMsg(content)
31+
}
32+
}

cmd/directory-picker.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package cmd
22

33
import (
44
"errors"
5-
"os"
6-
"path/filepath"
75
"strings"
86
"time"
97

@@ -18,27 +16,25 @@ var (
1816

1917
type directoryPicker struct {
2018
filepicker filepicker.Model
21-
nextCommand func(string) (tea.Model, tea.Cmd)
2219
message string
2320
selectedFile string
21+
startingDir string
2422
quitting bool
2523
err error
2624
}
2725

2826
// TODO: Hide unselectable
29-
func NewDirectoryPicker(dir bool, message string, nextCommand func(string) (tea.Model, tea.Cmd)) directoryPicker {
27+
func NewDirectoryPicker(dir bool, message string) directoryPicker {
3028
fp := filepicker.New()
3129
fp.DirAllowed = dir
3230
fp.FileAllowed = !dir
3331
h, _ := docStyle.GetFrameSize()
3432
fp.Height = height - h - 5
3533
fp.AutoHeight = true
3634
fp.ShowHidden = false
37-
fp.CurrentDirectory = currentDirectory
3835
return directoryPicker{
39-
filepicker: fp,
40-
nextCommand: nextCommand,
41-
message: message,
36+
filepicker: fp,
37+
message: message,
4238
}
4339
}
4440

@@ -56,16 +52,22 @@ func (d directoryPicker) Init() tea.Cmd {
5652

5753
func (d directoryPicker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5854
switch msg := msg.(type) {
55+
case SelectedFilePath:
56+
d.filepicker.CurrentDirectory = string(msg)
57+
d.startingDir = string(msg)
58+
return d, d.Init()
5959
case tea.WindowSizeMsg:
6060
h, _ := docStyle.GetFrameSize()
6161
d.filepicker.Height = d.filepicker.Height - h
6262
case tea.KeyMsg:
6363
switch msg.String() {
6464
case "e":
6565
if d.selectedFile != "" {
66-
return d.nextCommand(d.selectedFile)
66+
return state.SetAndGetNextCommand(d), tea.Sequence(sendPathCmd(d.startingDir), sendSelectedFile(d.selectedFile))
6767
}
68-
case "ctrl+c", "q", "esc":
68+
case "q", "esc":
69+
return state.PreviousCommand(), nil
70+
case "ctrl+c", "ctrl+d":
6971
d.quitting = true
7072
return d, tea.Quit
7173
}
@@ -95,16 +97,11 @@ func (d directoryPicker) View() string {
9597

9698
if d.err != nil {
9799
s.WriteString(d.filepicker.Styles.DisabledFile.Render(d.err.Error()))
98-
} else if d.selectedFile == "" {
99-
s.WriteString(d.message + "\n")
100100
} else {
101-
fileInfo, _ := os.Stat(d.selectedFile)
102-
if fileInfo.IsDir() {
103-
currentDirectory = d.selectedFile
104-
} else {
105-
currentDirectory = filepath.Dir(d.selectedFile)
106-
}
107-
s.WriteString("Selected: " + d.filepicker.Styles.Selected.Render(d.selectedFile))
101+
s.WriteString(d.message + "\n")
102+
}
103+
if d.selectedFile != "" {
104+
s.WriteString(" Selected: " + d.filepicker.Styles.Selected.Render(d.selectedFile))
108105
}
109106

110107
s.WriteString("\n\n" + d.filepicker.View() + "\n")

cmd/file-view.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,19 @@ type fileview struct {
3232
title string
3333
content string
3434
viewport viewport.Model
35-
callback func() (tea.Model, tea.Cmd)
3635
}
3736

38-
func NewFileView(content string, title string, callback func() (tea.Model, tea.Cmd)) fileview {
39-
f := fileview{title: title, content: content, callback: callback}
37+
func NewFileView() fileview {
38+
f := fileview{}
4039
headerHeight := lipgloss.Height(f.headerView())
4140
footerHeight := lipgloss.Height(f.footerView())
4241
verticalMarginHeight := headerHeight + footerHeight
4342
f.viewport = viewport.New(width, height-verticalMarginHeight)
4443
f.viewport.YPosition = headerHeight
45-
f.viewport.SetContent(f.content)
4644
return f
4745
}
4846

49-
func NewFileViewFromFile(path string, callback func() (tea.Model, tea.Cmd)) fileview {
47+
func GetFileContents(path string) (string, string) {
5048
content := ""
5149
if filepath.Ext(path) == ".are" {
5250
f, err := os.Open(path)
@@ -68,26 +66,39 @@ func NewFileViewFromFile(path string, callback func() (tea.Model, tea.Cmd)) file
6866
content = ""
6967
}
7068
}
71-
return NewFileView(content, filepath.Base(path), callback)
69+
return content, filepath.Base(path)
7270
}
7371

7472
func (f fileview) Init() tea.Cmd {
7573
return nil
7674
}
7775

7876
func (f fileview) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
79-
var cmd tea.Cmd
80-
8177
switch msg := msg.(type) {
78+
case ContentMsg:
79+
f.content = string(msg)
80+
f.viewport.SetContent(f.content)
81+
return f, nil
82+
case TitleMsg:
83+
f.title = string(msg)
84+
return f, nil
85+
case PathMsg:
86+
content, title := GetFileContents(string(msg))
87+
f.content = content
88+
f.viewport.SetContent(f.content)
89+
f.title = title
90+
return f, nil
8291
case tea.KeyMsg:
83-
if k := msg.String(); k == "ctrl+c" || k == "q" || k == "esc" {
84-
return f.callback()
92+
switch msg.String() {
93+
case "q", "esc":
94+
return state.PreviousCommand(), nil
95+
case "ctrl+c", "ctrl+d":
96+
return f, tea.Quit
8597
}
86-
8798
case tea.WindowSizeMsg:
8899
setViewport(f, msg)
89100
}
90-
101+
var cmd tea.Cmd
91102
f.viewport, cmd = f.viewport.Update(msg)
92103
return f, cmd
93104
}

cmd/initial.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
)
1010

1111
var (
12-
docStyle = lipgloss.NewStyle().Margin(1, 2)
13-
currentDirectory, _ = os.Getwd()
14-
width = 0
15-
height = 0
12+
state = NewState()
13+
docStyle = lipgloss.NewStyle().Margin(1, 2)
14+
width = 0
15+
height = 0
1616
)
1717

1818
type item struct {
@@ -56,25 +56,26 @@ func (i initial) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5656
height, width = max(msg.Height, height), max(msg.Width, width)
5757
case tea.KeyMsg:
5858
switch msg.String() {
59-
case "ctrl+c", "q":
59+
case "ctrl+c", "ctrl+d", "q":
6060
return i, tea.Quit
6161
case "enter", " ":
62+
current_path, err := os.Getwd()
63+
if err != nil {
64+
return i, tea.Quit
65+
}
6266
switch i.list.SelectedItem().FilterValue() {
6367
case "Traverse":
64-
d := NewDirectoryPicker(true, "Select a Mod Directory", func(dirpath string) (tea.Model, tea.Cmd) {
65-
f := NewDirectoryPicker(false, "Select an area to start", func(areapath string) (tea.Model, tea.Cmd) {
66-
t := NewTree(dirpath, areapath)
67-
return t, t.Init()
68-
})
69-
return f, f.Init()
70-
})
71-
return d, d.Init()
68+
d := NewDirectoryPicker(true, "Select a Mod Directory")
69+
f := NewDirectoryPicker(false, "Select an area to start")
70+
t := NewTree()
71+
state.SetNextCommand(d).SetNextCommand(f).SetNextCommand(t)
72+
return state.SetAndGetNextCommand(i), sendSelectedFile(current_path)
7273
case "Discover":
73-
d := NewDirectoryPicker(true, "Select a Mod Directory", func(path string) (tea.Model, tea.Cmd) {
74-
l := NewList(path)
75-
return l, l.Init()
76-
})
77-
return d, d.Init()
74+
d := NewDirectoryPicker(true, "Select a Mod Directory")
75+
l := NewList()
76+
f := NewFileView()
77+
state.SetNextCommand(d).SetNextCommand(l).SetNextCommand(f)
78+
return state.SetAndGetNextCommand(i), sendSelectedFile(current_path)
7879
}
7980
}
8081
}

cmd/list-varriables.go

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,16 @@ func generateRows(path string, file fs.FileInfo) *[]table.Row {
3636
return &rows
3737
}
3838

39-
func NewList(path string) listVariables {
39+
func NewList() listVariables {
4040
columns := []table.Column{
4141
{Title: "FileName", Width: int(0.2 * float64(width))},
4242
{Title: "Lang", Width: int(0.1 * float64(width))},
4343
{Title: "Id", Width: int(0.05 * float64(width))},
4444
{Title: "Value", Width: int(0.55 * float64(width))},
4545
}
46-
rows := []table.Row{}
47-
filepath.WalkDir(path, func(path string, file fs.DirEntry, err error) error {
48-
if err != nil {
49-
return err
50-
}
51-
ext := filepath.Ext(file.Name())
52-
if !file.IsDir() && strings.ToLower(ext) == ".tra" {
53-
info, _ := file.Info()
54-
file_rows := *generateRows(path, info)
55-
rows = append(rows, file_rows...)
56-
}
57-
58-
return nil
59-
})
6046

6147
t := table.New(
6248
table.WithColumns(columns),
63-
table.WithRows(rows),
6449
table.WithFocused(true),
6550
table.WithHeight(height-7),
6651
)
@@ -81,11 +66,32 @@ func NewList(path string) listVariables {
8166
return listVariables{table: t}
8267
}
8368

69+
func (l listVariables) readPath(path string) *[]table.Row {
70+
rows := []table.Row{}
71+
filepath.WalkDir(path, func(path string, file fs.DirEntry, err error) error {
72+
if err != nil {
73+
return err
74+
}
75+
ext := filepath.Ext(file.Name())
76+
if !file.IsDir() && strings.ToLower(ext) == ".tra" {
77+
info, _ := file.Info()
78+
file_rows := *generateRows(path, info)
79+
rows = append(rows, file_rows...)
80+
}
81+
82+
return nil
83+
})
84+
return &rows
85+
}
86+
8487
func (l listVariables) Init() tea.Cmd { return nil }
8588

8689
func (l listVariables) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
87-
var cmd tea.Cmd
8890
switch msg := msg.(type) {
91+
case SelectedFilePath:
92+
rows := l.readPath(string(msg))
93+
l.table.SetRows(*rows)
94+
return l, nil
8995
case tea.WindowSizeMsg:
9096
h, w := docStyle.GetFrameSize()
9197
h1, w1 := baseStyle.GetFrameSize()
@@ -106,13 +112,9 @@ func (l listVariables) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
106112
}
107113
case tea.KeyMsg:
108114
switch msg.String() {
109-
case "esc":
110-
if l.table.Focused() {
111-
l.table.Blur()
112-
} else {
113-
l.table.Focus()
114-
}
115-
case "q", "ctrl+c", "ctrl+d":
115+
case "q", "esc":
116+
return state.PreviousCommand(), nil
117+
case "ctrl+c", "ctrl+d":
116118
return l, tea.Quit
117119
case "enter":
118120
content := ""
@@ -128,12 +130,11 @@ func (l listVariables) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
128130
content += s
129131
}
130132
title := strings.Join(l.table.SelectedRow()[:3], " ")
131-
f := NewFileView(content, title, func() (tea.Model, tea.Cmd) {
132-
return l, cmd
133-
})
134-
return f, f.Init()
133+
state.setCurrentCommand(l)
134+
return state.NextCommand(), tea.Batch(sendTitleCmd(title), sendContentCmd(content))
135135
}
136136
}
137+
var cmd tea.Cmd
137138
l.table, cmd = l.table.Update(msg)
138139
return l, cmd
139140
}

0 commit comments

Comments
 (0)