Skip to content

Commit

Permalink
Only use colors if the handle supports ANSI
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspervdj-luminal authored Sep 16, 2020
1 parent b2c1a72 commit 2c2e16a
Show file tree
Hide file tree
Showing 54 changed files with 357 additions and 356 deletions.
30 changes: 17 additions & 13 deletions lib/Fregot/PrettyPrint/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module Fregot.PrettyPrint.Internal
, removeLines
) where

import Control.Monad (when)
import qualified Data.Aeson as Aeson
import Data.Char (isSpace)
import Data.Int (Int64)
Expand Down Expand Up @@ -202,19 +203,22 @@ hPutColDoc :: IO.Handle -> Doc Ansi.Color -> IO ()
hPutColDoc h = hPutSimpleColDoc h . renderDefaults

hPutSimpleColDoc :: IO.Handle -> SimpleDoc Ansi.Color -> IO ()
hPutSimpleColDoc h = IO.hWithEncoding h IO.utf8 . go
where
go SEmpty = IO.hPutStrLn h "" >> IO.hFlush h
go (SChar c doc) =
IO.hPutChar h c >> go doc
go (SText _width str doc) =
IO.hPutStr h str >> go doc
go (SLine i doc) =
IO.hPutStrLn h "" >> IO.hPutStr h (replicate i ' ') >> go doc
go (SAnnotStart col doc) =
Ansi.hSetSGR h [Ansi.SetColor Ansi.Foreground Ansi.Dull col] >> go doc
go (SAnnotStop doc) =
Ansi.hSetSGR h [Ansi.Reset] >> go doc
hPutSimpleColDoc h doc0 = IO.hWithEncoding h IO.utf8 $ do
ansi <- Ansi.hSupportsANSI h
let go SEmpty = IO.hPutStrLn h "" >> IO.hFlush h
go (SChar c doc) =
IO.hPutChar h c >> go doc
go (SText _width str doc) =
IO.hPutStr h str >> go doc
go (SLine i doc) =
IO.hPutStrLn h "" >> IO.hPutStr h (replicate i ' ') >> go doc
go (SAnnotStart col doc) = do
when ansi $ Ansi.hSetSGR h [Ansi.SetColor Ansi.Foreground Ansi.Dull col]
go doc
go (SAnnotStop doc) = do
when ansi $ Ansi.hSetSGR h [Ansi.Reset]
go doc
go doc0

renderDefaults :: Doc a -> SimpleDoc a
renderDefaults = removeIndentOnly . renderPretty 0.9 80
Expand Down
2 changes: 1 addition & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resolver: 'lts-15.6'
resolver: 'lts-16.9'

# Dev options
ghc-options:
Expand Down
11 changes: 4 additions & 7 deletions stack.yaml.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ packages:
original:
hackage: jose-0.8.3
- completed:
cabal-file:
size: 1314
sha256: fca4cbcd0681c9089225b2f1f2b338e88686c1ea53082c62f5c62062e9400f83
name: goldplate
version: 0.1.0
git: https://github.com/fugue/goldplate.git
Expand All @@ -34,7 +31,7 @@ packages:
commit: 0ac9d399eb7bcac810a1bf4d97ccc08fe6f39f39
snapshots:
- completed:
size: 491387
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/15/6.yaml
sha256: 8d81505a6de861e167a58534ab62330afb75bfa108735c7db1204f7ef2a39d79
original: lts-15.6
size: 532380
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/16/9.yaml
sha256: 14a7cec114424e4286adde73364438927a553ed248cc50f069a30a67e3ee1e69
original: lts-16.9
2 changes: 1 addition & 1 deletion tests/golden/cli/repl-input.stdout
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
repl% = "Hello, world!"
repl% = "Hello, world!"
repl%
14 changes: 7 additions & 7 deletions tests/golden/invalid/assign-rule-01.rego.stderr
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
fregot (compile error):
fregot (compile error):
"assign-rule-01.rego" (line 4, column 1):
bad default:

