add cmd line path args
This commit is contained in:
+4
-1
@@ -278,7 +278,10 @@ func (t *FsTree) renderNode(node *FsNode, depth int, builder *strings.Builder, h
|
|||||||
}
|
}
|
||||||
|
|
||||||
line := icon + " " + fileName + "\n"
|
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++
|
*currentLine++
|
||||||
|
|
||||||
if node.expanded {
|
if node.expanded {
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ type model struct {
|
|||||||
terminalWidth int
|
terminalWidth int
|
||||||
terminalHeight int
|
terminalHeight int
|
||||||
tree *fs.FsTree
|
tree *fs.FsTree
|
||||||
|
rootPath string // path to load the tree from
|
||||||
// spinner needs to be state as I need to update the spinner on
|
// spinner needs to be state as I need to update the spinner on
|
||||||
// each tick in update func
|
// each tick in update func
|
||||||
spinner spinner.Model
|
spinner spinner.Model
|
||||||
@@ -47,7 +48,7 @@ type model struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// initial model state
|
// initial model state
|
||||||
func createModel() model {
|
func createModel(rootPath string) model {
|
||||||
s := spinner.New()
|
s := spinner.New()
|
||||||
s.Spinner = spinner.Dot
|
s.Spinner = spinner.Dot
|
||||||
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
|
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
|
||||||
@@ -57,6 +58,7 @@ func createModel() model {
|
|||||||
terminalWidth: 0,
|
terminalWidth: 0,
|
||||||
terminalHeight: 0,
|
terminalHeight: 0,
|
||||||
tree: nil,
|
tree: nil,
|
||||||
|
rootPath: rootPath,
|
||||||
spinner: s,
|
spinner: s,
|
||||||
loading: true,
|
loading: true,
|
||||||
viewport: viewport.New(0, 0),
|
viewport: viewport.New(0, 0),
|
||||||
@@ -70,18 +72,25 @@ type treeLoadedMsg struct {
|
|||||||
tree *fs.FsTree
|
tree *fs.FsTree
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadTreeCmd() tea.Msg {
|
func loadTreeCmd(path string) tea.Cmd {
|
||||||
|
return func() tea.Msg {
|
||||||
cwd, err := os.Getwd()
|
var targetPath string
|
||||||
if err != nil {
|
if path == "" {
|
||||||
fmt.Println("Error getting cwd:", err)
|
cwd, err := os.Getwd()
|
||||||
os.Exit(1)
|
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 {
|
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) {
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
@@ -161,8 +170,14 @@ func (m model) View() string {
|
|||||||
// =================== bubbletea ui fns ===================
|
// =================== bubbletea ui fns ===================
|
||||||
|
|
||||||
func main() {
|
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(
|
p := tea.NewProgram(
|
||||||
createModel(),
|
createModel(rootPath),
|
||||||
tea.WithAltScreen(), // full screen tui
|
tea.WithAltScreen(), // full screen tui
|
||||||
tea.WithMouseAllMotion(),
|
tea.WithMouseAllMotion(),
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user