Skip to content

Commit

Permalink
Refactor: Extract say to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorriegler committed Sep 20, 2022
1 parent 53731a5 commit bfdbdd7
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 73 deletions.
73 changes: 0 additions & 73 deletions mob.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const (

var (
workingDir = ""
Debug = false // override with --debug parameter
GitPassthroughStderrStdout = false // hack to get git hooks to print to stdout/stderr
)

Expand Down Expand Up @@ -336,16 +335,6 @@ func getDefaultConfiguration() Configuration {
}
}

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 (c Configuration) mob(command string) string {
return c.CliName + " " + command
}
Expand Down Expand Up @@ -1935,65 +1924,3 @@ func startCommand(name string, args ...string) (string, error) {
var exit = func(code int) {
os.Exit(code)
}

func sayError(text string) {
sayWithPrefix(text, "ERROR ")
}

func debugInfo(text string) {
if Debug {
sayWithPrefix(text, "DEBUG ")
}
}

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 sayInfo(text string) {
sayWithPrefix(text, "> ")
}

func sayInfoIndented(text string) {
sayWithPrefix(text, " ")
}

func sayWarning(text string) {
sayWithPrefix(text, "⚠ ")
}

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 say(s string) {
if len(s) == 0 {
return
}
printToConsole(strings.TrimRight(s, " \r\n\t\v\f") + "\n")
}

func sayEmptyLine() {
printToConsole("\n")
}

var printToConsole = func(message string) {
fmt.Print(message)
}
80 changes: 80 additions & 0 deletions say.go
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)
}

0 comments on commit bfdbdd7

Please sign in to comment.