From 6b4891a59f833a986bbfac0ec466904bdf58632e Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Fri, 19 Jan 2024 21:36:48 -0500 Subject: [PATCH] Assign ANSI escape colors to named constants --- src/ALLglobals.go | 17 +++++++++++------ src/UNIXglobals.go | 4 ++-- src/WINglobals.go | 4 ++-- src/entryList.go | 30 ++++++++++++++++++------------ src/printInfo.go | 44 +++++++++++++++++++++++++------------------- 5 files changed, 58 insertions(+), 41 deletions(-) diff --git a/src/ALLglobals.go b/src/ALLglobals.go index 7091b65..42a2a42 100644 --- a/src/ALLglobals.go +++ b/src/ALLglobals.go @@ -5,11 +5,16 @@ import ( "os" ) -// invisible variables -var home, _ = os.UserHomeDir() - -// exported variables +// global variables used across multiple files var ( - RootLength = len(EntryRoot) - Width, _, _ = termy.GetSize(int(os.Stdout.Fd())) + home, _ = os.UserHomeDir() + rootLength = len(EntryRoot) + width, _, _ = termy.GetSize(int(os.Stdout.Fd())) +) + +// global constants used across multiple files +const ( + ansiReset = "\033[0m" + ansiBold = "\033[1m" + ansiBlackOnWhite = "\033[38;5;0;48;5;15m" ) diff --git a/src/UNIXglobals.go b/src/UNIXglobals.go index ec19adc..60cc48a 100644 --- a/src/UNIXglobals.go +++ b/src/UNIXglobals.go @@ -2,10 +2,10 @@ package main -// EntryRoot path to libmutton entry directory +// EntryRoot path to libmutton entry directory TODO to be moved to libmutton var EntryRoot = home + "/.local/share/libmutton" -// exported constants +// exported constants TODO to be moved to libmutton const ( PathSeparator = "/" Windows = false diff --git a/src/WINglobals.go b/src/WINglobals.go index 07b3e72..a241076 100644 --- a/src/WINglobals.go +++ b/src/WINglobals.go @@ -2,10 +2,10 @@ package main -// EntryRoot path to libmutton entry directory +// EntryRoot path to libmutton entry directory TODO to be moved to libmutton var EntryRoot = home + "\\AppData\\Local\\libmutton\\entries" -// exported constants +// exported constants TODO to be moved to libmutton const ( PathSeparator = "\\" Windows = true diff --git a/src/entryList.go b/src/entryList.go index d1a1f36..2dd90fe 100644 --- a/src/entryList.go +++ b/src/entryList.go @@ -8,11 +8,17 @@ import ( "strings" ) +// global variables used only in this file var ( fileList []string dirList []string ) +// global constants used only in this file +const ( + ansiAlternateEntryColor = "\033[38;5;8m" +) + // processing for printing file entries (determines color, line wrapping, and prints) func printFileEntry(entry string, lastSlash int, charCounter int, colorAlternator int8) (int, int8) { // determine color to print fileEntryName (alternate each time function is run) @@ -20,7 +26,7 @@ func printFileEntry(entry string, lastSlash int, charCounter int, colorAlternato if colorAlternator > 0 { colorCode = "" } else { - colorCode = "\033[38;5;8m" + colorCode = ansiAlternateEntryColor } colorAlternator = -colorAlternator @@ -30,20 +36,20 @@ func printFileEntry(entry string, lastSlash int, charCounter int, colorAlternato // determine whether to wrap to a new line (+1 is to account for trailing spaces) charCounter += len(fileEntryName) + 1 - if charCounter >= Width { + if charCounter >= width { charCounter = len(fileEntryName) + 1 fmt.Println() } // print fileEntryName to screen - fmt.Printf("%s%s\033[0m ", colorCode, fileEntryName) + fmt.Printf("%s%s%s ", colorCode, fileEntryName, ansiReset) return charCounter, colorAlternator } -// EntryListGen generates and displays full entry list +// EntryListGen generates and displays full libmutton entry list func EntryListGen() { - fmt.Print("\n\033[38;5;0;48;5;15mlibmutton entries:\033[0m") + fmt.Print("\n" + ansiBlackOnWhite + "libmutton entries:" + ansiReset) // walk entry directory _ = filepath.WalkDir(EntryRoot, @@ -57,14 +63,14 @@ func EntryListGen() { 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") + fmt.Print("\n\n\033[38;5;9mAn unexpected error occurred while generating the entry list: " + err.Error() + ansiReset) } // quit walking EntryRoot and return nil to allow the program to continue return nil } // trim root path from each path before storing - trimmedPath := fullPath[RootLength:] + trimmedPath := fullPath[rootLength:] // 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 @@ -118,9 +124,9 @@ func EntryListGen() { if !containsFiles { containsFiles = true 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/"+ansiReset+"\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.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", strings.ReplaceAll(directory, PathSeparator, "/")) } } @@ -135,11 +141,11 @@ func EntryListGen() { 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) + fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\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.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-\033[0m") + 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 fmt.Print("\n\nNothing's here! For help creating your first entry, run \"mutn help\".") } diff --git a/src/printInfo.go b/src/printInfo.go index 2f16900..07f3612 100644 --- a/src/printInfo.go +++ b/src/printInfo.go @@ -2,14 +2,20 @@ package main import "fmt" +// global constants used only in this file +const ( + ansiGoFuchsia = "\033[38;2;206;48;98m" + ansiGoGopher = "\033[38;2;1;173;216m" +) + func HelpMain() { - fmt.Print("\n\033[1mMUTN | Copyright (c) 2024 Randall Winkhart\033[0m\n" + ` + fmt.Print(ansiBold + "\nMUTN | Copyright (c) 2024 Randall Winkhart\n" + ansiReset + ` This software exists under the MIT license; you may redistribute it under certain conditions. This program comes with absolutely no warranty; type "mutn version" for details. -` + "\033[1mUsage:\033[0m" + ` mutn [/ [argument] [option]] | [argument] +` + ansiBold + "Usage:" + ansiReset + ` mutn [/ [argument] [option]] | [argument] -` + "\033[1mArguments:\033[0m" + ` +` + ansiBold + "Arguments:" + ansiReset + ` help/--help/-h Bring up this menu version/-v Display version and license information init Set up MUTN @@ -21,7 +27,7 @@ This program comes with absolutely no warranty; type "mutn version" for details. shear Delete an existing entry sync Manually sync the entry directory -` + "\033[1mOptions:\033[0m" + ` +` + ansiBold + "Options:" + ansiReset + ` add: password/-p Add a password entry note/-n Add a note entry @@ -40,12 +46,12 @@ edit: gen: update/-u Generate a password for an existing entry -` + "\033[1mTip 1:\033[0m" + ` You can quickly read an entry with "mutn /" +` + ansiBold + "Tip 1:" + ansiReset + ` You can quickly read an entry with "mutn /" ` + "\033[1mTip 2:\033[0m" + ` Type "mutn" to view a list of saved entries` + "\n\n") } func Version() { - fmt.Print("\n\033[1m MIT License\033[0m" + ` + fmt.Print(ansiBold + "\n MIT License" + ansiReset + ` Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated @@ -72,19 +78,19 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.` + "\n\n---------------------------------------------------------" + "\n\n MUTN is a simple, self-hosted,\n SSH-synchronized password manager based on libmutton\n\n" + - " .. \033[38;2;206;48;98m♥♥ ♥♥\033[0m ..\n" + - " /()\\''.''. \033[38;2;206;48;98m♥♥♥♥♥♥♥\033[0m .''.''/()\\ _)\n" + - " _. : * \033[38;2;206;48;98m♥♥♥♥♥\033[0m * : <[◎]|_|=\n" + - " }-}-*] `..'..' \033[38;2;206;48;98m♥♥♥\033[0m `..'..' |\n" + - " ◎-◎ // \\\\ \033[38;2;206;48;98m♥\033[0m // \\\\ /|\\\n" + - "\033[38;2;1;173;216m<><><><><><><><><><><><><><>-<><><><><><><><><><><><><><>\n" + - "\\\033[38;5;15;48;5;15m \033[0m\033[38;2;1;173;216m/\n" + - "\\\033[38;5;15;48;5;15m \033[0mMUTN Version 0.0.1\033[38;5;15;48;5;15m \033[0m\033[38;2;1;173;216m/\n" + - "\\\033[38;5;15;48;5;15m \033[0mThe Butchered Update\033[38;5;15;48;5;15m \033[0m\033[38;2;1;173;216m/\n" + - "\\\033[38;5;15;48;5;15m \033[0m\033[38;2;1;173;216m/\n" + - "\\\033[38;5;15;48;5;15m \033[0mCopyright (c) 2024 Randall Winkhart\033[38;5;15;48;5;15m \033[0m\033[38;2;1;173;216m/\n" + - "\\\033[38;5;15;48;5;15m \033[0m\033[38;2;1;173;216m/\n" + - "<><><><><><><><><><><><><><>-<><><><><><><><><><><><><><>\033[0m\n" + + " .. " + ansiGoFuchsia + "♥♥ ♥♥" + ansiReset + " ..\n" + + " /()\\''.''. " + ansiGoFuchsia + "♥♥♥♥♥♥♥" + ansiReset + " .''.''/()\\ _)\n" + + " _. : * " + ansiGoFuchsia + "♥♥♥♥♥" + ansiReset + " * : <[◎]|_|=\n" + + " }-}-*] `..'..' " + ansiGoFuchsia + "♥♥♥" + ansiReset + " `..'..' |\n" + + " ◎-◎ // \\\\ " + ansiGoFuchsia + "♥" + ansiReset + " // \\\\ /|\\\n" + + ansiGoGopher + "<><><><><><><><><><><><><><>-<><><><><><><><><><><><><><>\n" + + "\\" + ansiBlackOnWhite + " " + ansiReset + ansiGoGopher + "/\n" + + "\\" + ansiBlackOnWhite + " MUTN Version 0.0.1 " + ansiReset + ansiGoGopher + "/\n" + + "\\" + ansiBlackOnWhite + " The Butchered Update " + ansiReset + ansiGoGopher + "/\n" + + "\\" + ansiBlackOnWhite + " " + ansiReset + ansiGoGopher + "/\n" + + "\\" + ansiBlackOnWhite + " Copyright (c) 2024 Randall Winkhart " + ansiReset + ansiGoGopher + "/\n" + + "\\" + ansiBlackOnWhite + " " + ansiReset + ansiGoGopher + "/\n" + + "<><><><><><><><><><><><><><>-<><><><><><><><><><><><><><>\n" + ansiReset + "\nFor more information, see:\n" + "https://github.com/rwinkhart/MUTN\n" + "https://github.com/rwinkhart/libmutton\n\n")