Assign ANSI escape colors to named constants

This commit is contained in:
2024-01-19 21:36:48 -05:00
parent 29c9fb812c
commit 6b4891a59f
5 changed files with 58 additions and 41 deletions
+11 -6
View File
@@ -5,11 +5,16 @@ import (
"os" "os"
) )
// invisible variables // global variables used across multiple files
var home, _ = os.UserHomeDir()
// exported variables
var ( var (
RootLength = len(EntryRoot) home, _ = os.UserHomeDir()
Width, _, _ = termy.GetSize(int(os.Stdout.Fd())) 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"
) )
+2 -2
View File
@@ -2,10 +2,10 @@
package main 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" var EntryRoot = home + "/.local/share/libmutton"
// exported constants // exported constants TODO to be moved to libmutton
const ( const (
PathSeparator = "/" PathSeparator = "/"
Windows = false Windows = false
+2 -2
View File
@@ -2,10 +2,10 @@
package main 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" var EntryRoot = home + "\\AppData\\Local\\libmutton\\entries"
// exported constants // exported constants TODO to be moved to libmutton
const ( const (
PathSeparator = "\\" PathSeparator = "\\"
Windows = true Windows = true
+18 -12
View File
@@ -8,11 +8,17 @@ import (
"strings" "strings"
) )
// global variables used only in this file
var ( var (
fileList []string fileList []string
dirList []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) // processing for printing file entries (determines color, line wrapping, and prints)
func printFileEntry(entry string, lastSlash int, charCounter int, colorAlternator int8) (int, int8) { func printFileEntry(entry string, lastSlash int, charCounter int, colorAlternator int8) (int, int8) {
// determine color to print fileEntryName (alternate each time function is run) // 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 { if colorAlternator > 0 {
colorCode = "" colorCode = ""
} else { } else {
colorCode = "\033[38;5;8m" colorCode = ansiAlternateEntryColor
} }
colorAlternator = -colorAlternator 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) // determine whether to wrap to a new line (+1 is to account for trailing spaces)
charCounter += len(fileEntryName) + 1 charCounter += len(fileEntryName) + 1
if charCounter >= Width { if charCounter >= width {
charCounter = len(fileEntryName) + 1 charCounter = len(fileEntryName) + 1
fmt.Println() fmt.Println()
} }
// print fileEntryName to screen // print fileEntryName to screen
fmt.Printf("%s%s\033[0m ", colorCode, fileEntryName) fmt.Printf("%s%s%s ", colorCode, fileEntryName, ansiReset)
return charCounter, colorAlternator return charCounter, colorAlternator
} }
// EntryListGen generates and displays full entry list // EntryListGen generates and displays full libmutton entry list
func EntryListGen() { 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 // walk entry directory
_ = filepath.WalkDir(EntryRoot, _ = filepath.WalkDir(EntryRoot,
@@ -57,14 +63,14 @@ func EntryListGen() {
dirList = append(dirList, "") 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() + ansiReset)
} }
// quit walking EntryRoot and return nil to allow the program to continue // quit walking EntryRoot and return nil to allow the program to continue
return nil return nil
} }
// trim root path from each path before storing // 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 // 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
@@ -118,9 +124,9 @@ func EntryListGen() {
if !containsFiles { if !containsFiles {
containsFiles = true containsFiles = true
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/"+ansiReset+"\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/"+ansiReset+"\n", strings.ReplaceAll(directory, PathSeparator, "/"))
} }
} }
@@ -135,11 +141,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
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/"+ansiReset+"\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/"+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 } 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\".") fmt.Print("\n\nNothing's here! For help creating your first entry, run \"mutn help\".")
} }
+25 -19
View File
@@ -2,14 +2,20 @@ package main
import "fmt" 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() { 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 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. This program comes with absolutely no warranty; type "mutn version" for details.
` + "\033[1mUsage:\033[0m" + ` mutn [/<entry name> [argument] [option]] | [argument] ` + ansiBold + "Usage:" + ansiReset + ` mutn [/<entry name> [argument] [option]] | [argument]
` + "\033[1mArguments:\033[0m" + ` ` + ansiBold + "Arguments:" + ansiReset + `
help/--help/-h Bring up this menu help/--help/-h Bring up this menu
version/-v Display version and license information version/-v Display version and license information
init Set up MUTN 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 shear Delete an existing entry
sync Manually sync the entry directory sync Manually sync the entry directory
` + "\033[1mOptions:\033[0m" + ` ` + ansiBold + "Options:" + ansiReset + `
add: add:
password/-p Add a password entry password/-p Add a password entry
note/-n Add a note entry note/-n Add a note entry
@@ -40,12 +46,12 @@ edit:
gen: gen:
update/-u Generate a password for an existing entry update/-u Generate a password for an existing entry
` + "\033[1mTip 1:\033[0m" + ` You can quickly read an entry with "mutn /<entry name>" ` + ansiBold + "Tip 1:" + ansiReset + ` You can quickly read an entry with "mutn /<entry name>"
` + "\033[1mTip 2:\033[0m" + ` Type "mutn" to view a list of saved entries` + "\n\n") ` + "\033[1mTip 2:\033[0m" + ` Type "mutn" to view a list of saved entries` + "\n\n")
} }
func Version() { 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 Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated 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.` + OR OTHER DEALINGS IN THE SOFTWARE.` +
"\n\n---------------------------------------------------------" + "\n\n---------------------------------------------------------" +
"\n\n MUTN is a simple, self-hosted,\n SSH-synchronized password manager based on libmutton\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" + " .. " + ansiGoFuchsia + "♥♥ ♥♥" + ansiReset + " ..\n" +
" /()\\''.''. \033[38;2;206;48;98m♥♥♥♥♥♥♥\033[0m .''.''/()\\ _)\n" + " /()\\''.''. " + ansiGoFuchsia + "♥♥♥♥♥♥♥" + ansiReset + " .''.''/()\\ _)\n" +
" _. : * \033[38;2;206;48;98m♥♥♥♥♥\033[0m * : <[◎]|_|=\n" + " _. : * " + ansiGoFuchsia + "♥♥♥♥♥" + ansiReset + " * : <[◎]|_|=\n" +
" }-}-*] `..'..' \033[38;2;206;48;98m♥♥♥\033[0m `..'..' |\n" + " }-}-*] `..'..' " + ansiGoFuchsia + "♥♥♥" + ansiReset + " `..'..' |\n" +
" ◎-◎ // \\\\ \033[38;2;206;48;98m♥\033[0m // \\\\ /|\\\n" + " ◎-◎ // \\\\ " + ansiGoFuchsia + "♥" + ansiReset + " // \\\\ /|\\\n" +
"\033[38;2;1;173;216m<><><><><><><><><><><><><><>-<><><><><><><><><><><><><><>\n" + ansiGoGopher + "<><><><><><><><><><><><><><>-<><><><><><><><><><><><><><>\n" +
"\\\033[38;5;15;48;5;15m \033[0m\033[38;2;1;173;216m/\n" + "\\" + ansiBlackOnWhite + " " + ansiReset + ansiGoGopher + "/\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" + "\\" + ansiBlackOnWhite + " MUTN Version 0.0.1 " + ansiReset + ansiGoGopher + "/\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" + "\\" + ansiBlackOnWhite + " The Butchered Update " + ansiReset + ansiGoGopher + "/\n" +
"\\\033[38;5;15;48;5;15m \033[0m\033[38;2;1;173;216m/\n" + "\\" + ansiBlackOnWhite + " " + ansiReset + ansiGoGopher + "/\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" + "\\" + ansiBlackOnWhite + " Copyright (c) 2024 Randall Winkhart " + ansiReset + ansiGoGopher + "/\n" +
"\\\033[38;5;15;48;5;15m \033[0m\033[38;2;1;173;216m/\n" + "\\" + ansiBlackOnWhite + " " + ansiReset + ansiGoGopher + "/\n" +
"<><><><><><><><><><><><><><>-<><><><><><><><><><><><><><>\033[0m\n" + "<><><><><><><><><><><><><><>-<><><><><><><><><><><><><><>\n" + ansiReset +
"\nFor more information, see:\n" + "\nFor more information, see:\n" +
"https://github.com/rwinkhart/MUTN\n" + "https://github.com/rwinkhart/MUTN\n" +
"https://github.com/rwinkhart/libmutton\n\n") "https://github.com/rwinkhart/libmutton\n\n")