Skip to content

Commit

Permalink
[ style ] adhere to coding style (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-hoeck authored Aug 21, 2023
1 parent 9e719d2 commit ec8d82a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 75 deletions.
34 changes: 19 additions & 15 deletions src/Fix.idr
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ noTrailingNewlines = reverse . dropWhile1 (\x => null x) . cons [] . reverse
unlinesImpl : List1 (List Char) -> List Char
unlinesImpl ([] ::: []) = [NL]
unlinesImpl (h ::: t) = h ++ run t
where run : List (List Char) -> List Char
run [] = []
run (cs :: css) = NL :: cs ++ run css

where
run : List (List Char) -> List Char
run [] = []
run (cs :: css) = NL :: cs ++ run css


removeCRLF : List Char -> List Char
Expand All @@ -33,11 +35,12 @@ removeCRLF ('\r' :: '\n' :: xs) = '\n' :: removeCRLF xs
removeCRLF (x :: xs) = x :: removeCRLF xs

transformImpl : List Char -> List Char
transformImpl = unlinesImpl
. noTrailingNewlines
. map noTrailingSpace
. split isNL
. removeCRLF
transformImpl =
unlinesImpl
. noTrailingNewlines
. map noTrailingSpace
. split isNL
. removeCRLF

||| Transforms the given string in the following way:
||| * on every line, trailing whitespace characters are removed
Expand Down Expand Up @@ -89,13 +92,14 @@ transTest5 : TestTransformImpl "test\r\ntrailing" = "test\ntrailing\n"
transTest5 = Refl

Empties : List String
Empties = [ ""
, "\n"
, "\n\n"
, "\n\n\n"
, " \n \n\n"
, " \n \n\n\t"
]
Empties =
[ ""
, "\n"
, "\n\n"
, "\n\n\n"
, " \n \n\n"
, " \n \n\n\t"
]

testEmpties : map TestTransformImpl Empties = replicate (length Empties) "\n"
testEmpties = Refl
15 changes: 8 additions & 7 deletions src/Main.idr
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ Prog : Type -> Type
Prog t = ReaderT Config (EitherT (List String) IO) t

run : Config -> Prog () -> IO ()
run c (MkReaderT f) = do Right () <- runEitherT (f c)
| Left es => traverse_ putStrLn es
pure ()
run c (MkReaderT f) = do
Right () <- runEitherT (f c) | Left es => traverse_ putStrLn es
pure ()

--------------------------------------------------------------------------------
-- Logging
Expand Down Expand Up @@ -54,10 +54,11 @@ throwOne e = throwError [e]
-- File Handling
--------------------------------------------------------------------------------

tryFile : (mod : String)
-> (String -> Prog (Either FileError a))
-> (pth : FilePath)
-> Prog a
tryFile :
(mod : String)
-> (String -> Prog (Either FileError a))
-> (pth : FilePath)
-> Prog a
tryFile mod p pth = do
trace $ "\{mod} \{pth}"
Right a <- p (interpolate pth)
Expand Down
112 changes: 59 additions & 53 deletions src/Options.idr
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,16 @@ record Config where
%runElab derive "Config" [Show,Eq,Pretty]

init : List String -> Config
init fs = MkConfig {
printHelp = False
, verbosity = 2
, checkOnly = True
, includeAll = False
, includeHidden = False
, files = case fs of [] => ["."]; _ => map fromString fs
, extensions = ["idr"]
}
init fs =
MkConfig
{ printHelp = False
, verbosity = 2
, checkOnly = True
, includeAll = False
, includeHidden = False
, files = case fs of [] => ["."]; _ => map fromString fs
, extensions = ["idr"]
}

||| Tests, whether a file or directory is hidden and should
||| still be included.
Expand Down Expand Up @@ -107,42 +108,46 @@ setHidden : Config -> Config
setHidden = {includeHidden := True}

descs : List $ OptDescr (Config -> Config)
descs = [ MkOpt ['h'] ["help"] (NoArg help)
"prints this help text\n "
, MkOpt ['v'] ["verbose"] (NoArg $ adjVerbosity S)
"increase verbosity (default verbosity is 2)\n "
, MkOpt ['q'] ["quiet"] (NoArg $ adjVerbosity pred)
"decrease verbosity (default verbosity is 2)\n "
, MkOpt ['c'] ["check"] (NoArg $ doFix False)
"check and list files with issues (default)\n "
, MkOpt ['f'] ["fix"] (NoArg $ doFix True)
"check and fix files with issues\n "
, MkOpt ['e'] ["ext"] (ReqArg setExts "<exts>")
$ unlines [ "comma separated list of extensions of files"
, "to be included when traversing directories"
, "(default is \"idr\")."
, "Note: Files whose name starts with a dot will be"
, "ignored unless '--includeHidden' is set."
, ""
]
, MkOpt ['a'] ["all"] (NoArg setAll)
"include all non-hidden files when traversing directories\n "
, MkOpt [] ["includeHidden"] (NoArg setHidden)
$ unlines [ "include hidden files (name starts with a dot)"
, "when traversing directories"
, ""
]
descs =
[ MkOpt ['h'] ["help"] (NoArg help)
"prints this help text\n "
, MkOpt ['v'] ["verbose"] (NoArg $ adjVerbosity S)
"increase verbosity (default verbosity is 2)\n "
, MkOpt ['q'] ["quiet"] (NoArg $ adjVerbosity pred)
"decrease verbosity (default verbosity is 2)\n "
, MkOpt ['c'] ["check"] (NoArg $ doFix False)
"check and list files with issues (default)\n "
, MkOpt ['f'] ["fix"] (NoArg $ doFix True)
"check and fix files with issues\n "
, MkOpt ['e'] ["ext"] (ReqArg setExts "<exts>") $
unlines
[ "comma separated list of extensions of files"
, "to be included when traversing directories"
, "(default is \"idr\")."
, "Note: Files whose name starts with a dot will be"
, "ignored unless '--includeHidden' is set."
, ""
]
, MkOpt ['a'] ["all"] (NoArg setAll)
"include all non-hidden files when traversing directories\n "
, MkOpt [] ["includeHidden"] (NoArg setHidden) $
unlines
[ "include hidden files (name starts with a dot)"
, "when traversing directories"
, ""
]
]

export
applyArgs : List String -> Either (List String) Config
applyArgs args =
case getOpt RequireOrder descs args of
MkResult os n [] [] => Right $ foldl (flip apply) (init n) os
MkResult _ _ u e => Left $ map unknown u ++ e
MkResult os n [] [] => Right $ foldl (flip apply) (init n) os
MkResult _ _ u e => Left $ map unknown u ++ e

where unknown : String -> String
unknown = ("Unknown option: " ++)
where
unknown : String -> String
unknown = ("Unknown option: " ++)

--------------------------------------------------------------------------------
-- Usage Info
Expand All @@ -158,21 +163,22 @@ usage : String
usage = "Usage: " ++ progName ++ " [options] [FILES]\n\nOptions:\n"

synopsis : String
synopsis = unlines [
progName ++ " version " ++ version
, ""
, " Removes trailing whitespace characters from the specified"
, " text files making sure every text file ends with exactly one"
, " newline character. Windows style line breaks are replaced"
, " by Unix newline characters."
, ""
, " If the passed file list contains directories, " ++ progName
, " will recursively adjust all files with the given extensions"
, " (see option --ext) in those directories. If no files are"
, " specified, the current directory will be traversed instead."
, ""
, usage
]
synopsis =
unlines
[ progName ++ " version " ++ version
, ""
, " Removes trailing whitespace characters from the specified"
, " text files making sure every text file ends with exactly one"
, " newline character. Windows style line breaks are replaced"
, " by Unix newline characters."
, ""
, " If the passed file list contains directories, " ++ progName
, " will recursively adjust all files with the given extensions"
, " (see option --ext) in those directories. If no files are"
, " specified, the current directory will be traversed instead."
, ""
, usage
]

export
info : String
Expand Down

0 comments on commit ec8d82a

Please sign in to comment.