1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00

support file tree mode on windows

This commit is contained in:
Jesse Duffield 2021-04-08 21:24:49 +10:00
parent f89c47b83d
commit 21049be233
5 changed files with 28 additions and 33 deletions

View File

@ -1,8 +1,6 @@
package filetree package filetree
import ( import (
"os"
"path/filepath"
"sort" "sort"
"strings" "strings"
@ -14,18 +12,17 @@ func BuildTreeFromFiles(files []*models.File) *FileNode {
var curr *FileNode var curr *FileNode
for _, file := range files { for _, file := range files {
split := strings.Split(file.Name, string(os.PathSeparator)) splitPath := split(file.Name)
curr = root curr = root
outer: outer:
for i := range split { for i := range splitPath {
var setFile *models.File var setFile *models.File
isFile := i == len(split)-1 isFile := i == len(splitPath)-1
if isFile { if isFile {
setFile = file setFile = file
} }
path := filepath.Join(split[:i+1]...) path := join(splitPath[:i+1])
for _, existingChild := range curr.Children { for _, existingChild := range curr.Children {
if existingChild.Path == path { if existingChild.Path == path {
curr = existingChild curr = existingChild
@ -61,17 +58,17 @@ func BuildTreeFromCommitFiles(files []*models.CommitFile) *CommitFileNode {
var curr *CommitFileNode var curr *CommitFileNode
for _, file := range files { for _, file := range files {
split := strings.Split(file.Name, string(os.PathSeparator)) splitPath := split(file.Name)
curr = root curr = root
outer: outer:
for i := range split { for i := range splitPath {
var setFile *models.CommitFile var setFile *models.CommitFile
isFile := i == len(split)-1 isFile := i == len(splitPath)-1
if isFile { if isFile {
setFile = file setFile = file
} }
path := filepath.Join(split[:i+1]...) path := join(splitPath[:i+1])
for _, existingChild := range curr.Children { for _, existingChild := range curr.Children {
if existingChild.Path == path { if existingChild.Path == path {
@ -108,3 +105,11 @@ func BuildFlatTreeFromFiles(files []*models.File) *FileNode {
return &FileNode{Children: sortedFiles} return &FileNode{Children: sortedFiles}
} }
func split(str string) []string {
return strings.Split(str, "/")
}
func join(strs []string) string {
return strings.Join(strs, "/")
}

View File

@ -1,17 +1,12 @@
package filetree package filetree
import (
"os"
"strings"
)
type CollapsedPaths map[string]bool type CollapsedPaths map[string]bool
func (cp CollapsedPaths) ExpandToPath(path string) { func (cp CollapsedPaths) ExpandToPath(path string) {
// need every directory along the way // need every directory along the way
split := strings.Split(path, string(os.PathSeparator)) splitPath := split(path)
for i := range split { for i := range splitPath {
dir := strings.Join(split[0:i+1], string(os.PathSeparator)) dir := join(splitPath[0 : i+1])
cp[dir] = false cp[dir] = false
} }
} }

View File

@ -1,10 +1,6 @@
package filetree package filetree
import ( import (
"os"
"path/filepath"
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
) )
@ -169,8 +165,8 @@ func (s *CommitFileNode) AnyFile(test func(file *models.CommitFile) bool) bool {
} }
func (s *CommitFileNode) NameAtDepth(depth int) string { func (s *CommitFileNode) NameAtDepth(depth int) string {
splitName := strings.Split(s.Path, string(os.PathSeparator)) splitName := split(s.Path)
name := filepath.Join(splitName[depth:]...) name := join(splitName[depth:])
return name return name
} }

View File

@ -2,9 +2,6 @@ package filetree
import ( import (
"fmt" "fmt"
"os"
"path/filepath"
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
) )
@ -171,18 +168,18 @@ func (s *FileNode) AnyFile(test func(file *models.File) bool) bool {
} }
func (s *FileNode) NameAtDepth(depth int) string { func (s *FileNode) NameAtDepth(depth int) string {
splitName := strings.Split(s.Path, string(os.PathSeparator)) splitName := split(s.Path)
name := filepath.Join(splitName[depth:]...) name := join(splitName[depth:])
if s.File != nil && s.File.IsRename() { if s.File != nil && s.File.IsRename() {
splitPrevName := strings.Split(s.File.PreviousName, string(os.PathSeparator)) splitPrevName := split(s.File.PreviousName)
prevName := s.File.PreviousName prevName := s.File.PreviousName
// if the file has just been renamed inside the same directory, we can shave off // if the file has just been renamed inside the same directory, we can shave off
// the prefix for the previous path too. Otherwise we'll keep it unchanged // the prefix for the previous path too. Otherwise we'll keep it unchanged
sameParentDir := len(splitName) == len(splitPrevName) && filepath.Join(splitName[0:depth]...) == filepath.Join(splitPrevName[0:depth]...) sameParentDir := len(splitName) == len(splitPrevName) && join(splitName[0:depth]) == join(splitPrevName[0:depth])
if sameParentDir { if sameParentDir {
prevName = filepath.Join(splitPrevName[depth:]...) prevName = join(splitPrevName[depth:])
} }
return fmt.Sprintf("%s%s%s", prevName, " → ", name) return fmt.Sprintf("%s%s%s", prevName, " → ", name)

View File

@ -1,3 +1,5 @@
// +build !windows
package gui package gui
import ( import (