Skip to content

Commit 5a5291a

Browse files
committed
feat: Allow specifying multiple panels at startup
1 parent 9653d8a commit 5a5291a

File tree

6 files changed

+61
-49
lines changed

6 files changed

+61
-49
lines changed

src/cmd/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ func Run(content embed.FS) {
8484
},
8585
Action: func(c *cli.Context) error {
8686
// If no args are called along with "spf" use current dir
87-
path := ""
87+
firstFilePanelDirs := []string{""}
8888
if c.Args().Present() {
89-
path = c.Args().First()
89+
firstFilePanelDirs = c.Args().Slice()
9090
}
9191

9292
variable.UpdateVarFromCliArgs(c)
@@ -100,7 +100,7 @@ func Run(content embed.FS) {
100100

101101
firstUse := checkFirstUse()
102102

103-
p := tea.NewProgram(internal.InitialModel(path, firstUse, hasTrash), tea.WithAltScreen(), tea.WithMouseCellMotion())
103+
p := tea.NewProgram(internal.InitialModel(firstFilePanelDirs, firstUse, hasTrash), tea.WithAltScreen(), tea.WithMouseCellMotion())
104104
if _, err := p.Run(); err != nil {
105105
utils.PrintfAndExit("Alas, there's been an error: %v", err)
106106
}

src/internal/config_function.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
// initialConfig load and handle all configuration files (spf config,Hotkeys
1919
// themes) setted up. Returns absolute path of dir pointing to the file Panel
2020

21-
func initialConfig(dir string) (toggleDotFile bool, toggleFooter bool, firstFilePanelDir string) { //nolint: nonamedreturns // This is the only usecase of named returns, distinguish between multiple return values
21+
func initialConfig(firstFilePanelDirs []string) (toggleDotFile bool, toggleFooter bool) { //nolint: nonamedreturns // This is the only usecase of named returns, distinguish between multiple return values
2222
// Open log stream
2323
file, err := os.OpenFile(variable.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
2424

@@ -54,29 +54,29 @@ func initialConfig(dir string) (toggleDotFile bool, toggleFooter bool, firstFile
5454
}
5555
}
5656

57-
// spf was not called with an argument
58-
firstFilePanelDir = dir
59-
if firstFilePanelDir == "" {
60-
firstFilePanelDir = common.Config.DefaultDirectory
61-
}
57+
for i := range firstFilePanelDirs {
58+
if firstFilePanelDirs[i] == "" {
59+
firstFilePanelDirs[i] = common.Config.DefaultDirectory
60+
}
6261

63-
if strings.HasPrefix(firstFilePanelDir, "~") {
64-
// We only need to replace the first ~ , not all of them
65-
// And only if its a prefix
66-
firstFilePanelDir = strings.Replace(firstFilePanelDir, "~", variable.HomeDir, 1)
67-
}
68-
firstFilePanelDir, err = filepath.Abs(firstFilePanelDir)
69-
// In case of unexpected path error, fallback to home dir
70-
if err != nil {
71-
slog.Error("Unexpected error while calculating firstFilePanelDir", "error", err)
72-
firstFilePanelDir = variable.HomeDir
62+
if strings.HasPrefix(firstFilePanelDirs[i], "~") {
63+
// We only need to replace the first ~ , not all of them
64+
// And only if its a prefix
65+
firstFilePanelDirs[i] = strings.Replace(firstFilePanelDirs[i], "~", variable.HomeDir, 1)
66+
}
67+
firstFilePanelDirs[i], err = filepath.Abs(firstFilePanelDirs[i])
68+
// In case of unexpected path error, fallback to home dir
69+
if err != nil {
70+
slog.Error("Unexpected error while calculating firstFilePanelDir", "error", err)
71+
firstFilePanelDirs[i] = variable.HomeDir
72+
}
7373
}
7474

7575
slog.Debug("Runtime information", "runtime.GOOS", runtime.GOOS,
76-
"start directory", firstFilePanelDir)
76+
"start directories", firstFilePanelDirs)
7777

7878
toggleDotFile = utils.ReadBoolFile(variable.ToggleDotFile, false)
7979
toggleFooter = utils.ReadBoolFile(variable.ToggleFooter, true)
8080

81-
return toggleDotFile, toggleFooter, firstFilePanelDir
81+
return toggleDotFile, toggleFooter
8282
}

src/internal/default_config.go

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
// Generate and return model containing default configurations for interface
11-
func defaultModelConfig(toggleDotFile bool, toggleFooter bool, firstFilePanelDir string) model {
11+
func defaultModelConfig(toggleDotFile bool, toggleFooter bool, firstFilePanelDirs []string) model {
1212
return model{
1313
filePanelFocusIndex: 0,
1414
focusPanel: nonePanelFocus,
@@ -23,28 +23,7 @@ func defaultModelConfig(toggleDotFile bool, toggleFooter bool, firstFilePanelDir
2323
searchBar: common.GenerateSearchBar(),
2424
},
2525
fileModel: fileModel{
26-
filePanels: []filePanel{
27-
{
28-
render: 0,
29-
cursor: 0,
30-
location: firstFilePanelDir,
31-
sortOptions: sortOptionsModel{
32-
width: 20,
33-
height: 4,
34-
open: false,
35-
cursor: common.Config.DefaultSortType,
36-
data: sortOptionsModelData{
37-
options: []string{"Name", "Size", "Date Modified"},
38-
selected: common.Config.DefaultSortType,
39-
reversed: common.Config.SortOrderReversed,
40-
},
41-
},
42-
panelMode: browserMode,
43-
focusType: focus,
44-
directoryRecords: make(map[string]directoryRecord),
45-
searchBar: common.GenerateSearchBar(),
46-
},
47-
},
26+
filePanels: filePanelSlice(firstFilePanelDirs),
4827
filePreview: filePreviewPanel{
4928
open: common.Config.DefaultOpenFilePreview,
5029
},

src/internal/model.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ var progressBarLastRenderTime = time.Now() //nolint: gochec
3737
// is passed to tea.NewProgram() which accepts tea.Model
3838
// Either way type 'model' is not exported, so there is not way main package can
3939
// be aware of it, and use it directly
40-
func InitialModel(dir string, firstUseCheck, hasTrashCheck bool) tea.Model {
41-
toggleDotFile, toggleFooter, firstFilePanelDir := initialConfig(dir)
40+
func InitialModel(firstFilePanelDirs []string, firstUseCheck, hasTrashCheck bool) tea.Model {
41+
toggleDotFile, toggleFooter := initialConfig(firstFilePanelDirs)
4242
firstUse = firstUseCheck
4343
hasTrash = hasTrashCheck
4444
batCmd = checkBatCmd()
45-
return defaultModelConfig(toggleDotFile, toggleFooter, firstFilePanelDir)
45+
return defaultModelConfig(toggleDotFile, toggleFooter, firstFilePanelDirs)
4646
}
4747

4848
// Init function to be called by Bubble tea framework, sets windows title,

src/internal/model_prompt_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ func TestModel_Update_Prompt(t *testing.T) {
7070
// too big Shell command output
7171

7272
t.Run("Basic Prompt Opening", func(t *testing.T) {
73-
m := defaultModelConfig(false, false, "/")
73+
m := defaultModelConfig(false, false, []string{"/"})
7474
firstUse = false
7575
_, err := TeaUpdate(&m, utils.TeaRuneKeyMsg(common.Hotkeys.OpenCommandLine[0]))
7676
require.NoError(t, err, "Opening the prompt should not produce an error")
7777
assert.True(t, m.promptModal.IsOpen())
7878
})
7979

8080
t.Run("Split Panel", func(t *testing.T) {
81-
m := defaultModelConfig(false, false, "/")
81+
m := defaultModelConfig(false, false, []string{"/"})
8282
firstUse = false
8383
assert.Len(t, m.fileModel.filePanels, 1)
8484
_, _ = TeaUpdate(&m, utils.TeaRuneKeyMsg(common.Hotkeys.OpenSPFPrompt[0]))

src/internal/type_utils.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,39 @@ func (d directory) requiredHeight() int {
4444
return 1
4545
}
4646

47+
// ================ filepanel
48+
49+
func filePanelSlice(dir []string) []filePanel {
50+
res := make([]filePanel, len(dir))
51+
for i := range dir {
52+
res[i] = defaultFilePanel(dir[i])
53+
}
54+
return res
55+
}
56+
57+
func defaultFilePanel(dir string) filePanel {
58+
return filePanel{
59+
render: 0,
60+
cursor: 0,
61+
location: dir,
62+
sortOptions: sortOptionsModel{
63+
width: 20,
64+
height: 4,
65+
open: false,
66+
cursor: common.Config.DefaultSortType,
67+
data: sortOptionsModelData{
68+
options: []string{"Name", "Size", "Date Modified"},
69+
selected: common.Config.DefaultSortType,
70+
reversed: common.Config.SortOrderReversed,
71+
},
72+
},
73+
panelMode: browserMode,
74+
focusType: focus,
75+
directoryRecords: make(map[string]directoryRecord),
76+
searchBar: common.GenerateSearchBar(),
77+
}
78+
}
79+
4780
// ================ String method for easy logging =====================
4881

4982
func (f focusPanelType) String() string {

0 commit comments

Comments
 (0)