Skip to content

Commit

Permalink
improve separator printing
Browse files Browse the repository at this point in the history
  • Loading branch information
mitranim committed Jan 25, 2022
1 parent e4fd29c commit 5348a15
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions gow.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Options:
-w Paths to watch, relative to CWD, comma-separated; default: %[2]q
-r Enable terminal raw mode and hotkeys; default: %[3]v
-g The Go tool to use; default: %[4]q
-S Separate runs with a specific string; default: "%[5]v"
-S Separator string printed after each run; supports \n; default: "%[5]v"
Supported control codes / hotkeys:
Expand Down Expand Up @@ -81,6 +81,8 @@ const (
TERM_CLEAR_SOFT = ESC + "c"
TERM_CLEAR_SCROLLBACK = ESC + "[3J"
TERM_CLEAR_HARD = TERM_CLEAR_SOFT + TERM_CLEAR_SCROLLBACK

NEWLINE = "\n"
)

var (
Expand All @@ -91,14 +93,14 @@ var (
FLAG_CLEAR_SOFT = FLAG_SET.Bool("s", false, "")
FLAG_RAW = FLAG_SET.Bool("r", true, "")
FLAG_SEP = FLAG_SET.String("S", "", "")
SEP []byte

EXTENSIONS = &flagStrings{validateExtension, []string{"go", "mod"}}
IGNORED_PATHS = &flagStrings{validatePath, nil}
WATCH = &flagStrings{validatePath, DEFAULT_WATCH}
DEFAULT_WATCH = []string{`.`}

log = l.New(os.Stderr, "[gow] ", 0)

log = l.New(os.Stderr, "[gow] ", 0)
killSignals = []os.Signal{syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM}
)

Expand All @@ -124,6 +126,8 @@ func main() {
os.Exit(1)
}

SEP = []byte(unescapedLine(*FLAG_SEP))

// Everything needed for cleanup must be registered here.
var termios *unix.Termios
var cmd *exec.Cmd
Expand Down Expand Up @@ -265,9 +269,8 @@ func main() {
log.Println("exit ok")
}

if *FLAG_SEP != "" {
sep := strings.Replace(*FLAG_SEP, "\\n", string([]byte{10}), -1)
fmt.Println(sep)
if len(SEP) > 0 {
log.Writer().Write(SEP)
}
cmd = nil
cmdStdin = nil
Expand Down Expand Up @@ -604,3 +607,11 @@ func signalsInclude(signals []os.Signal, sig os.Signal) bool {
}
return false
}

func unescapedLine(str string) string {
str = strings.ReplaceAll(str, `\n`, NEWLINE)
if len(str) > 0 && !strings.HasSuffix(str, NEWLINE) {
str += NEWLINE
}
return str
}

0 comments on commit 5348a15

Please sign in to comment.