remove more redundant code
This commit is contained in:
+24
-65
@@ -11,7 +11,6 @@ import (
|
||||
"errors"
|
||||
"mend/styles"
|
||||
"mend/utils"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
@@ -27,27 +26,6 @@ const (
|
||||
FolderNode
|
||||
)
|
||||
|
||||
// ==================== Message types ====================
|
||||
type Msg interface{}
|
||||
|
||||
type MsgMoveUp struct{}
|
||||
type MsgMoveDown struct{}
|
||||
type MsgToggleExpand struct{}
|
||||
type MsgLeftClickLine struct {
|
||||
Line int
|
||||
}
|
||||
type MsgCreateNode struct {
|
||||
Parent *FsNode
|
||||
Name string
|
||||
NodeType FsNodeType
|
||||
}
|
||||
type MsgDeleteNode struct {
|
||||
Node *FsNode
|
||||
}
|
||||
type MsgHover struct {
|
||||
Line int
|
||||
}
|
||||
|
||||
// ==================== FsNode definition ====================
|
||||
// a single node, Fs => deals with file system related info mostly
|
||||
// a name for example can be content derived as well
|
||||
@@ -82,26 +60,28 @@ func (t *FsTree) Init() tea.Cmd {
|
||||
|
||||
func (t *FsTree) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch m := msg.(type) {
|
||||
case MsgMoveUp:
|
||||
_ = t.MoveUp()
|
||||
case MsgMoveDown:
|
||||
_ = t.MoveDown()
|
||||
case MsgToggleExpand:
|
||||
_ = t.ToggleSelectedExpand()
|
||||
case MsgLeftClickLine:
|
||||
nodeAtLine := t.lines[m.Line]
|
||||
if nodeAtLine != nil {
|
||||
t.selectedNode = nodeAtLine
|
||||
if nodeAtLine.nodeType == FolderNode {
|
||||
_ = t.ToggleExpand(nodeAtLine)
|
||||
case tea.KeyMsg:
|
||||
switch m.String() {
|
||||
case "up", "k":
|
||||
_ = t.MoveUp()
|
||||
case "down", "j":
|
||||
_ = t.MoveDown()
|
||||
case "enter", " ":
|
||||
_ = t.ToggleSelectedExpand()
|
||||
}
|
||||
case tea.MouseMsg:
|
||||
t.hoveredNode = t.lines[m.Y] // hover
|
||||
|
||||
// click
|
||||
if m.Button == tea.MouseButtonLeft && m.Action == tea.MouseActionPress {
|
||||
nodeAtLine := t.lines[m.Y]
|
||||
if nodeAtLine != nil {
|
||||
t.selectedNode = nodeAtLine
|
||||
if nodeAtLine.nodeType == FolderNode {
|
||||
_ = t.ToggleExpand(nodeAtLine)
|
||||
}
|
||||
}
|
||||
}
|
||||
case MsgCreateNode:
|
||||
_ = t.CreateNode(m.Parent, m.Name, m.NodeType)
|
||||
case MsgDeleteNode:
|
||||
_ = t.DeleteNode(m.Node)
|
||||
case MsgHover:
|
||||
t.hoveredNode = t.lines[m.Line]
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
@@ -125,16 +105,11 @@ func NewFsTree(rootPath string) *FsTree {
|
||||
// but since I use a map, it's easy to add
|
||||
walkFileSystemAndBuildTree(rootPath, root)
|
||||
|
||||
var tree *FsTree
|
||||
tree := &FsTree{
|
||||
root: root,
|
||||
}
|
||||
if len(root.children) > 0 {
|
||||
tree = &FsTree{
|
||||
root: root,
|
||||
selectedNode: root.children[0],
|
||||
}
|
||||
} else {
|
||||
tree = &FsTree{
|
||||
root: root,
|
||||
}
|
||||
tree.selectedNode = root.children[0]
|
||||
}
|
||||
tree.buildLines()
|
||||
return tree
|
||||
@@ -227,22 +202,6 @@ func (t *FsTree) ToggleSelectedExpand() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *FsTree) GetSelectedContent() (string, error) {
|
||||
if t.selectedNode == nil {
|
||||
return "", errors.New("no node is currently selected")
|
||||
}
|
||||
|
||||
if t.selectedNode.nodeType != FileNode {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
contentBytes, err := os.ReadFile(t.selectedNode.path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(contentBytes), nil
|
||||
}
|
||||
|
||||
func (t *FsTree) renderNode(node *FsNode, depth int, builder *strings.Builder) {
|
||||
if node == nil {
|
||||
return
|
||||
|
||||
@@ -111,48 +111,18 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
return m, cmd
|
||||
case tea.MouseMsg:
|
||||
if m.tree != nil {
|
||||
// track hover for all mouse events within file tree
|
||||
// pass mouse events within file tree bounds to tree
|
||||
if msg.X < m.width {
|
||||
m.tree.Update(fs.MsgHover{Line: msg.Y})
|
||||
} else {
|
||||
m.tree.Update(fs.MsgHover{Line: -1})
|
||||
}
|
||||
|
||||
if msg.Button == tea.MouseButtonLeft && msg.Action == tea.MouseActionPress {
|
||||
// within file tree
|
||||
if msg.X < m.width {
|
||||
m.tree.Update(fs.MsgLeftClickLine{Line: msg.Y})
|
||||
content, err := m.tree.GetSelectedContent()
|
||||
if err == nil {
|
||||
m.viewport.SetContent(content)
|
||||
}
|
||||
}
|
||||
m.tree.Update(msg)
|
||||
}
|
||||
}
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "q", "ctrl+c":
|
||||
return m, tea.Quit
|
||||
case "up", "k":
|
||||
case "up", "k", "down", "j", "enter", " ":
|
||||
if m.tree != nil {
|
||||
m.tree.Update(fs.MsgMoveUp{})
|
||||
}
|
||||
case "down", "j":
|
||||
if m.tree != nil {
|
||||
m.tree.Update(fs.MsgMoveDown{})
|
||||
}
|
||||
case "enter", " ":
|
||||
if m.tree != nil {
|
||||
m.tree.Update(fs.MsgToggleExpand{})
|
||||
}
|
||||
case "l", "L":
|
||||
if m.tree != nil {
|
||||
content, err := m.tree.GetSelectedContent()
|
||||
if err != nil {
|
||||
fmt.Println("Error getting selected content:", err)
|
||||
} else {
|
||||
m.viewport.SetContent(content)
|
||||
}
|
||||
m.tree.Update(msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -175,7 +145,6 @@ func main() {
|
||||
rootPath = os.Args[1]
|
||||
}
|
||||
// if rootPath is empty, createModel will use cwd
|
||||
|
||||
p := tea.NewProgram(
|
||||
createModel(rootPath),
|
||||
tea.WithAltScreen(), // full screen tui
|
||||
|
||||
Reference in New Issue
Block a user