4| default allow := false
^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^

Default rules should use `=` rather than `:=`.

fregot (compile error):
fregot (compile error):
"assign-rule-01.rego" (line 8, column 1):
conflicting `:=` rule:

8| pi = 3.14
^^^^^^^^^
^^^^^^^^^

rules declared using `:=` cannot have multiple definitions

"assign-rule-01.rego" (line 7, column 1):
conflicting `:=` rule:

7| pi := 3.14
^^^^^^^^^^
^^^^^^^^^^

rules declared using `:=` cannot have multiple definitions

fregot (compile error):
fregot (compile error):
"assign-rule-01.rego" (line 11, column 1):
bad assignment:

11| double(x) := ret {ret := x + x}
^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^

Functions should use `=` rather than `:=`.
4 changes: 2 additions & 2 deletions tests/golden/invalid/bad-import.rego.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
fregot (fatal parse error):
fregot (fatal parse error):
"bad-import.rego" (line 3, column 8):
parse failed:

3| import wat.foo
^
^

unexpected wat.foo (imports should start with `data.` or `input.`)
28 changes: 14 additions & 14 deletions tests/golden/invalid/builtins-01.rego.stderr
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
fregot (typecheck error):
fregot (typecheck error):
"builtins-01.rego" (line 5, column 3):
Subtype error:

5| all({"foo": true, "bar": false, "baz": 100})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Cannot use type object{
"bar": boolean,
"foo": boolean,
"baz": number
} as set{boolean}|object{any: boolean}|array{number: boolean}
Cannot use type object{
"bar": boolean,
"foo": boolean,
"baz": number
} as set{boolean}|object{any: boolean}|array{number: boolean}

fregot (typecheck error):
fregot (typecheck error):
"builtins-01.rego" (line 9, column 3):
Subtype error:

9| any({"foo": true, "bar": false, "baz": 100})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Cannot use type object{
"bar": boolean,
"foo": boolean,
"baz": number
} as set{boolean}|object{any: boolean}|array{number: boolean}
Cannot use type object{
"bar": boolean,
"foo": boolean,
"baz": number
} as set{boolean}|object{any: boolean}|array{number: boolean}
12 changes: 6 additions & 6 deletions tests/golden/invalid/builtins-02.rego.stderr
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
fregot (typecheck error):
fregot (typecheck error):
"builtins-02.rego" (line 5, column 3):
Subtype error:

5| 0 - set()
^^^^^^^^^
^^^^^^^^^

Cannot use type number as set{any}
Cannot use type number as set{any}

fregot (typecheck error):
fregot (typecheck error):
"builtins-02.rego" (line 9, column 3):
Subtype error:

9| set() - 0
^^^^^^^^^
^^^^^^^^^

Cannot use type number as set{any}
Cannot use type number as set{any}
12 changes: 6 additions & 6 deletions tests/golden/invalid/default-index.rego.stderr
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
fregot (compile error):
fregot (compile error):
"default-index.rego" (line 5, column 1):
complete definition mismatch:

