mirror of
https://github.com/rwinkhart/MUTN.git
synced 2026-09-06 00:57:15 -04:00
Fix incorrect method of simplifying directory headers
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
gofmt -l -w -s ./src/*.go
|
||||||
git add -f extra src/*.go src/*.mod .gitignore commit.sh LICENSE README.md
|
git add -f extra src/*.go src/*.mod .gitignore commit.sh LICENSE README.md
|
||||||
git commit -m "$1"
|
git commit -m "$1"
|
||||||
git push
|
git push
|
||||||
|
|||||||
+19
-24
@@ -14,12 +14,18 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// calculates and returns the final visual indentation multiplier (needed to adjust indentation for skipped parent directories) - also subtracts "old" text from directory header
|
// calculates and returns the final visual indentation multiplier (needed to adjust indentation for skipped parent directories) - also subtracts "old" text from directory header
|
||||||
func indentSubtractor(skippedDirList []bool, dirList []string, currentDirIndex int, indent int) int {
|
func indentSubtractor(skippedDirList []bool, dirList []string, currentDirIndex int, indent int) (int, string) {
|
||||||
var subtractor int // tracks how much to subtract from expected indentation multiplier
|
var subtractor int // tracks how much to subtract from expected indentation multiplier
|
||||||
|
var lastPrefixIndex int // tracks the index (in both skippedDirList and dirList) of the last displayed parent directory
|
||||||
|
var trimmedDirectory = dirList[currentDirIndex]
|
||||||
|
|
||||||
for i := len(skippedDirList[:currentDirIndex]) - 1; i >= 0; i-- { // checks each skipped directory to determine if it is a parent to the current directory
|
for i, skipped := range skippedDirList[:currentDirIndex] { // checks each skipped directory to determine if it is a parent to the current directory
|
||||||
if skippedDirList[i] && strings.HasPrefix(dirList[currentDirIndex], dirList[i]+PathSeparator) {
|
if strings.HasPrefix(trimmedDirectory, dirList[i]+PathSeparator) { // if the current directory is the child of this iteration's directory...
|
||||||
subtractor++ // if the skipped directory is a parent, increment the subtractor
|
if skipped { // ...and this iteration's directory was skipped...
|
||||||
|
subtractor++ // increment the subtractor to indicate that the visual indentation should be reduced
|
||||||
|
} else {
|
||||||
|
lastPrefixIndex = i
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,22 +33,11 @@ func indentSubtractor(skippedDirList []bool, dirList []string, currentDirIndex i
|
|||||||
|
|
||||||
if indent < 0 { // disallow negative indentation multipliers
|
if indent < 0 { // disallow negative indentation multipliers
|
||||||
indent = 0
|
indent = 0
|
||||||
} else if indent > 0 { // trim already printed text from directory header
|
} else if indent > 0 { // trim the most recently displayed parent directory from the directory header to avoid displaying redundant information
|
||||||
var sliceIndex int // track index of where new text begins
|
trimmedDirectory = strings.Replace(trimmedDirectory, dirList[lastPrefixIndex], "", 1)
|
||||||
var count int // track PathSeparator occurrences in current directory header
|
|
||||||
for currentIndex := 0; currentIndex < len(dirList[currentDirIndex]); currentIndex++ { // iterate over characters in the current directory header
|
|
||||||
if string(dirList[currentDirIndex][currentIndex]) == PathSeparator { // increment count if an occurrence of PathSeparator is found
|
|
||||||
count++
|
|
||||||
if count == indent+1 { // set sliceIndex and break once the correct number of PathSeparator occurrences are found
|
|
||||||
sliceIndex = currentIndex
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
dirList[currentDirIndex] = dirList[currentDirIndex][sliceIndex+1:] // update the directory header with the newly trimmed one for visual indentation
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return indent
|
return indent, trimmedDirectory
|
||||||
}
|
}
|
||||||
|
|
||||||
// processing for printing file entries (determines color, line wrapping, and prints)
|
// processing for printing file entries (determines color, line wrapping, and prints)
|
||||||
@@ -162,11 +157,11 @@ func EntryListGen() {
|
|||||||
if !containsFiles {
|
if !containsFiles {
|
||||||
containsFiles = true
|
containsFiles = true
|
||||||
skippedDirList[i] = false // the directory header is being printed, indicate that it is not being skipped
|
skippedDirList[i] = false // the directory header is being printed, indicate that it is not being skipped
|
||||||
indent = indentSubtractor(skippedDirList, dirList, i, indent) // calculate the final indentation multiplier
|
indent, directory = indentSubtractor(skippedDirList, dirList, i, indent) // calculate the final indentation multiplier
|
||||||
if !Windows { // for consistency, format directories with UNIX-style path separators on all platforms
|
if !Windows { // for consistency, format directories with UNIX-style path separators on all platforms
|
||||||
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", dirList[i])
|
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", directory)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", strings.ReplaceAll(dirList[i], PathSeparator, "/"))
|
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", strings.ReplaceAll(directory, PathSeparator, "/"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,11 +173,11 @@ func EntryListGen() {
|
|||||||
if !containsSubdirectory { // nor does it contain any subdirectories...
|
if !containsSubdirectory { // nor does it contain any subdirectories...
|
||||||
if dirListLength > 1 { // and directories besides the root-level exist... display directory header and empty directory warning
|
if dirListLength > 1 { // and directories besides the root-level exist... display directory header and empty directory warning
|
||||||
skippedDirList[i] = false // the directory header is being printed, indicate that it is not being skipped
|
skippedDirList[i] = false // the directory header is being printed, indicate that it is not being skipped
|
||||||
indent = indentSubtractor(skippedDirList, dirList, i, indent) // calculate the final indentation multiplier
|
indent, directory = indentSubtractor(skippedDirList, dirList, i, indent) // calculate the final indentation multiplier
|
||||||
if !Windows { // for consistency, format directories with UNIX-style path separators on all platforms
|
if !Windows { // for consistency, format directories with UNIX-style path separators on all platforms
|
||||||
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", dirList[i])
|
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", directory)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", strings.ReplaceAll(dirList[i], PathSeparator, "/"))
|
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", strings.ReplaceAll(directory, PathSeparator, "/"))
|
||||||
}
|
}
|
||||||
fmt.Print(strings.Repeat(" ", indent*2) + "\033[38;5;11m-empty directory-" + ansiReset)
|
fmt.Print(strings.Repeat(" ", indent*2) + "\033[38;5;11m-empty directory-" + ansiReset)
|
||||||
} else { // warn if the only thing that exists is the root-level directory
|
} else { // warn if the only thing that exists is the root-level directory
|
||||||
|
|||||||
Reference in New Issue
Block a user