Skip to content

Commit

Permalink
add tran tui init function in internal/tui/init.go
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed Feb 4, 2022
1 parent a11a322 commit b1f66ab
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions internal/tui/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package tui

import (
"os"
"log"
"strings"
"path/filepath"

"github.com/spf13/viper"
"github.com/abdfnx/tran/dfs"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
)

// Init initializes the UI and sets up initial data.
func (b Bubble) Init() tea.Cmd {
var cmds []tea.Cmd
startDir := viper.GetString("start-dir")

switch {
case startDir != "":
_, err := os.Stat(startDir)
if err != nil {
return nil
}

if strings.HasPrefix(startDir, dfs.RootDirectory) {
cmds = append(cmds, b.updateDirectoryListingCmd(startDir))
} else {
path, err := os.Getwd()

if err != nil {
log.Fatal(err)
}

filePath := filepath.Join(path, startDir)

cmds = append(cmds, b.updateDirectoryListingCmd(filePath))
}

case b.appConfig.Tran.StartDir == dfs.HomeDirectory:
homeDir, err := dfs.GetHomeDirectory()
if err != nil {
log.Fatal(err)
}

cmds = append(cmds, b.updateDirectoryListingCmd(homeDir))

default:
cmds = append(cmds, b.updateDirectoryListingCmd(b.appConfig.Tran.StartDir))
}

cmds = append(cmds, spinner.Tick)
cmds = append(cmds, textinput.Blink)

return tea.Batch(cmds...)
}

0 comments on commit b1f66ab

Please # to comment.