From e25b85b21ed952af2ce1dde3b99809c9fe2d4afa Mon Sep 17 00:00:00 2001 From: Denis Kuznetsov Date: Thu, 19 Sep 2019 11:44:57 +0300 Subject: [PATCH 1/4] Update ChangeLog.md Add last feature and fix typo --- ChangeLog.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index 63aea4f..97fa921 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -5,4 +5,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added - Minimalistic first implementation using a hardcoded dictionary file -- Words count ommand-line arg +- Words count command-line arg +- Colored password output From b3b465eb9c477b5aa4013498fda7f6139ecbee09 Mon Sep 17 00:00:00 2001 From: dk Date: Thu, 19 Sep 2019 22:05:36 +0300 Subject: [PATCH 2/4] Refactoring Add multicolor feature --- app/Main.hs | 24 +++++++----------------- app/PrettyAnsi.hs | 37 +++++++++++++++++++++++++++++++++++++ correct-unicorn.cabal | 1 + 3 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 app/PrettyAnsi.hs diff --git a/app/Main.hs b/app/Main.hs index ebdaded..6bfb5da 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -4,6 +4,8 @@ import Options.Applicative import Data.Semigroup ((<>)) import System.Random +import PrettyAnsi + dictionaryPath:: String dictionaryPath = "/usr/share/dict/words" @@ -27,6 +29,10 @@ main = genPassword =<< execParser opts <> progDesc "passphrase generator inspired by xkcd 936" <> header "have fun!" ) +usedColors :: [[Char]] +usedColors = [ansiGreen, ansiYellow] +-- usedColors = [ansiGreen, ansiYellow, ansiBlue, ansiMagenta] + genPassword :: Settings -> IO () genPassword (Settings count) = do content <- readFile dictionaryPath @@ -36,22 +42,6 @@ genPassword (Settings count) = do indexes = take count $ randoms stdGen :: [Int] cappedIndexes = fmap (`mod` dictSize) indexes usedWords = [dictionaryWords !! x | x <- cappedIndexes] - coloredWords = paintWords usedWords + coloredWords = paintWords usedWords usedColors output = unwords coloredWords putStrLn $ bold output - -colors :: [[Char]] -colors = ["\x1b[32m", "\x1b[33m"] - -prependColor :: (Int, [Char]) -> [Char] -prependColor (index, word) = do - let isOdd = mod index 2 - colour = colors !! isOdd - colour ++ word - -paintWords :: [[Char]] -> [[Char]] -paintWords words = fmap prependColor $ zip [0..] words - -bold :: [Char] -> [Char] -bold string = "\x1b[1m" ++ string - diff --git a/app/PrettyAnsi.hs b/app/PrettyAnsi.hs new file mode 100644 index 0000000..94607a3 --- /dev/null +++ b/app/PrettyAnsi.hs @@ -0,0 +1,37 @@ +module PrettyAnsi where + +ansiBlack :: [Char] +ansiBlack = "\x1b[30m" +ansiRed :: [Char] +ansiRed = "\x1b[31m" +ansiGreen :: [Char] +ansiGreen = "\x1b[32m" +ansiYellow :: [Char] +ansiYellow = "\x1b[33m" +ansiBlue :: [Char] +ansiBlue = "\x1b[34m" +ansiMagenta :: [Char] +ansiMagenta = "\x1b[35m" +ansiCyan :: [Char] +ansiCyan = "\x1b[36m" +ansiWhite :: [Char] +ansiWhite = "\x1b[37m" +ansiReset :: [Char] +ansiReset = "\x1b[0m" +ansiBold :: [Char] +ansiBold = "\x1b[0m" + +prependColor :: [[Char]] -> Int -> (Int, [Char]) -> [Char] +prependColor colors colorsCount (number, word)= do + let colorIndex = mod number colorsCount + color = colors !! colorIndex + color ++ word + + +paintWords :: [[Char]] -> [[Char]] -> [[Char]] +paintWords words colors = do + let mapper = prependColor colors $ length colors + fmap mapper $ zip [0..] words + +bold :: [Char] -> [Char] +bold string = ansiBold ++ string diff --git a/correct-unicorn.cabal b/correct-unicorn.cabal index afabd47..9d1cdbe 100644 --- a/correct-unicorn.cabal +++ b/correct-unicorn.cabal @@ -30,6 +30,7 @@ source-repository head executable correct-unicorn main-is: Main.hs other-modules: + PrettyAnsi Paths_correct_unicorn hs-source-dirs: app From 27af64ec371d71a32dcf847b33882ba162038798 Mon Sep 17 00:00:00 2001 From: dk Date: Fri, 20 Sep 2019 09:36:58 +0300 Subject: [PATCH 3/4] clearer version of paintWords --- app/PrettyAnsi.hs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/app/PrettyAnsi.hs b/app/PrettyAnsi.hs index 94607a3..0f698c0 100644 --- a/app/PrettyAnsi.hs +++ b/app/PrettyAnsi.hs @@ -21,17 +21,9 @@ ansiReset = "\x1b[0m" ansiBold :: [Char] ansiBold = "\x1b[0m" -prependColor :: [[Char]] -> Int -> (Int, [Char]) -> [Char] -prependColor colors colorsCount (number, word)= do - let colorIndex = mod number colorsCount - color = colors !! colorIndex - color ++ word - - paintWords :: [[Char]] -> [[Char]] -> [[Char]] -paintWords words colors = do - let mapper = prependColor colors $ length colors - fmap mapper $ zip [0..] words +paintWords words colors = + [color ++ word | (color, word) <- zip (cycle colors) words] bold :: [Char] -> [Char] bold string = ansiBold ++ string From e15f12f24f4cae9b6bab2dfae26e01291a532373 Mon Sep 17 00:00:00 2001 From: dk Date: Wed, 15 Dec 2021 14:16:18 +0300 Subject: [PATCH 4/4] update resolver --- .gitignore | 1 + stack.yaml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index de2338f..46ee412 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ cabal.project.local cabal.project.local~ .HTF/ .ghc.environment.* +stack.yaml.lock diff --git a/stack.yaml b/stack.yaml index 9b0e238..ea112c3 100644 --- a/stack.yaml +++ b/stack.yaml @@ -1,3 +1,3 @@ -resolver: lts-14.4 +resolver: ghc-8.10.7 packages: - .