From be9299d2dc8e0a9e76fe6f07cabb15c144999710 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Mon, 18 Jul 2016 00:43:21 +0300 Subject: refactoring --- apps/AddPath.hs | 20 +++++++-------- apps/Banner.hs | 31 +++++++++++++++++++++++ apps/FixNtSymbolPath.hs | 21 ++++++++-------- apps/Prompt.hs | 46 ++++++++++++++++++++++++++++++++++ apps/RemovePath.hs | 13 +++++----- apps/SetEnv.hs | 19 +++++++------- apps/UnsetEnv.hs | 19 +++++++------- apps/Utils.hs | 66 ------------------------------------------------- 8 files changed, 125 insertions(+), 110 deletions(-) create mode 100644 apps/Banner.hs create mode 100644 apps/Prompt.hs delete mode 100644 apps/Utils.hs (limited to 'apps') diff --git a/apps/AddPath.hs b/apps/AddPath.hs index db0dcc2..343207c 100644 --- a/apps/AddPath.hs +++ b/apps/AddPath.hs @@ -9,12 +9,12 @@ module Main (main) where import Control.Monad (void, when) import Data.List (union) import Data.Maybe (fromMaybe) -import Text.Printf (printf) import Options.Applicative import qualified Windows.Environment as Env -import qualified Utils +import Banner +import Prompt data Options = Options { optName :: Env.VarName @@ -56,18 +56,18 @@ addPath options = do let newPaths = union oldPaths pathsToAdd when (length oldPaths /= length newPaths) $ do let newValue = Env.pathJoin newPaths - let promptBanner = Utils.engraveBanner profile varName oldValue newValue - void $ prompt promptBanner $ Env.engrave profile varName newValue + let banner = engraveBanner profile varName oldValue newValue + void $ prompt banner $ Env.engrave profile varName newValue where varName = optName options pathsToAdd = optPaths options forAllUsers = optGlobal options - profile = if forAllUsers - then Env.AllUsers - else Env.CurrentUser + profile + | forAllUsers = Env.AllUsers + | otherwise = Env.CurrentUser skipPrompt = optYes options - prompt = if skipPrompt - then const Utils.withoutPrompt - else Utils.withPrompt + prompt + | skipPrompt = const withoutPrompt + | otherwise = withPrompt diff --git a/apps/Banner.hs b/apps/Banner.hs new file mode 100644 index 0000000..c25741b --- /dev/null +++ b/apps/Banner.hs @@ -0,0 +1,31 @@ +{- + - Copyright 2016 Egor Tensin + - This file is licensed under the terms of the MIT License. + - See LICENSE.txt for details. +-} + +module Banner + ( engraveBanner + , wipeBanner + ) where + +import Data.Maybe (fromJust, isJust) +import Text.Printf (printf) + +import qualified Windows.Environment as Env + +engraveBanner :: Env.Profile -> Env.VarName -> Maybe Env.VarValue -> Env.VarValue -> String +engraveBanner profile name oldValue newValue = + warning ++ valuesStr + where + warning = printf "Saving variable '%s' to '%s'...\n" name (Env.profileKeyPath profile) + valuesStr + | isJust oldValue = oldValueStr ++ newValueStr + | otherwise = theValueStr + oldValueStr = printf "\tOld value: %s\n" $ fromJust oldValue + newValueStr = printf "\tNew value: %s\n" newValue + theValueStr = printf "\tValue: %s\n" newValue + +wipeBanner :: Env.Profile -> Env.VarName -> String +wipeBanner profile name = + printf "Deleting variable '%s' from '%s'...\n" name (Env.profileKeyPath profile) diff --git a/apps/FixNtSymbolPath.hs b/apps/FixNtSymbolPath.hs index 8c999a8..219ceb2 100644 --- a/apps/FixNtSymbolPath.hs +++ b/apps/FixNtSymbolPath.hs @@ -15,7 +15,8 @@ import System.FilePath (combine) import Options.Applicative import qualified Windows.Environment as Env -import qualified Utils +import Banner +import Prompt data Options = Options { optYes :: Bool @@ -72,22 +73,22 @@ fixNtSymbolPath options = do let newPaths = union oldPaths $ dirPaths remoteDirs when (length oldPaths /= length newPaths) $ do let newValue = Env.pathJoin newPaths - let promptBanner = Utils.engraveBanner profile varName oldValue newValue - confirmed <- prompt promptBanner $ Env.engrave profile varName newValue - when confirmed $ + let banner = engraveBanner profile varName oldValue newValue + agreed <- prompt banner $ Env.engrave profile varName newValue + when agreed $ createDirs localDirs where varName = "_NT_SYMBOL_PATH" forAllUsers = optGlobal options - profile = if forAllUsers - then Env.AllUsers - else Env.CurrentUser + profile + | forAllUsers = Env.AllUsers + | otherwise = Env.CurrentUser skipPrompt = optYes options - prompt = if skipPrompt - then const Utils.withoutPrompt - else Utils.withPrompt + prompt + | skipPrompt = const withoutPrompt + | otherwise = withPrompt main :: IO () main = execParser parser >>= fixNtSymbolPath diff --git a/apps/Prompt.hs b/apps/Prompt.hs new file mode 100644 index 0000000..97c23fa --- /dev/null +++ b/apps/Prompt.hs @@ -0,0 +1,46 @@ +{- + - Copyright 2015 Egor Tensin + - This file is licensed under the terms of the MIT License. + - See LICENSE.txt for details. +-} + +module Prompt + ( withPrompt + , withoutPrompt + ) where + +import Control.Monad (liftM, void, when) +import Data.Char (toLower) +import System.IO (hFlush, stdout) + +prompt :: String -> IO String +prompt banner = do + putStr banner + hFlush stdout + getLine + +promptYesNo :: String -> IO Bool +promptYesNo banner = do + response <- liftM (map toLower) $ prompt banner + if response `elem` yeses + then return True + else if response `elem` noes + then return False + else promptToContinue + where + yeses = ["y", "yes"] + noes = ["n", "no"] + +promptToContinue :: IO Bool +promptToContinue = promptYesNo "Continue? (y/n) " + +withPrompt :: String -> IO a -> IO Bool +withPrompt banner m = do + putStr banner + hFlush stdout + agreed <- promptToContinue + when agreed $ void m + return agreed + +withoutPrompt :: IO a -> IO Bool +withoutPrompt m = m >> return True diff --git a/apps/RemovePath.hs b/apps/RemovePath.hs index ecc56c0..871ebef 100644 --- a/apps/RemovePath.hs +++ b/apps/RemovePath.hs @@ -13,7 +13,8 @@ import Data.Maybe (fromJust, isJust) import Options.Applicative import qualified Windows.Environment as Env -import qualified Utils +import Banner +import Prompt data Options = Options { optName :: Env.VarName @@ -65,10 +66,10 @@ removePath options = do let newPaths = oldPaths \\ pathsToRemove when (length oldPaths /= length newPaths) $ do let newValue = Env.pathJoin newPaths - let promptBanner = Utils.engraveBanner profile varName oldValue newValue - void $ prompt promptBanner $ Env.engrave profile varName newValue + let banner = engraveBanner profile varName oldValue newValue + void $ prompt banner $ Env.engrave profile varName newValue skipPrompt = optYes options - prompt = if skipPrompt - then const Utils.withoutPrompt - else Utils.withPrompt + prompt + | skipPrompt = const withoutPrompt + | otherwise = withPrompt diff --git a/apps/SetEnv.hs b/apps/SetEnv.hs index a48fbe6..e94e350 100644 --- a/apps/SetEnv.hs +++ b/apps/SetEnv.hs @@ -11,7 +11,8 @@ import Control.Monad (void) import Options.Applicative import qualified Windows.Environment as Env -import qualified Utils +import Banner +import Prompt data Options = Options { optYes :: Bool @@ -47,19 +48,19 @@ main = execParser parser >>= setEnv fullDesc <> progDesc "Set environment variable" setEnv :: Options -> IO () -setEnv options = void $ prompt confirmationBanner $ Env.engrave profile varName varValue +setEnv options = void $ prompt banner $ Env.engrave profile varName varValue where - confirmationBanner = Utils.engraveBanner profile varName Nothing varValue + banner = engraveBanner profile varName Nothing varValue varName = optName options varValue = optValue options forAllUsers = optGlobal options - profile = if forAllUsers - then Env.AllUsers - else Env.CurrentUser + profile + | forAllUsers = Env.AllUsers + | otherwise = Env.CurrentUser skipPrompt = optYes options - prompt = if skipPrompt - then const Utils.withoutPrompt - else Utils.withPrompt + prompt + | skipPrompt = const withoutPrompt + | otherwise = withPrompt diff --git a/apps/UnsetEnv.hs b/apps/UnsetEnv.hs index 88101d9..eebef00 100644 --- a/apps/UnsetEnv.hs +++ b/apps/UnsetEnv.hs @@ -11,7 +11,8 @@ import Control.Monad (void) import Options.Applicative import qualified Windows.Environment as Env -import qualified Utils +import Banner +import Prompt data Options = Options { optYes :: Bool @@ -42,18 +43,18 @@ main = execParser parser >>= unsetEnv fullDesc <> progDesc "Unset environment variable" unsetEnv :: Options -> IO () -unsetEnv options = void $ prompt confirmationBanner $ Env.wipe profile varName +unsetEnv options = void $ prompt banner $ Env.wipe profile varName where - confirmationBanner = Utils.wipeBanner profile varName + banner = wipeBanner profile varName varName = optName options forAllUsers = optGlobal options - profile = if forAllUsers - then Env.AllUsers - else Env.CurrentUser + profile + | forAllUsers = Env.AllUsers + | otherwise = Env.CurrentUser skipPrompt = optYes options - prompt = if skipPrompt - then const Utils.withoutPrompt - else Utils.withPrompt + prompt + | skipPrompt = const withoutPrompt + | otherwise = withPrompt diff --git a/apps/Utils.hs b/apps/Utils.hs deleted file mode 100644 index e34950f..0000000 --- a/apps/Utils.hs +++ /dev/null @@ -1,66 +0,0 @@ -{- - - Copyright 2015 Egor Tensin - - This file is licensed under the terms of the MIT License. - - See LICENSE.txt for details. --} - -module Utils - ( withPrompt - , withoutPrompt - - , engraveBanner - , wipeBanner - ) where - -import Control.Monad (liftM, void, when) -import Data.Maybe (fromJust, isJust) -import Data.Char (toLower) -import System.IO (hFlush, stdout) -import Text.Printf (printf) - -import Windows.Environment (Profile, profileKeyPath, VarName, VarValue) - -prompt :: String -> IO String -prompt banner = do - putStr banner - hFlush stdout - getLine - -promptYesNo :: String -> IO Bool -promptYesNo banner = do - response <- liftM (map toLower) $ prompt banner - if response `elem` yeses - then return True - else if response `elem` noes - then return False - else promptToContinue - where - yeses = ["y", "yes"] - noes = ["n", "no"] - -promptToContinue :: IO Bool -promptToContinue = promptYesNo "Continue? (y/n) " - -withPrompt :: String -> IO a -> IO Bool -withPrompt banner m = do - putStr banner - hFlush stdout - agreed <- promptToContinue - when agreed $ void m - return agreed - -withoutPrompt :: IO a -> IO Bool -withoutPrompt m = m >> return True - -engraveBanner :: Profile -> VarName -> Maybe VarValue -> VarValue -> String -engraveBanner profile name oldValue newValue = - header ++ values - where - header = printf "Saving variable '%s' to '%s'...\n" name (profileKeyPath profile) - values = if isJust oldValue - then printf "\tOld value: %s\n\tNew value: %s\n" (fromJust oldValue) newValue - else printf "\tValue: %s\n" newValue - -wipeBanner :: Profile -> VarName -> String -wipeBanner profile name = - printf "Deleting variable '%s' from '%s'...\n" name (profileKeyPath profile) -- cgit v1.2.3