mirror of
https://github.com/rwinkhart/MUTN.git
synced 2026-09-05 16:47:20 -04:00
Merge fileList slices into one, remove now-redundant loop
This commit is contained in:
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
// EntryRoot path to libmutton entry directory
|
// EntryRoot path to libmutton entry directory
|
||||||
var EntryRoot = home + "/.local/share/fake"
|
var EntryRoot = home + "/.local/share/libmutton"
|
||||||
|
|
||||||
// exported constants
|
// exported constants
|
||||||
const (
|
const (
|
||||||
|
|||||||
+57
-68
@@ -9,9 +9,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
fileListRoot []string
|
fileList []string
|
||||||
fileListMain []string
|
dirList []string
|
||||||
dirListMain []string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// processing for printing file entries (determines color, line wrapping, and prints)
|
// 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
|
// 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" {
|
if err.Error() == "lstat "+EntryRoot+": no such file or directory" {
|
||||||
_ = os.Mkdir(EntryRoot, 0700)
|
_ = os.Mkdir(EntryRoot, 0700)
|
||||||
|
dirList = append(dirList, "")
|
||||||
} else {
|
} else {
|
||||||
// otherwise, print the source of the error
|
// 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")
|
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
|
// 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
|
// 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 {
|
if !entry.IsDir() {
|
||||||
fileListRoot = append(fileListRoot, trimmedPath)
|
fileList = append(fileList, trimmedPath)
|
||||||
} else if !entry.IsDir() {
|
} else {
|
||||||
fileListMain = append(fileListMain, trimmedPath)
|
dirList = append(dirList, trimmedPath)
|
||||||
} else if trimmedPath != "" {
|
|
||||||
dirListMain = append(dirListMain, trimmedPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -83,87 +81,78 @@ func EntryListGen() {
|
|||||||
charCounter := 0 // set to track whether to line-wrap based on character count in line
|
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
|
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
|
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
|
// dirList iteration
|
||||||
// must be done within loop in order to allow not printing the header if there are no root-level entries
|
dirListLength := len(dirList) // save length for multiple references below
|
||||||
if !ran {
|
var containsSubdirectory bool // track whether the current directory contains a subdirectory
|
||||||
ran = true
|
var indent int // visual indentation multiplier
|
||||||
fmt.Print("\n\n\033[38;5;7;48;5;8m/\033[0m\n")
|
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
|
// check if next directory is within the current one
|
||||||
charCounter, colorAlternator = printFileEntry(file, strings.LastIndex(file, PathSeparator)+1, charCounter, colorAlternator)
|
if dirListLength > i+1 {
|
||||||
|
nextDir := dirList[i+1]
|
||||||
}
|
if directory == nextDir[:strings.LastIndex(nextDir, PathSeparator)] {
|
||||||
|
containsSubdirectory = true
|
||||||
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
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
containsSubdirectory = false
|
containsSubdirectory = false
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
containsSubdirectory = false
|
||||||
|
}
|
||||||
|
|
||||||
// fileListMain iteration
|
// fileList iteration
|
||||||
ran = false // set to track whether the loop has been run yet
|
ran = false // set to track whether the loop has been run yet
|
||||||
for _, file := range fileListMain {
|
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)
|
// 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
|
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
|
// 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 {
|
if file[:lastSlash-1] == directory {
|
||||||
|
|
||||||
// print directory header if this is the first run of the loop
|
// 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
|
// must be done within loop in order to allow not printing the header if its directory contains no entries
|
||||||
if !ran {
|
if !ran {
|
||||||
ran = true
|
ran = true
|
||||||
// for consistency, format directories with UNIX-style path separators on all platforms
|
// for consistency, format directories with UNIX-style path separators on all platforms
|
||||||
if !Windows {
|
if !Windows {
|
||||||
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/\033[0m\n", directory)
|
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/\033[0m\n", directory)
|
||||||
} else {
|
} 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.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 indent > 0 { // indent entry list to the same degree as the directory header
|
||||||
if !containsSubdirectory { // display directory header and empty directory warning if it also contains no subdirectories
|
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
|
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)
|
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/\033[0m\n", directory)
|
||||||
} else {
|
} 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.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")
|
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
|
// print trailing new lines for proper spacing after entry list is complete
|
||||||
|
|||||||
Reference in New Issue
Block a user