From 4658cc95745dbdffd7bc1be2e61fa463b28b4a16 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 21 Jun 2015 19:40:40 +0200 Subject: Added adjmix --- adjmix/Adjmix.hs | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 adjmix/Adjmix.hs (limited to 'adjmix/Adjmix.hs') diff --git a/adjmix/Adjmix.hs b/adjmix/Adjmix.hs new file mode 100644 index 0000000..49b820f --- /dev/null +++ b/adjmix/Adjmix.hs @@ -0,0 +1,71 @@ +{-# LANGUAGE RecordWildCards #-} + +import Options.Applicative + +import System.FilePath + +import Data.Char + +import Trivmix.Types + +data Options = Options + { baseDirectory :: FilePath + , targetDirectory :: FilePath + , levelFile :: FilePath + , adjustment :: Adjustment Level + } +optionParser :: Parser Options +optionParser = Options + <$> strOption ( long "base" + <> metavar "DIRECTORY" + <> value "/dev/shm/mix" + ) + <*> strOption ( long "target" + <> short 't' + <> metavar "DIRECTORY" + <> help "Directory relative to ‘--base’ containing the level file" + ) + <*> strOption ( long "level" + <> metavar "FILENAME" + <> value "level" + <> help "Filename of the level file" + ) + <*> ( ( Set <$> option auto ( long "set" + <> short 'o' + <> metavar "LEVEL" + ) + ) + <|> ( Add <$> option auto ( long "add" + <> short 'a' + <> metavar "LEVEL" + ) + ) + <|> ( Sub <$> option auto ( long "sub" + <> short 's' + <> metavar "LEVEL" + ) + ) + ) + +main :: IO () +main = execParser opts >>= adjmix + where + opts = info (helper <*> optionParser) + ( fullDesc + <> progDesc "Adjust the level file of a trivmix" + <> header "Adjmix — A trivial interface to a trivial mixer" + ) + +adjmix :: Options -> IO () +adjmix Options{..} = do + oldLevel <- readFile levelFile >>= readIO . stripSpace + let + newLevel = oldLevel `doAdjustment` adjustment + writeFile levelFile (show newLevel ++ "\n") + where + levelFile = baseDirectory targetDirectory levelFile + stripSpace = reverse . stripSpace' . reverse . stripSpace' + stripSpace' [] = [] + stripSpace' l@(x:xs) = if isSpace x + then stripSpace' xs + else l -- cgit v1.2.3