-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Extract say to its own file
- Loading branch information
1 parent
53731a5
commit bfdbdd7
Showing
2 changed files
with
80 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
var Debug = false // override with --debug parameter | ||
|
||
func parseDebug(args []string) { | ||
// debug needs to be parsed at the beginning to have DEBUG enabled as quickly as possible | ||
// otherwise, parsing others or other parameters don't have debug enabled | ||
for i := 0; i < len(args); i++ { | ||
if args[i] == "--debug" { | ||
Debug = true | ||
} | ||
} | ||
} | ||
|
||
func sayError(text string) { | ||
sayWithPrefix(text, "ERROR ") | ||
} | ||
|
||
func sayWarning(text string) { | ||
sayWithPrefix(text, "⚠ ") | ||
} | ||
|
||
func sayInfo(text string) { | ||
sayWithPrefix(text, "> ") | ||
} | ||
|
||
func sayInfoIndented(text string) { | ||
sayWithPrefix(text, " ") | ||
} | ||
|
||
func sayIndented(text string) { | ||
sayWithPrefix(text, " ") | ||
} | ||
|
||
func sayFix(instruction string, command string) { | ||
sayWithPrefix(instruction, "👉 ") | ||
sayEmptyLine() | ||
sayIndented(command) | ||
sayEmptyLine() | ||
} | ||
|
||
func sayNext(instruction string, command string) { | ||
sayWithPrefix(instruction, "👉 ") | ||
sayEmptyLine() | ||
sayIndented(command) | ||
sayEmptyLine() | ||
} | ||
|
||
func sayWithPrefix(s string, prefix string) { | ||
lines := strings.Split(strings.TrimSpace(s), "\n") | ||
for i := 0; i < len(lines); i++ { | ||
printToConsole(prefix + strings.TrimSpace(lines[i]) + "\n") | ||
} | ||
} | ||
|
||
func sayEmptyLine() { | ||
printToConsole("\n") | ||
} | ||
|
||
func say(s string) { | ||
if len(s) == 0 { | ||
return | ||
} | ||
printToConsole(strings.TrimRight(s, " \r\n\t\v\f") + "\n") | ||
} | ||
|
||
func debugInfo(text string) { | ||
if Debug { | ||
sayWithPrefix(text, "DEBUG ") | ||
} | ||
} | ||
|
||
var printToConsole = func(message string) { | ||
fmt.Print(message) | ||
} |