BlackFox.MasterOfFoo 2.1.0

dotnet add package BlackFox.MasterOfFoo --version 2.1.0                
NuGet\Install-Package BlackFox.MasterOfFoo -Version 2.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BlackFox.MasterOfFoo" Version="2.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BlackFox.MasterOfFoo --version 2.1.0                
#r "nuget: BlackFox.MasterOfFoo, 2.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install BlackFox.MasterOfFoo as a Cake Addin
#addin nuget:?package=BlackFox.MasterOfFoo&version=2.1.0

// Install BlackFox.MasterOfFoo as a Cake Tool
#tool nuget:?package=BlackFox.MasterOfFoo&version=2.1.0                

MasterOfFoo

"f" Logo

Github Actions Status Nuget Package

A library to allow using printf style strings in more places.

The code is essentially an extracted version of printf.fs where the environement can not only decide what to do with the final blocks that compose the string (printf put them on the console, sprintf in a buffer, ...) but also what to do with the parameters passed for each format specifier.

Sample usage

module MyModule =
    open System.Text
    open BlackFox.MasterOfFoo
    type private MySprintfEnv() =
        inherit PrintfEnv<unit, string, string>()
        let buf = StringBuilder()
        override this.Finalize() = buf.ToString ()
        override this.Write(s : PrintableElement) = ignore(buf.Append(s.FormatAsPrintF()))
        override this.WriteT(s : string) = ignore(buf.Append(s))

    let mysprintf (format: Format<'T, unit, string, string>) =
        doPrintfFromEnv format (MySprintfEnv())

MyModule.mysprintf "Hello %s." "World"

Mini-Doc

PrintableElement

PrintableElement represent an element in a string, for example sprintf "Foo %s bar" "x" produce 3 PrintableElement, the first contains the string "Foo " the second is a format specifier 's' with an associated string value "x" and then there is the string the string " Bar".

Members :

  • ElementType: Tell your if this is a string or a format specifier.
  • Value: give the value if it was a format specifier.
  • ValueType: give the type of value expected by the format specifier.
  • StarWidth: The width if specified via another parameter as in "%*i".
  • StarPrecision: The precision if specified via another parameter as in "%.*f".
  • FormatAsPrintF(): Get the string representation that printf would have normally generated.
  • Specifier: The format specification for format specifiers.

PrintfEnv

PrintfEnv is the type to implement to create a printf variant it has 3 type parameters:

  • 'State: The state of the printer, passed as argument when using '%t'.
  • 'Residue: The type that methods passed to '%t' must return.
  • 'Result: The final result type for the printer.

Members:

  • Finalize: Create the final result for this printer
  • Write: Write an element from the format string to the printer
  • WriteT: Write the result of the method provided by %t to the printer.

Functions

  • doPrintfFromEnv: Take a format and a PrintfEnv to create a printf-like function
  • doPrintf: Same as doPrintfFromEnv but allow to know the number of elements when the PrintfEnv is created.

FAQ

What does it allow exactly that can't be done with the original set of functions ?

  • Generating complex object that aren't only a string like an SqlCommand or structured logging.
  • Escaping parts in strings, like an xmlprintf that would escape < to &lt in parameters but not in the format string.

What are the limitations ?

The main limitation is that the F# compiler allow a strict set of things an you can't go differently. The function signature that is the first argument to Format<_,_,_,_,> is generated from rules in the compiler and no library can change them.

The consequence is that we're limited to what is present in the F# compiler, can't add a %Z or allow %0s to work.

Aren't you just replicating ksprintf ?

ksprintf allow you to run code on the final generated result, essentially allowing you to run code during PrintfEnv.Finalize but you can't manipualte the format specifiers or their parameters.

What this Star syntax

When * is specified for either the width or the precision an additional parameter is taken by the format to get the value.

> sprintf "%*.*f";;
val it : (int -> int -> float -> string) = <fun:it@1>

How are interpolated strings represented ?

The details of string interpolation internals are specified in F# RFC FS-1001 - String Interpolation.

They appear as follow in this library:

  • Type-checked "printf-style" fills behave exactly as they do in sprintf and friends.
  • Unchecked ".NET-style" fills appear with a Specifier.TypeChar of 'P' and the .NET format string in Specifier.InteropHoleDotNetFormat.

Projects using it

  • ColoredPrintf: A small library that I created to add colored parts to printf strings.

If you use it somewhere, ping me on the fediverse @vbfox@hachyderm.io so I can add you.

More fun ?

module ColorPrintf =
    open System
    open System.Text
    open BlackFox.MasterOfFoo

    type private Colorize<'Result>(k) =
        inherit PrintfEnv<unit, string, 'Result>()
        override this.Finalize() : 'Result = k()
        override this.Write(s : PrintableElement) =
            match s.ElementType with
            | PrintableElementType.FromFormatSpecifier ->
                let color = Console.ForegroundColor
                Console.ForegroundColor <- ConsoleColor.Blue
                Console.Write(s.FormatAsPrintF())
                Console.ForegroundColor <- color
            | _ -> Console.Write(s.FormatAsPrintF())
        override this.WriteT(s : string) =
            let color = Console.ForegroundColor
            Console.ForegroundColor <- ConsoleColor.Red
            Console.Write(s)
            Console.ForegroundColor <- color

    let colorprintf (format: Format<'T, unit, string, unit>) =
        doPrintfFromEnv format (Colorize id)

ColorPrintf.colorprintf "%s est %t" "La vie" (fun _ -> "belle !")
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on BlackFox.MasterOfFoo:

Package Downloads
BlackFox.ColoredPrintf

Provide a printf replacement with colors. This library provide a colorprintf function where colors can be set for a range using the syntax : $foreground;background[text] For example: colorprintf "Hello $red[%s]." "world"

Interstellar.Core

Cross-platform F# browser library

FSharp.Logf

Printf-style logging for structured loggers.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.0 331 9/6/2024
2.0.0 207 9/5/2024
2.0.0-alpha.2 50 9/5/2024
2.0.0-alpha.1 56 9/5/2024
1.0.6 7,179 4/17/2021
1.0.5 3,560 12/4/2018
1.0.4 1,232 12/4/2018
1.0.3 1,362 9/9/2018
1.0.2 17,653 7/22/2018
1.0.1 3,308 2/11/2018
0.2.1 1,646 4/28/2017
0.2.0 1,794 10/23/2016
0.1.2 1,889 10/7/2016
0.1.1 1,591 10/7/2016

Synchronize with the latest FSharp.Core version and support the `%B` format specifier