remove more redundant code
This commit is contained in:
+24
-65
@@ -11,7 +11,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"mend/styles"
|
"mend/styles"
|
||||||
"mend/utils"
|
"mend/utils"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -27,27 +26,6 @@ const (
|
|||||||
FolderNode
|
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 ====================
|
// ==================== FsNode definition ====================
|
||||||
// a single node, Fs => deals with file system related info mostly
|
// a single node, Fs => deals with file system related info mostly
|
||||||
// a name for example can be content derived as well
|
// 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) {
|
func (t *FsTree) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
switch m := msg.(type) {
|
switch m := msg.(type) {
|
||||||
case MsgMoveUp:
|
case tea.KeyMsg:
|
||||||
_ = t.MoveUp()
|
switch m.String() {
|
||||||
case MsgMoveDown:
|
case "up", "k":
|
||||||
_ = t.MoveDown()
|
_ = t.MoveUp()
|
||||||
case MsgToggleExpand:
|
case "down", "j":
|
||||||
_ = t.ToggleSelectedExpand()
|
_ = t.MoveDown()
|
||||||
case MsgLeftClickLine:
|
case "enter", " ":
|
||||||
nodeAtLine := t.lines[m.Line]
|
_ = t.ToggleSelectedExpand()
|
||||||
if nodeAtLine != nil {
|
}
|
||||||
t.selectedNode = nodeAtLine
|
case tea.MouseMsg:
|
||||||
if nodeAtLine.nodeType == FolderNode {
|
t.hoveredNode = t.lines[m.Y] // hover
|
||||||
_ = t.ToggleExpand(nodeAtLine)
|
|
||||||
|
// 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
|
return t, nil
|
||||||
}
|
}
|
||||||
@@ -125,16 +105,11 @@ func NewFsTree(rootPath string) *FsTree {
|
|||||||
// but since I use a map, it's easy to add
|
// but since I use a map, it's easy to add
|
||||||
walkFileSystemAndBuildTree(rootPath, root)
|
walkFileSystemAndBuildTree(rootPath, root)
|
||||||
|
|
||||||
var tree *FsTree
|
tree := &FsTree{
|
||||||
|
root: root,
|
||||||
|
}
|
||||||
if len(root.children) > 0 {
|
if len(root.children) > 0 {
|
||||||
tree = &FsTree{
|
tree.selectedNode = root.children[0]
|
||||||
root: root,
|
|
||||||
selectedNode: root.children[0],
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
tree = &FsTree{
|
|
||||||
root: root,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
tree.buildLines()
|
tree.buildLines()
|
||||||
return tree
|
return tree
|
||||||
@@ -227,22 +202,6 @@ func (t *FsTree) ToggleSelectedExpand() error {
|
|||||||
return nil
|
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) {
|
func (t *FsTree) renderNode(node *FsNode, depth int, builder *strings.Builder) {
|
||||||
if node == nil {
|
if node == nil {
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -111,48 +111,18 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
return m, cmd
|
return m, cmd
|
||||||
case tea.MouseMsg:
|
case tea.MouseMsg:
|
||||||
if m.tree != nil {
|
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 {
|
if msg.X < m.width {
|
||||||
m.tree.Update(fs.MsgHover{Line: msg.Y})
|
m.tree.Update(msg)
|
||||||
} 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
case "q", "ctrl+c":
|
case "q", "ctrl+c":
|
||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
case "up", "k":
|
case "up", "k", "down", "j", "enter", " ":
|
||||||
if m.tree != nil {
|
if m.tree != nil {
|
||||||
m.tree.Update(fs.MsgMoveUp{})
|
m.tree.Update(msg)
|
||||||
}
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -175,7 +145,6 @@ func main() {
|
|||||||
rootPath = os.Args[1]
|
rootPath = os.Args[1]
|
||||||
}
|
}
|
||||||
// if rootPath is empty, createModel will use cwd
|
// if rootPath is empty, createModel will use cwd
|
||||||
|
|
||||||
p := tea.NewProgram(
|
p := tea.NewProgram(
|
||||||
createModel(rootPath),
|
createModel(rootPath),
|
||||||
tea.WithAltScreen(), // full screen tui
|
tea.WithAltScreen(), // full screen tui
|
||||||
|
|||||||
Reference in New Issue
Block a user