diff --git a/elm.json b/elm.json index c93d1f43..2d606497 100644 --- a/elm.json +++ b/elm.json @@ -32,7 +32,8 @@ "lukewestby/elm-string-interpolate": "1.0.4 <= v < 2.0.0", "mdgriffith/elm-markup": "3.0.1 <= v < 4.0.0", "miniBill/elm-codec": "1.2.0 <= v < 2.0.0", - "noahzgordon/elm-color-extra": "1.0.2 <= v < 2.0.0" + "noahzgordon/elm-color-extra": "1.0.2 <= v < 2.0.0", + "tripokey/elm-fuzzy": "5.2.1 <= v < 6.0.0" }, "test-dependencies": { "avh4/elm-program-test": "3.1.0 <= v < 4.0.0", diff --git a/examples/external-data/elm.json b/examples/external-data/elm.json index 3a1f0391..87373855 100644 --- a/examples/external-data/elm.json +++ b/examples/external-data/elm.json @@ -30,7 +30,8 @@ "mdgriffith/elm-ui": "1.1.5", "miniBill/elm-codec": "1.2.0", "noahzgordon/elm-color-extra": "1.0.2", - "rtfeldman/elm-hex": "1.0.0" + "rtfeldman/elm-hex": "1.0.0", + "tripokey/elm-fuzzy": "5.2.1" }, "indirect": { "elm/bytes": "1.0.8", diff --git a/src/Secrets.elm b/src/Secrets.elm index df3e19ee..cf0f330f 100644 --- a/src/Secrets.elm +++ b/src/Secrets.elm @@ -2,6 +2,7 @@ module Secrets exposing (..) import BuildError exposing (BuildError) import Dict exposing (Dict) +import Fuzzy import Json.Decode as Decode exposing (Decoder) import TerminalText as Terminal @@ -51,13 +52,22 @@ buildError secretName availableEnvironmentVariables = [ Terminal.text "I expected to find this Secret in your environment variables but didn't find a match:\nSecrets.get \"" , Terminal.red (Terminal.text secretName) , Terminal.text "\"\n\n" - - -- , Terminal.text "Maybe you meant one of:\n" - -- , Terminal.text (String.join ", " availableEnvironmentVariables) + , Terminal.text "So maybe " + , Terminal.yellow <| Terminal.text (sortMatches secretName availableEnvironmentVariables |> List.head |> Maybe.withDefault "") + , Terminal.text " should be " + , Terminal.green <| Terminal.text secretName ] } +sortMatches missingSecret availableSecrets = + let + simpleMatch config separators needle hay = + Fuzzy.match config separators needle hay |> .score + in + List.sortBy (simpleMatch [] [] missingSecret) availableSecrets + + decoder : Decoder Secrets decoder = Decode.dict Decode.string diff --git a/src/TerminalText.elm b/src/TerminalText.elm index 837b5edd..13ca8e52 100644 --- a/src/TerminalText.elm +++ b/src/TerminalText.elm @@ -9,6 +9,8 @@ type Text type Color = Red | Blue + | Green + | Yellow text : String -> Text @@ -16,6 +18,16 @@ text value = RawText value +green : Text -> Text +green inner = + Style (colorToString Green) inner + + +yellow : Text -> Text +yellow inner = + Style (colorToString Yellow) inner + + red : Text -> Text red inner = Style (colorToString Red) inner @@ -49,6 +61,12 @@ colorToString color = Blue -> "[34m" + Green -> + "[32;1m" + + Yellow -> + "[33;1m" + toString : List Text -> String toString list =