unbeliever-0.8.0.0: Opinionated Haskell Interoperability

Safe HaskellNone
LanguageHaskell2010

Core.Text.Utilities

Contents

Description

Useful tools for working with Ropes. Support for pretty printing, multi-line strings, and...

Synopsis

Pretty printing

class Render α where Source #

Types which can be rendered "prettily", that is, formatted by a pretty printer and embossed with beautiful ANSI colours when printed to the terminal.

Use render to build text object for later use or Core.Program.Execute's writeR if you're writing directly to console now.

Associated Types

type Token α :: * Source #

Which type are the annotations of your Doc going to be expressed in?

Methods

colourize :: Token α -> AnsiStyle Source #

Convert semantic tokens to specific ANSI escape tokens

intoDocA :: α -> Doc (Token α) Source #

Arrange your type as a Doc ann, annotated with your semantic tokens.

Instances
Render Text Source # 
Instance details

Defined in Core.Text.Utilities

Associated Types

type Token Text :: Type Source #

Render Value Source # 
Instance details

Defined in Core.Encoding.Json

Associated Types

type Token Value :: Type Source #

Render Rope Source # 
Instance details

Defined in Core.Text.Utilities

Associated Types

type Token Rope :: Type Source #

Render Bytes Source # 
Instance details

Defined in Core.Text.Bytes

Associated Types

type Token Bytes :: Type Source #

Render JsonKey Source # 
Instance details

Defined in Core.Encoding.Json

Associated Types

type Token JsonKey :: Type Source #

Render JsonValue Source # 
Instance details

Defined in Core.Encoding.Json

Associated Types

type Token JsonValue :: Type Source #

Render [Char] Source # 
Instance details

Defined in Core.Text.Utilities

Associated Types

type Token [Char] :: Type Source #

render :: Render α => Int -> α -> Rope Source #

Given an object of a type with a Render instance, transform it into a Rope saturated with ANSI escape codes representing syntax highlighting or similar colouring, wrapping at the specified width.

The obvious expectation is that the next thing you're going to do is send the Rope to console with:

    write (render 80 thing)

However, the better thing to do is to instead use:

    writeR thing

which is able to pretty print the document text respecting the available width of the terminal.

Helpers

indefinite :: Rope -> Rope Source #

Render "a" or "an" in front of a word depending on English's idea of whether it's a vowel or not.

wrap :: Int -> Rope -> Rope Source #

Often the input text represents a paragraph, but does not have any internal newlines (representing word wrapping). This function takes a line of text and inserts newlines to simulate such folding. It also appends a trailing newline to finish the paragraph.

Multi-line strings

quote :: QuasiQuoter Source #

Multi-line string literals.

To use these you need to enable the QuasiQuotes language extension in your source file:

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}

you are then able to easily write a string stretching over several lines.

How best to formatting multi-line string literal within your source code is an aesthetic judgement. Sometimes you don't care about the whitespace leading a passage (8 spaces in this example):

    let message = [quote|
        This is a test of the Emergency Broadcast System. Do not be
        alarmed. If this were a real emergency, someone would have tweeted
        about it by now.
    |]

because you are feeding it into a Doc for pretty printing and know the renderer will convert the whole text into a single line and then re-flow it. Other times you will want to have the string as is, literally:

    let poem = [quote|
If the sun
    rises
        in the
    west
you     drank
    too much
                last week.
    |]

Leading whitespace from the first line and trailing whitespace from the last line will be trimmed, so this:

    let value = [quote|
Hello
    |]

is translated to:

    let value = fromString "Hello\n"

without the leading newline or trailing four spaces. Note that as string literals they are presented to your code with fromString :: String -> α so any type with an IsString instance (as Rope has) can be constructed from a multi-line [quote| ... |] literal.