mirror of
https://github.com/rwinkhart/MUTN.git
synced 2026-08-27 20:36:29 -04:00
Merge fileList slices into one, remove now-redundant loop
This commit is contained in:
+1
-1
@@ -3,7 +3,7 @@
|
||||
package main
|
||||
|
||||
// EntryRoot path to libmutton entry directory
|
||||
var EntryRoot = home + "/.local/share/fake"
|
||||
var EntryRoot = home + "/.local/share/libmutton"
|
||||
|
||||
// exported constants
|
||||
const (
|
||||
|
||||
+57
-68
@@ -9,9 +9,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
fileListRoot []string
|
||||
fileListMain []string
|
||||
dirListMain []string
|
||||
fileList []string
|
||||
dirList []string
|
||||
)
|
||||
|
||||
// processing for printing file entries (determines color, line wrapping, and prints)
|
||||
@@ -55,6 +54,7 @@ func EntryListGen() {
|
||||
// create EntryRoot if the error is the result of it not existing on the system
|
||||
if err.Error() == "lstat "+EntryRoot+": no such file or directory" {
|
||||
_ = os.Mkdir(EntryRoot, 0700)
|
||||
dirList = append(dirList, "")
|
||||
} else {
|
||||
// otherwise, print the source of the error
|
||||
fmt.Print("\n\n\033[38;5;9mAn unexpected error occurred while generating the entry list: " + err.Error() + "\033[0m")
|
||||
@@ -68,12 +68,10 @@ func EntryListGen() {
|
||||
|
||||
// create three separate slices for root-level entries, all other entries, and all subdirectories
|
||||
// root-level entries get their own slice so that they can be alphabetically sorted without the chance of directories being placed in from of them
|
||||
if !entry.IsDir() && strings.Count(trimmedPath, PathSeparator) == 1 {
|
||||
fileListRoot = append(fileListRoot, trimmedPath)
|
||||
} else if !entry.IsDir() {
|
||||
fileListMain = append(fileListMain, trimmedPath)
|
||||
} else if trimmedPath != "" {
|
||||
dirListMain = append(dirListMain, trimmedPath)
|
||||
if !entry.IsDir() {
|
||||
fileList = append(fileList, trimmedPath)
|
||||
} else {
|
||||
dirList = append(dirList, trimmedPath)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -83,87 +81,78 @@ func EntryListGen() {
|
||||
charCounter := 0 // set to track whether to line-wrap based on character count in line
|
||||
var colorAlternator int8 = 1 // set to allow alternating colors for each printed entry name
|
||||
ran := false // set to track whether the loop has been run yet
|
||||
for _, file := range fileListRoot {
|
||||
|
||||
// print root ("/") header if this is the first run of the loop
|
||||
// must be done within loop in order to allow not printing the header if there are no root-level entries
|
||||
if !ran {
|
||||
ran = true
|
||||
fmt.Print("\n\n\033[38;5;7;48;5;8m/\033[0m\n")
|
||||
// dirList iteration
|
||||
dirListLength := len(dirList) // save length for multiple references below
|
||||
var containsSubdirectory bool // track whether the current directory contains a subdirectory
|
||||
var indent int // visual indentation multiplier
|
||||
for i, directory := range dirList {
|
||||
|
||||
// reset formatting variables for new directory
|
||||
charCounter = 0
|
||||
colorAlternator = 1
|
||||
|
||||
// determine directory's indentation multiplier based on PathSeparator occurrences - only run if last directory contained a subdirectory (indicating that the current directory is a subdirectory)
|
||||
indent = strings.Count(directory, PathSeparator) - 1 // subtract 1 to account for trailing PathSeparator
|
||||
if indent < 0 {
|
||||
indent = 0
|
||||
}
|
||||
|
||||
// get index of last occurrence of pathSeparator in trimmed entry path (used to split entry's containing directory and entry's name) and pass to printFileEntry
|
||||
charCounter, colorAlternator = printFileEntry(file, strings.LastIndex(file, PathSeparator)+1, charCounter, colorAlternator)
|
||||
|
||||
}
|
||||
|
||||
dirListMainLength := len(dirListMain) // save length for multiple references below
|
||||
if dirListMainLength > 0 { // DirListMain iteration - only run if non-root-level directories are present TODO is this still necessary?
|
||||
var containsSubdirectory bool // track whether the current directory contains a subdirectory
|
||||
var indent int // visual indentation multiplier
|
||||
for i, directory := range dirListMain {
|
||||
|
||||
// reset formatting variables for new directory
|
||||
charCounter = 0
|
||||
colorAlternator = 1
|
||||
|
||||
// determine directory's indentation multiplier based on PathSeparator occurrences - only run if last directory contained a subdirectory (indicating that the current directory is a subdirectory)
|
||||
indent = strings.Count(directory, PathSeparator) - 1 // subtract 1 to account for trailing PathSeparator
|
||||
|
||||
// check if next directory is within the current one
|
||||
if dirListMainLength > i+1 {
|
||||
nextDir := dirListMain[i+1]
|
||||
if directory == nextDir[:strings.LastIndex(nextDir, PathSeparator)] {
|
||||
containsSubdirectory = true
|
||||
} else {
|
||||
containsSubdirectory = false
|
||||
}
|
||||
// check if next directory is within the current one
|
||||
if dirListLength > i+1 {
|
||||
nextDir := dirList[i+1]
|
||||
if directory == nextDir[:strings.LastIndex(nextDir, PathSeparator)] {
|
||||
containsSubdirectory = true
|
||||
} else {
|
||||
containsSubdirectory = false
|
||||
}
|
||||
} else {
|
||||
containsSubdirectory = false
|
||||
}
|
||||
|
||||
// fileListMain iteration
|
||||
ran = false // set to track whether the loop has been run yet
|
||||
for _, file := range fileListMain {
|
||||
// fileList iteration
|
||||
ran = false // set to track whether the loop has been run yet
|
||||
for _, file := range fileList {
|
||||
|
||||
// get index of last occurrence of pathSeparator in trimmed entry path (used to split entry's containing directory and entry's name)
|
||||
lastSlash := strings.LastIndex(file, PathSeparator) + 1
|
||||
// get index of last occurrence of pathSeparator in trimmed entry path (used to split entry's containing directory and entry's name)
|
||||
lastSlash := strings.LastIndex(file, PathSeparator) + 1
|
||||
|
||||
// print the current file if it belongs in the current directory - otherwise, break the loop and move on to the next directory
|
||||
if file[:lastSlash-1] == directory {
|
||||
// print the current file if it belongs in the current directory - otherwise, break the loop and move on to the next directory
|
||||
if file[:lastSlash-1] == directory {
|
||||
|
||||
// print directory header if this is the first run of the loop
|
||||
// must be done within loop in order to allow not printing the header if its directory contains no entries
|
||||
if !ran {
|
||||
ran = true
|
||||
// for consistency, format directories with UNIX-style path separators on all platforms
|
||||
if !Windows {
|
||||
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/\033[0m\n", directory)
|
||||
} else {
|
||||
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/\033[0m\n", strings.ReplaceAll(directory, PathSeparator, "/"))
|
||||
}
|
||||
// print directory header if this is the first run of the loop
|
||||
// must be done within loop in order to allow not printing the header if its directory contains no entries
|
||||
if !ran {
|
||||
ran = true
|
||||
// for consistency, format directories with UNIX-style path separators on all platforms
|
||||
if !Windows {
|
||||
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/\033[0m\n", directory)
|
||||
} else {
|
||||
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/\033[0m\n", strings.ReplaceAll(directory, PathSeparator, "/"))
|
||||
}
|
||||
|
||||
if indent > 0 { // indent entry list to the same degree as the directory header
|
||||
fmt.Print(strings.Repeat(" ", indent*2))
|
||||
}
|
||||
charCounter, colorAlternator = printFileEntry(file, lastSlash, charCounter, colorAlternator)
|
||||
}
|
||||
}
|
||||
|
||||
if !ran { // if the current directory contains no files...
|
||||
if !containsSubdirectory { // display directory header and empty directory warning if it also contains no subdirectories
|
||||
if indent > 0 { // indent entry list to the same degree as the directory header
|
||||
fmt.Print(strings.Repeat(" ", indent*2))
|
||||
}
|
||||
charCounter, colorAlternator = printFileEntry(file, lastSlash, charCounter, colorAlternator)
|
||||
}
|
||||
}
|
||||
|
||||
if !ran { // if the current directory contains no files...
|
||||
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 !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/\033[0m\n", directory)
|
||||
} else {
|
||||
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/\033[0m\n", strings.ReplaceAll(directory, PathSeparator, "/"))
|
||||
}
|
||||
fmt.Print(strings.Repeat(" ", indent*2) + "\033[38;5;11m-empty directory-\033[0m")
|
||||
} else { // warn if the only thing that exists is the root-level directory
|
||||
fmt.Print("\n\nNothing's here! For help creating your first entry, run \"mutn help\".")
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if !ran {
|
||||
fmt.Print("\n\nNothing's here! For help creating your first entry, run \"mutn help\".")
|
||||
}
|
||||
|
||||
// print trailing new lines for proper spacing after entry list is complete
|
||||
|
||||
Reference in New Issue
Block a user