diff options
-rw-r--r-- | scratch/brilliant/App.hs | 8 | ||||
-rw-r--r-- | scratch/brilliant/Main.hs | 36 | ||||
-rw-r--r-- | scratch/brilliant/README.md | 31 | ||||
-rw-r--r-- | scratch/brilliant/Spec.hs | 8 |
4 files changed, 51 insertions, 32 deletions
diff --git a/scratch/brilliant/App.hs b/scratch/brilliant/App.hs index 3801eac7a803..dd3f2788a254 100644 --- a/scratch/brilliant/App.hs +++ b/scratch/brilliant/App.hs @@ -8,7 +8,7 @@ import Utils ((|>)) import qualified Utils -------------------------------------------------------------------------------- -transform :: Transform -> Keyboard -> Keyboard -transform HorizontalFlip (Keyboard xs) = xs |> fmap reverse |> Keyboard -transform VerticalFlip (Keyboard xs) = xs |> reverse |> Keyboard -transform (Shift n) (Keyboard xs) = xs |> fmap (Utils.rotate n) |> Keyboard +transform :: Keyboard -> Transform -> Keyboard +transform (Keyboard xs) HorizontalFlip = xs |> fmap reverse |> Keyboard +transform (Keyboard xs) VerticalFlip = xs |> reverse |> Keyboard +transform (Keyboard xs) (Shift n) = xs |> fmap (Utils.rotate n) |> Keyboard diff --git a/scratch/brilliant/Main.hs b/scratch/brilliant/Main.hs index 999771f55836..4cc6ac1101c2 100644 --- a/scratch/brilliant/Main.hs +++ b/scratch/brilliant/Main.hs @@ -5,41 +5,29 @@ module Main where import Options.Applicative import Data.Semigroup ((<>)) -import qualified System.Environment as Env +import qualified Transforms +import qualified Keyboard +import qualified App -------------------------------------------------------------------------------- data CommandArgs = CommandArgs - { hello :: String - , quiet :: Bool - , enthusiasm :: Int + { transforms :: String } deriving (Eq, Show) parseArgs :: Parser CommandArgs parseArgs = CommandArgs <$> strOption - ( long "hello" - <> metavar "TARGET" - <> help "Target for the greeting" ) - <*> switch - ( long "quiet" - <> short 'q' - <> help "Whether to be quiet" ) - <*> option auto - ( long "enthusiasm" - <> help "How enthusiastic to greet" - <> showDefault - <> value 1 - <> metavar "INT" ) + ( long "transforms" + <> short 't' + <> help "String of transforms where (e.g. \"HHVS12VHVHS3\")" ) main :: IO () main = do - args <- execParser opts - greet args + CommandArgs{..} <- execParser opts + case Transforms.fromString transforms of + Nothing -> putStrLn "You must provide valid input (e.g. \"HHVS12VHVHS3\")" + Just xs -> print $ foldl App.transform Keyboard.qwerty xs where opts = info (parseArgs <**> helper) ( fullDesc - <> progDesc "Print a greeting for TARGET" - <> header "header - a test for optparse-applicative" ) - -greet :: CommandArgs -> IO () -greet CommandArgs{..} = putStrLn $ "Hello, " ++ hello ++ replicate enthusiasm '!' + <> progDesc "Transform a QWERTY keyboard using a string of commands") diff --git a/scratch/brilliant/README.md b/scratch/brilliant/README.md new file mode 100644 index 000000000000..89042564e28f --- /dev/null +++ b/scratch/brilliant/README.md @@ -0,0 +1,31 @@ +# Transform QWERTY + +Apply a series of transforms to a QWERTY keyboard. + +## Usage + +To run the program, enter the following: + +```shell +$ runhaskell Main.hs --help +Usage: Main.hs (-t|--transforms ARG) + Transform a QWERTY keyboard using a string of commands + +Available options: + -t,--transforms ARG String of transforms where (e.g. "HHVS12VHVHS3") + -h,--help Show this help text +``` + +For example: + +```shell +$ runhaskell Main.hs --transforms=HHVS12VHVHS3 +[N][M][,][.][/][Z][X][C][V][B] +[H][J][K][L][;][A][S][D][F][G] +[Y][U][I][O][P][Q][W][E][R][T] +[6][7][8][9][0][1][2][3][4][5] +``` + +## Environment + +You'll need `runhaskell`, so call `nix-shell` from this project's root directory. diff --git a/scratch/brilliant/Spec.hs b/scratch/brilliant/Spec.hs index c55da72d0c37..d5ad6234ddc0 100644 --- a/scratch/brilliant/Spec.hs +++ b/scratch/brilliant/Spec.hs @@ -41,7 +41,7 @@ main = hspec $ do describe "App.transform" $ do it "flips a keyboard horizontally" $ do - App.transform HorizontalFlip Keyboard.qwerty == do + App.transform Keyboard.qwerty HorizontalFlip == do Keyboard [ reverse ['1','2','3','4','5','6','7','8','9','0'] , reverse ['Q','W','E','R','T','Y','U','I','O','P'] , reverse ['A','S','D','F','G','H','J','K','L',';'] @@ -49,7 +49,7 @@ main = hspec $ do ] it "flips a keyboard vertically" $ do - App.transform VerticalFlip Keyboard.qwerty == do + App.transform Keyboard.qwerty VerticalFlip == do Keyboard $ reverse [ ['1','2','3','4','5','6','7','8','9','0'] , ['Q','W','E','R','T','Y','U','I','O','P'] , ['A','S','D','F','G','H','J','K','L',';'] @@ -57,7 +57,7 @@ main = hspec $ do ] it "shifts a keyboard N times" $ do - App.transform (Shift 2) Keyboard.qwerty == do + App.transform Keyboard.qwerty (Shift 2) == do Keyboard $ [ Utils.rotate 2 ['1','2','3','4','5','6','7','8','9','0'] , Utils.rotate 2 ['Q','W','E','R','T','Y','U','I','O','P'] , Utils.rotate 2 ['A','S','D','F','G','H','J','K','L',';'] @@ -65,7 +65,7 @@ main = hspec $ do ] it "shifts negative amounts" $ do - App.transform (Shift (-3)) Keyboard.qwerty == do + App.transform Keyboard.qwerty (Shift (-3)) == do Keyboard $ [ Utils.rotate (-3) ['1','2','3','4','5','6','7','8','9','0'] , Utils.rotate (-3) ['Q','W','E','R','T','Y','U','I','O','P'] , Utils.rotate (-3) ['A','S','D','F','G','H','J','K','L',';'] |