5| power_level[character] = level {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

generates an object

"default-index.rego" (line 3, column 1):
complete definition mismatch:

3| default power_level = 9001
^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^

is a complete rule

fregot (typecheck error):
fregot (typecheck error):
"default-index.rego" (line 5, column 26):
Unbound variables:

5| power_level[character] = level {
^^^^^
^^^^^

The variable level is referenced, but it is never assigned a value
The variable level is referenced, but it is never assigned a value
8 changes: 4 additions & 4 deletions tests/golden/invalid/inconsistent.rego.stdout
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
passed: 0, failed: 0, errored: 1
fregot.tests.invalid.inconsistent.test_a:
fregot (eval error):
fregot (eval error):
"inconsistent.rego" (line 4, column 1):
inconsistent result:

4| a = 2
^^^^^
^^^^^

Inconsistent result for complete rule, but got:
2
2
And:
1
1
4 changes: 2 additions & 2 deletions tests/golden/invalid/missing-package-decl.rego.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
fregot (fatal parse error):
fregot (fatal parse error):
"missing-package-decl.rego" (line 1, column 1):
parse failed:

1| default allow = false
^
^

unexpected keyword 'default'
expecting 'package' declaration
14 changes: 7 additions & 7 deletions tests/golden/invalid/obj-01.rego.stdout
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
passed: 0, failed: 0, errored: 1
fregot.tests.invalid.obj_01.test_inconsistency:
fregot (eval error):
fregot (eval error):
"obj-01.rego" (line 7, column 3):
inconsistent object:

7| inconsistent == {"hi": 1}
^^^^^^^^^^^^
^^^^^^^^^^^^

Object key-value pairs must be consistent, but got:
1
1
And:
2
2
For key:
"hi"
"hi"

Stack trace:
rule fregot.tests.invalid.obj_01.inconsistent at obj-01.rego:7:3
rule fregot.tests.invalid.obj_01.test_inconsistency at tests:1:1
rule fregot.tests.invalid.obj_01.inconsistent at obj-01.rego:7:3
rule fregot.tests.invalid.obj_01.test_inconsistency at tests:1:1
4 changes: 2 additions & 2 deletions tests/golden/invalid/recursive-01.rego.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
fregot (recursion check error):
fregot (recursion check error):
"recursive-01.rego" (line 3, column 1):
rule is recursive:

3| foo {
^^^
^^^

This rule is recursive.
4 changes: 2 additions & 2 deletions tests/golden/invalid/recursive-02.rego.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
fregot (recursion check error):
fregot (recursion check error):
"recursive-02.rego" (line 3, column 1):
rule is recursive:

3| foo {
^^^
^^^

This rule is recursive.
26 changes: 13 additions & 13 deletions tests/golden/invalid/recursive-03.rego.stderr
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
fregot (recursion check error):
fregot (recursion check error):
"recursive-03.rego" (line 3, column 1):
rules are recursive:

3| foo {
^^^
^^^

These rules are mutually recursive:
foo
bar
qux
foo
bar
qux

"recursive-03.rego" (line 7, column 1):
rules are recursive:

7| bar {
^^^
^^^

These rules are mutually recursive:
foo
bar
qux
foo
bar
qux

"recursive-03.rego" (line 11, column 1):
rules are recursive:

11| qux {
^^^
^^^

These rules are mutually recursive:
foo
bar
qux
foo
bar
qux
12 changes: 6 additions & 6 deletions tests/golden/invalid/stack-trace-01.rego.stdout
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
passed: 0, failed: 0, errored: 1
fregot.tests.invalid.stack_trace_01.test_stack_trace:
fregot (eval error):
fregot (eval error):
"stack-trace-01.rego" (line 5, column 3):
builtin type error:

5| x + input.msg with input as {"msg": "crash"}
^^^^^^^^^^^^^
^^^^^^^^^^^^^

Expected number but got string

Stack trace:
rule fregot.tests.invalid.stack_trace_01.rule1 at stack-trace-01.rego:9:3
rule fregot.tests.invalid.stack_trace_01.rule2 at stack-trace-01.rego:13:3
rule fregot.tests.invalid.stack_trace_01.rule3 at stack-trace-01.rego:17:3
rule fregot.tests.invalid.stack_trace_01.test_stack_trace at tests:1:1
rule fregot.tests.invalid.stack_trace_01.rule1 at stack-trace-01.rego:9:3
rule fregot.tests.invalid.stack_trace_01.rule2 at stack-trace-01.rego:13:3
rule fregot.tests.invalid.stack_trace_01.rule3 at stack-trace-01.rego:17:3
rule fregot.tests.invalid.stack_trace_01.test_stack_trace at tests:1:1
Loading

0 comments on commit 2c2e16a

Please sign in to comment.