add cmd line path args

This commit is contained in:
2026-01-03 17:19:55 +00:00
parent 2da6d92f72
commit cfd302ba0e
2 changed files with 29 additions and 11 deletions
+4 -1
View File
@@ -278,7 +278,10 @@ func (t *FsTree) renderNode(node *FsNode, depth int, builder *strings.Builder, h
}
line := icon + " " + fileName + "\n"
builder.WriteString(line)
// the depth 0 is the dummy root node, I don't render that
if depth > 0 {
builder.WriteString(line)
}
*currentLine++
if node.expanded {
+25 -10
View File
@@ -39,6 +39,7 @@ type model struct {
terminalWidth int
terminalHeight int
tree *fs.FsTree
rootPath string // path to load the tree from
// spinner needs to be state as I need to update the spinner on
// each tick in update func
spinner spinner.Model
@@ -47,7 +48,7 @@ type model struct {
}
// initial model state
func createModel() model {
func createModel(rootPath string) model {
s := spinner.New()
s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
@@ -57,6 +58,7 @@ func createModel() model {
terminalWidth: 0,
terminalHeight: 0,
tree: nil,
rootPath: rootPath,
spinner: s,
loading: true,
viewport: viewport.New(0, 0),
@@ -70,18 +72,25 @@ type treeLoadedMsg struct {
tree *fs.FsTree
}
func loadTreeCmd() tea.Msg {
cwd, err := os.Getwd()
if err != nil {
fmt.Println("Error getting cwd:", err)
os.Exit(1)
func loadTreeCmd(path string) tea.Cmd {
return func() tea.Msg {
var targetPath string
if path == "" {
cwd, err := os.Getwd()
if err != nil {
fmt.Println("Error getting cwd:", err)
os.Exit(1)
}
targetPath = cwd
} else {
targetPath = path
}
return treeLoadedMsg{tree: fs.NewFsTree(targetPath)}
}
return treeLoadedMsg{tree: fs.NewFsTree(cwd)}
}
func (m model) Init() tea.Cmd {
return tea.Batch(m.spinner.Tick, loadTreeCmd)
return tea.Batch(m.spinner.Tick, loadTreeCmd(m.rootPath))
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
@@ -161,8 +170,14 @@ func (m model) View() string {
// =================== bubbletea ui fns ===================
func main() {
var rootPath string
if len(os.Args) > 1 {
rootPath = os.Args[1]
}
// if rootPath is empty, createModel will use cwd
p := tea.NewProgram(
createModel(),
createModel(rootPath),
tea.WithAltScreen(), // full screen tui
tea.WithMouseAllMotion(),
)