Skip to content

Commit

Permalink
fix(highlight): only highlight source code when supported (#10)
Browse files Browse the repository at this point in the history
This change ensures that we only highlight caller source code when:

1. It is enabled
   - By default, highlighting is enabled
   - Can be disabled by setting `JUSTEST_DISABLE_SOURCE_HIGHLIGHT` to a
     "falsy" value like `false`, `FALSE`, `False`, `0`, or `f`
2. Supported
   - Currently only macOS is supported, because dark-mode detection is only
     supported on macOS
   - Dark mode detection succeeded (again, currently only on macOS)
  • Loading branch information
arikkfir committed May 11, 2024
1 parent c67c495 commit 4815530
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions location.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/exec"
"runtime"
"strconv"
"strings"

Expand All @@ -23,6 +24,7 @@ const (

var (
displayMode = displayModeLight
highlight = true
)

// Source code highlighting
Expand Down Expand Up @@ -86,15 +88,6 @@ func readSourceAt(file string, line int) string {
lines := strings.Split(fileContents, "\n")
if len(lines) > line {
source = strings.TrimSpace(lines[line-1])

highlight := true
if highlightEnv := os.Getenv("JUSTEST_DISABLE_SOURCE_HIGHLIGHT"); highlightEnv != "" {
if val, err := strconv.ParseBool(highlightEnv); err != nil {
panic(fmt.Sprintf("Error parsing JUSTEST_HIGHLIGHT_SOURCE environment variable - illegal value: %s", highlightEnv))
} else {
highlight = val
}
}
if highlight {
output := bytes.Buffer{}
if err := quick.Highlight(&output, source, "go", goSourceFormatter, goSourceStyle[displayMode]); err == nil {
Expand All @@ -106,21 +99,35 @@ func readSourceAt(file string, line int) string {
return source
}

const (
appleScriptDarkModeQuery string = `tell application "System Events" to tell appearance preferences to get dark mode`
)

func init() {
cmd := exec.Command("osascript", "-e", appleScriptDarkModeQuery)
if out, err := cmd.Output(); err != nil {
displayMode = displayModeLight
fmt.Printf("Error determining system's dark mode: %+v\n", err)
} else if dark, err := strconv.ParseBool(strings.TrimSpace(string(out))); err != nil {
displayMode = displayModeLight
fmt.Printf("Error determining system's dark mode: %+v\n", err)
} else if dark {
displayMode = displayModeDark
} else {
displayMode = displayModeLight
const appleScriptDarkModeQuery string = `tell application "System Events" to tell appearance preferences to get dark mode`

if highlightEnv := os.Getenv("JUSTEST_DISABLE_SOURCE_HIGHLIGHT"); highlightEnv != "" {
if val, err := strconv.ParseBool(highlightEnv); err != nil {
panic(fmt.Sprintf("Error parsing JUSTEST_HIGHLIGHT_SOURCE environment variable - illegal value: %s", highlightEnv))
} else {
highlight = val
}
}

if highlight {
switch runtime.GOOS {
case "darwin":
cmd := exec.Command("osascript", "-e", appleScriptDarkModeQuery)
if out, err := cmd.Output(); err != nil {
fmt.Printf("Error determining system's dark mode: %+v\n", err)
highlight = false
} else if dark, err := strconv.ParseBool(strings.TrimSpace(string(out))); err != nil {
fmt.Printf("Error determining system's dark mode: %+v\n", err)
highlight = false
} else if dark {
displayMode = displayModeDark
} else {
displayMode = displayModeLight
}
case "windows", "linux":
// TODO: Implement a similar mechanism for Windows and Linux
highlight = false
}
}
}

0 comments on commit 4815530

Please sign in to comment.