safe-printf: Well-typed, flexible and variadic printf for Haskell

[ bsd3, library, text ] [ Propose Tags ] [ Report a vulnerability ]

More type-safe, flexible replacement of variadic printf for Text.Printf.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0
Dependencies base (>=4 && <5), haskell-src-meta (>=0.6 && <0.7), template-haskell (>=2.9 && <2.11), th-lift (>=0.6 && <0.8) [details]
License BSD-3-Clause
Copyright (c) Hiromi ISHII 2015
Author Hiromi ISHII
Maintainer konn.jinro_at_gmail.com
Category Text
Home page https://github.com/konn/safe-printf
Source repo head: git clone git://github.com/konn/safe-printf.git
Uploaded by HiromiIshii at 2015-06-10T14:43:39Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 972 total (4 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2015-06-11 [all 1 reports]

Readme for safe-printf-0.1.0.0

[back to package description]

safe-printf -- Well-typed, variadic and flexible printf functions for Haskell

Build Status loop-effin

What is this?

Haskell's standard Text.Printf module provides variadic printf function but not type-safe. This library provides an alternative for this, more type-safe version of printf function, combinators and quasiquoters.

The current implementation is just a proof-of-concept, so it is not so efficient and provides APIs only for String value generation. In future, we will support Text types and improve the effiiciency.

Install

$ git clone https://github.com/konn/safe-printf.git
$ cd safe-printf
$ cabal install

Usage

We provide two interfaces to construct format: smart constructors and quasiquoters.

Smart constructors

You need OverloadedStrings extension.

{-# LANGUAGE OverloadedStrings #-}
module Main where
import Text.Printf.Safe             (printf, (%), (><))
import Text.Printf.Safe.Combinators (b', d, _S)
main = do
  putStrLn $ printf ("1 + 2 = " %d >< " and 0 == 1 is " %_S >< "." ) (1 + 2) (0 == 1)
  putStrLn $ printf ("42 is " % b' '0' 10 >< "in binary.") 42
  putStrLn $ printf ("48% of people answers that the negation of True is" %(show . not) >< ".") True

Quasiquote interface

Quiasiquote interface provides more readable way for generating formats.

{-# LANGUAGE QuasiQuotes #-}
module Main where
import Text.Printf.Safe    (printf, fmt)
main = do
  putStrLn $ printf [fmt|1 + 2 = %d and 0 == 1 is %S.|] (1 + 2) (0 == 1)
  putStrLn $ printf [fmt|42 is %010b in binary.|] 42
  putStrLn $ printf [fmt|48%% of people answers that the negation of True is %{show . not}.|] True

TODO

  • Support Text and perhaps Builder.
  • Improve efficiency.
  • Provide IO functions?

Licence

BSD3

(c) Hiromi ISHII 2015