packages feed

hformat 0.3.2.0 → 0.3.3.0

raw patch · 3 files changed

+23/−2 lines, 3 filesPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

API changes (from Hackage documentation)

- Text.Format: instance Data.Semigroup.Semigroup Text.Format.Formatted
+ Text.Format: getNamedArguments :: Format -> [String]
+ Text.Format: instance GHC.Base.Semigroup Text.Format.Formatted

Files

hformat.cabal view
@@ -1,5 +1,5 @@ name:                hformat
-version:             0.3.2.0
+version:             0.3.3.0
 synopsis:            Simple Haskell formatting
 description: String formatting
 homepage:            http://github.com/mvoidex/hformat
src/Text/Format.hs view
@@ -15,7 +15,7 @@ module Text.Format (
 	FormattedPart(..), Formatted(..), withFlags,
 	FormatArg(..), Format(..), Formatter(..),
-	prebuild, build,
+	prebuild, build, getNamedArguments,
 	Formattable(..), Hole(..), fmt, FormatResult(..),
 	format, formats, (~~), (~%),
 
@@ -131,6 +131,24 @@ 				return $ fval fmtCfgs
 			isPos (FormatPos _) = True
 			isPos _ = False
+
+getNamedArguments ∷ Format → [String]
+getNamedArguments = nub ∘ getNamedArguments'
+	where
+		getNamedArguments' ∷ Format → [String]
+		getNamedArguments' (Format "" _) = mempty
+		getNamedArguments' (Format ('{':'{':fstr') args) = getNamedArguments' (Format fstr' args)
+		getNamedArguments' (Format ('}':'}':fstr') args) = getNamedArguments' (Format fstr' args)
+		getNamedArguments' (Format ('{':'}':fstr') args) = getNamedArguments' (Format fstr' args)
+		getNamedArguments' (Format fstr@('{':fstr') args) = case reads fstr of
+			[] → error $ "Can't parse formatter at " ++ fstr'
+			(f, fstr''):_ → getNamedArgument f `mappend` getNamedArguments' (Format fstr'' args)
+			where
+		getNamedArguments' (Format fstr' args) = getNamedArguments' (Format fstr'' args) where
+			(_, fstr'') = break (∈ "{}") fstr'
+		getNamedArgument ∷ Formatter → [String]
+		getNamedArgument (Formatter (Left name) _ _) = [name]
+		getNamedArgument _ = mempty
 
 -- | Formattable class, by default using @show@
 class Formattable a where
tests/Test.hs view
@@ -43,6 +43,9 @@ 	describe "prebuild" $
 		it "should show partially formatted" $
 			show (format "{0} ≡ {1}" ~~ str "foo" ∷ Format) ≡ str "foo ≡ {1}"
+	describe "list of named arguments" $
+		it "should list the named arguments" $
+			getNamedArguments "Positional arguments are ignored: {0} {} {2}. I want just the following, without repetition: {foo}, {bar} and {foo}." ≡ ["foo", "bar"]
 
 str ∷ String → String
 str = id