allow toggling on/off file tree mode

This commit is contained in:
Jesse Duffield
2021-03-21 08:41:06 +11:00
parent c27cea6f30
commit da6fe01eca
14 changed files with 175 additions and 58 deletions

View File

@@ -239,7 +239,7 @@ func (gui *Gui) handleFilePress() error {
return gui.surfaceError(err)
}
} else {
if err := gui.GitCommand.UnStageFile(file.Name, file.Tracked); err != nil {
if err := gui.GitCommand.UnStageFile(file.Names(), file.Tracked); err != nil {
return gui.surfaceError(err)
}
}
@@ -250,7 +250,7 @@ func (gui *Gui) handleFilePress() error {
}
} else {
// pretty sure it doesn't matter that we're always passing true here
if err := gui.GitCommand.UnStageFile(node.Path, true); err != nil {
if err := gui.GitCommand.UnStageFile([]string{node.Path}, true); err != nil {
return gui.surfaceError(err)
}
}
@@ -307,7 +307,7 @@ func (gui *Gui) handleIgnoreFile() error {
unstageFiles := func() error {
return node.ForEachFile(func(file *models.File) error {
if file.HasStagedChanges {
if err := gui.GitCommand.UnStageFile(file.Name, file.Tracked); err != nil {
if err := gui.GitCommand.UnStageFile(file.Names(), file.Tracked); err != nil {
return err
}
}
@@ -526,8 +526,8 @@ func (gui *Gui) refreshStateFiles() error {
prevSelectedLineIdx := gui.State.Panels.Files.SelectedLineIdx
// get files to stage
noRenames := gui.State.StatusLineManager.TreeMode
files := gui.GitCommand.GetStatusFiles(commands.GetStatusFileOptions{NoRenames: noRenames})
// noRenames := gui.State.StatusLineManager.TreeMode
files := gui.GitCommand.GetStatusFiles(commands.GetStatusFileOptions{})
gui.State.StatusLineManager.SetFiles(
gui.GitCommand.MergeStatusFiles(gui.State.StatusLineManager.GetAllFiles(), files, selectedFile),
)
@@ -800,3 +800,30 @@ func (gui *Gui) handleToggleDirCollapsed() error {
return nil
}
func (gui *Gui) handleToggleFileTreeView() error {
// get path of currently selected file
node := gui.getSelectedStatusNode()
path := ""
if node != nil {
path = node.Path
}
gui.State.StatusLineManager.TreeMode = !gui.State.StatusLineManager.TreeMode
gui.State.StatusLineManager.SetTree()
// find that same node in the new format and move the cursor to it
if path != "" {
index, found := gui.State.StatusLineManager.GetIndexForPath(path)
if found {
gui.filesListContext().GetPanelState().SetSelectedLineIdx(index)
}
}
if gui.getFilesView().Context == FILES_CONTEXT_KEY {
if err := gui.Contexts.Files.Context.HandleRender(); err != nil {
return err
}
}
return nil
}

View File

@@ -505,6 +505,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
Description: gui.Tr.LcViewResetToUpstreamOptions,
OpensMenu: true,
},
{
ViewName: "files",
Contexts: []string{FILES_CONTEXT_KEY},
Key: gui.getKey(config.Files.ToggleTreeView),
Handler: gui.wrappedHandler(gui.handleToggleFileTreeView),
Description: gui.Tr.LcToggleTreeView,
},
{
ViewName: "branches",
Contexts: []string{LOCAL_BRANCHES_CONTEXT_KEY},

View File

@@ -2,6 +2,7 @@ package gui
import (
"fmt"
"sort"
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/models"
@@ -24,23 +25,19 @@ func NewStatusLineManager(files []*models.File, log *logrus.Entry) *StatusLineMa
return &StatusLineManager{
Files: files,
Log: log,
TreeMode: true, // always true for now
TreeMode: false, // always true for now
CollapsedPaths: map[string]bool{},
}
}
func (m *StatusLineManager) GetItemAtIndex(index int) *models.StatusLineNode {
if m.TreeMode {
// need to traverse the three depth first until we get to the index.
return m.Tree.GetNodeAtIndex(index+1, m.CollapsedPaths) // ignoring root
}
// need to traverse the three depth first until we get to the index.
return m.Tree.GetNodeAtIndex(index+1, m.CollapsedPaths) // ignoring root
}
m.Log.Warn(index)
if index > len(m.Files)-1 {
return nil
}
return &models.StatusLineNode{File: m.Files[index]}
func (m *StatusLineManager) GetIndexForPath(path string) (int, bool) {
index, found := m.Tree.GetIndexForPath(path, m.CollapsedPaths)
return index - 1, found
}
func (m *StatusLineManager) GetAllItems() []*models.StatusLineNode {
@@ -57,7 +54,19 @@ func (m *StatusLineManager) GetAllFiles() []*models.File {
func (m *StatusLineManager) SetFiles(files []*models.File) {
m.Files = files
m.Tree = GetTreeFromStatusFiles(files)
sort.SliceStable(m.Files, func(i, j int) bool {
return m.Files[i].Name < m.Files[j].Name
})
m.SetTree()
}
func (m *StatusLineManager) SetTree() {
if m.TreeMode {
m.Tree = GetTreeFromStatusFiles(m.Files, m.Log)
} else {
m.Tree = GetFlatTreeFromStatusFiles(m.Files)
}
}
func (m *StatusLineManager) Render(diffName string, submoduleConfigs []*models.SubmoduleConfig) []string {
@@ -84,7 +93,7 @@ func (m *StatusLineManager) renderAux(s *models.StatusLineNode, prefix string, d
}
getLine := func() string {
return prefix + presentation.GetStatusNodeLine(s.GetHasUnstagedChanges(), s.GetHasStagedChanges(), s.Name, diffName, submoduleConfigs, s.File)
return prefix + presentation.GetStatusNodeLine(s.GetHasUnstagedChanges(), s.GetHasStagedChanges(), s.NameAtDepth(depth), diffName, submoduleConfigs, s.File)
}
if s.IsLeaf() {

View File

@@ -3,38 +3,39 @@ package gui
import (
"os"
"path/filepath"
"sort"
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/sirupsen/logrus"
)
func GetTreeFromStatusFiles(files []*models.File) *models.StatusLineNode {
func GetTreeFromStatusFiles(files []*models.File, log *logrus.Entry) *models.StatusLineNode {
root := &models.StatusLineNode{}
sort.SliceStable(files, func(i, j int) bool {
return files[i].Name < files[j].Name
})
var curr *models.StatusLineNode
for _, file := range files {
split := strings.Split(file.Name, string(os.PathSeparator))
curr = root
outer:
for i, dir := range split {
for i := range split {
var setFile *models.File
if i == len(split)-1 {
isFile := i == len(split)-1
if isFile {
setFile = file
}
path := filepath.Join(split[:i+1]...)
for _, existingChild := range curr.Children {
if existingChild.Name == dir {
if existingChild.Path == path {
curr = existingChild
continue outer
}
}
newChild := &models.StatusLineNode{
Name: dir,
Path: filepath.Join(split[:i+1]...),
Name: path, // TODO: Remove concept of name
Path: path,
File: setFile,
}
curr.Children = append(curr.Children, newChild)
@@ -48,3 +49,16 @@ func GetTreeFromStatusFiles(files []*models.File) *models.StatusLineNode {
return root
}
func GetFlatTreeFromStatusFiles(files []*models.File) *models.StatusLineNode {
root := &models.StatusLineNode{}
for _, file := range files {
root.Children = append(root.Children, &models.StatusLineNode{
Name: file.Name,
Path: file.GetPath(),
File: file,
})
}
return root
}

View File

@@ -110,7 +110,7 @@ func (gui *Gui) fileForSubmodule(submodule *models.SubmoduleConfig) *models.File
func (gui *Gui) resetSubmodule(submodule *models.SubmoduleConfig) error {
file := gui.fileForSubmodule(submodule)
if file != nil {
if err := gui.GitCommand.UnStageFile(file.Name, file.Tracked); err != nil {
if err := gui.GitCommand.UnStageFile(file.Names(), file.Tracked); err != nil {
return gui.surfaceError(err)
}
}