fmt 0.0.0.1 → 0.0.0.2
raw patch · 6 files changed
+222/−28 lines, 6 filesdep +fmtdep +hspecPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
Dependencies added: fmt, hspec
API changes (from Hackage documentation)
+ Fmt: (%<<) :: (Show a, FromBuilder b) => Builder -> a -> b
+ Fmt: (>%%<) :: (Buildable a, FromBuilder b) => Builder -> a -> b
+ Fmt: (>%%<<) :: (Show a, FromBuilder b) => Builder -> a -> b
+ Fmt: (>>%%<) :: (Buildable a, FromBuilder b) => Builder -> a -> b
+ Fmt: (>>%%<<) :: (Show a, FromBuilder b) => Builder -> a -> b
+ Fmt: (>>%) :: (FromBuilder b) => Builder -> Builder -> b
+ Fmt: infixl 1 >>%%<
+ Fmt.IO: instance a ~ () => Fmt.FromBuilder (GHC.Types.IO a)
Files
- CHANGELOG.md +16/−2
- LICENSE +26/−16
- fmt.cabal +14/−2
- lib/Fmt.hs +76/−8
- lib/Fmt/IO.hs +20/−0
- tests/Main.hs +70/−0
CHANGELOG.md view
@@ -1,3 +1,17 @@-# 0.1.0.0+# 0.0.0.2 -First release.+* Added `>%%<` so that it'd be possible to write `%<a>%%<b>%` instead of weird `%<a%<b>%`.++* Added `%<< ... >>%`, which work work `Show` instead of `Buildable`. If you don't care about speed and just want to output something, use them.++* Added an `IO ()` instance in `Fmt.IO`. If you import that module, raw formatted strings would print themselves.++* Added tests.++* Changed fixities of operators so that `%<n+1>%` would work.++* Changed license to BSD3 since all our dependencies are BSD3 and we can't use MIT.++# 0.0.0.1++First (completely experimental) release.
LICENSE view
@@ -1,20 +1,30 @@ Copyright (c) 2016 Artyom -Permission is hereby granted, free of charge, to any person obtaining-a copy of this software and associated documentation files (the-"Software"), to deal in the Software without restriction, including-without limitation the rights to use, copy, modify, merge, publish,-distribute, sublicense, and/or sell copies of the Software, and to-permit persons to whom the Software is furnished to do so, subject to-the following conditions:+All rights reserved. -The above copyright notice and this permission notice shall be included-in all copies or substantial portions of the Software.+Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met: -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.-IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY-CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,-TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE-SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.+ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Artyom nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
fmt.cabal view
@@ -1,11 +1,11 @@ name: fmt-version: 0.0.0.1+version: 0.0.0.2 synopsis: Nice formatting library description: Nice formatting library homepage: http://github.com/aelve/fmt bug-reports: http://github.com/aelve/fmt/issues-license: MIT+license: BSD3 license-file: LICENSE author: Artyom maintainer: yom@artyom.me@@ -22,6 +22,7 @@ library exposed-modules: Fmt+ Fmt.IO -- other-modules: -- other-extensions: build-depends: base >=4.6 && <5,@@ -29,4 +30,15 @@ text-format >= 0.3 ghc-options: -Wall -fno-warn-unused-do-bind hs-source-dirs: lib+ default-language: Haskell2010++test-suite tests+ main-is: Main.hs+ type: exitcode-stdio-1.0+ build-depends: base >=4.6 && <5+ , fmt+ , hspec >= 2.2 && < 2.4+ , text+ ghc-options: -Wall -fno-warn-unused-do-bind+ hs-source-dirs: tests default-language: Haskell2010
lib/Fmt.hs view
@@ -4,17 +4,91 @@ ( (%<), (>%),+ (>%%<), + (%<<),+ (>>%),+ (>>%%<<),++ (>%%<<),+ (>>%%<),+ FromBuilder(..), ) where -import Data.Text+import qualified Data.Text as T import qualified Data.Text.Lazy as TL import Data.Text.Lazy.Builder hiding (fromString) import Data.Monoid import Data.Text.Buildable +----------------------------------------------------------------------------+-- Operators with 'Buildable'+----------------------------------------------------------------------------++(%<) :: (Buildable a, FromBuilder b) => Builder -> a -> b+(%<) x a = fromBuilder (x <> build a)++(>%) :: (FromBuilder b) => Builder -> Builder -> b+(>%) x a = fromBuilder (x <> a)++(>%%<) :: (Buildable a, FromBuilder b) => Builder -> a -> b+(>%%<) x a = fromBuilder (x <> build a)++infixl 1 %<+infixl 1 >%+infixl 1 >%%<++----------------------------------------------------------------------------+-- Operators with 'Show'+----------------------------------------------------------------------------++(%<<) :: (Show a, FromBuilder b) => Builder -> a -> b+(%<<) x a = x %< show a+{-# INLINE (%<<) #-}++(>>%) :: (FromBuilder b) => Builder -> Builder -> b+(>>%) x a = x >% a+{-# INLINE (>>%) #-}++(>>%%<<) :: (Show a, FromBuilder b) => Builder -> a -> b+(>>%%<<) x a = x %< show a+{-# INLINE (>>%%<<) #-}++infixl 1 %<<+infixl 1 >>%+infixl 1 >>%%<<++----------------------------------------------------------------------------+-- Combinations+----------------------------------------------------------------------------++(>>%%<) :: (Buildable a, FromBuilder b) => Builder -> a -> b+(>>%%<) x a = x >%%< a+{-# INLINE (>>%%<) #-}++(>%%<<) :: (Show a, FromBuilder b) => Builder -> a -> b+(>%%<<) x a = x >>%%<< a+{-# INLINE (>%%<<) #-}++infixl 1 >>%%<+infixl 1 >%%<<++-- TODO: something for indentation+-- TODO: something to format a record nicely (with generics, probably)+-- TODO: something like https://hackage.haskell.org/package/groom+-- TODO: reexport Buildable+-- TODO: write docs+-- TODO: mention printf in description so that it would be findable+-- TODO: mention things that work (<n+1>, <f n>, <show n>)+-- TODO: colors?+-- TODO: add NL for newline?+-- TODO: have to decide on whether it would be >%< or >%%< or maybe >|<+-- TODO: actually, what about |< and >|?+-- TODO: what effect does it have on compilation time? what effect do+-- other formatting libraries have on compilation time?+ class FromBuilder a where fromBuilder :: Builder -> a @@ -26,16 +100,10 @@ fromBuilder = TL.unpack . toLazyText {-# INLINE fromBuilder #-} -instance FromBuilder Text where+instance FromBuilder T.Text where fromBuilder = TL.toStrict . toLazyText {-# INLINE fromBuilder #-} instance FromBuilder TL.Text where fromBuilder = toLazyText {-# INLINE fromBuilder #-}--(%<) :: (Buildable a, FromBuilder b) => Builder -> a -> b-(%<) x a = fromBuilder (x <> build a)--(>%) :: (FromBuilder b) => Builder -> Builder -> b-(>%) x a = fromBuilder (x <> a)
+ lib/Fmt/IO.hs view
@@ -0,0 +1,20 @@+{-# LANGUAGE GADTs #-}++{-# OPTIONS_GHC -fno-warn-orphans #-}+++module Fmt.IO+(+ module Fmt,+)+where+++import Fmt+import qualified Data.Text.Lazy.Builder as TL+import qualified Data.Text.Lazy.IO as TL+++instance (a ~ ()) => FromBuilder (IO a) where+ fromBuilder = TL.putStr . TL.toLazyText+ {-# INLINE fromBuilder #-}
+ tests/Main.hs view
@@ -0,0 +1,70 @@+{-# LANGUAGE+OverloadedStrings+ #-}+++module Main where+++-- Monoid+import Data.Monoid ((<>))+-- Text+import Data.Text (Text)+import qualified Data.Text.Lazy as TL+import Data.Text.Lazy.Builder (Builder)+-- Tests+import Test.Hspec++-- Fmt+import Fmt+++main :: IO ()+main = hspec $ do+ let n = 25 :: Int+ s = "!" :: String++ it "simple examples" $ do+ ("a"%<n>%"b") ==%> "a25b"+ ("a"%<n>%"b"%<s>%"") ==%> "a25b!"+ (""%<n>%%<s>%"") ==%> "25!"+ (""%<negate n>%%<s>%"") ==%> "-25!"+ (""%<Just n>%%<s>%"") ==%> "25!"++ describe "examples with Show/mixed" $ do+ it "copy of Buildable examples" $ do+ ("a"%<<n>>%"b") ==%> "a25b"+ ("a"%<<n>>%%<<n>>%"b") ==%> "a2525b"+ -- These are mixed, i.e. both Buildable and Show versions are used+ ("a"%<<n>>%"b"%<s>%"") ==%> "a25b!"+ (""%<<n>>%%<s>%"") ==%> "25!"+ (""%<<negate n>>%%<s>%"") ==%> "-25!"+ it "examples that don't work with Buildable" $ do+ (""%<<Just n>>%"") ==%> "Just 25"+ (""%<<(n,n)>>%"") ==%> "(25,25)"++ it "plays nice with other operators" $ do+ -- If precedence is bad these won't compile+ (""%<n-1>%%<n+1>%"") ==%> "2426"+ (id $ ""%<n-1>%%<n+1>%"") ==%> "2426"++ it "works with <>" $ do+ ("number: "%<n>%"\n"<>+ "string: "%<s>%"") ==%> "number: 25\nstring: !"++ describe "output as" $ do+ it "String" $+ ("a"%<n>%"b" :: String) `shouldBe` "a25b"+ it "Text" $+ ("a"%<n>%"b" :: Text) `shouldBe` "a25b"+ it "Lazy Text" $+ ("a"%<n>%"b" :: TL.Text) `shouldBe` "a25b"+ it "Builder" $+ ("a"%<n>%"b" :: Builder) `shouldBe` "a25b"++----------------------------------------------------------------------------+-- Utilities+----------------------------------------------------------------------------++(==%>) :: Text -> Text -> Expectation+(==%>) = shouldBe