 _ = require 'lodash'
-
 { clearLine, cursorTo } = require 'readline'
 { stripColor } = require 'chalk'
 

MAX_COLS_PER_LINE = stdout.columns - 1

# Private methods
getNonOcuppyingLength = (str) -> str.length - stripColor(str.replace("\n", "")).length

tag = (txt, style) -> style(_.padEnd(txt, LEFT_MARGIN)) + TAG_INDICATOR

# Assumes there is no endline within the string
fold = (str, leftPad = LEFT_PADSTRING_LONG_LINES) ->
    cols = Math.max 10, MAX_COLS_PER_LINE - leftPad.length
    nonOcuppyingLength = getNonOcuppyingLength(str)
    if str.length - nonOcuppyingLength <= cols # Base case
         str
    else # Recursive case
        cutString = str[...cols + nonOcuppyingLength]
        lastSpace = cutString.lastIndexOf(' ')

        # This if-else is in order to avoid cutting a word in half in case it's possible
        if lastSpace >= 0
            cutString[...lastSpace] + "\n" + leftPad + fold(str[lastSpace+1..])
        else # Quite strange there's no space in 70 character string...
            cutString[...-1] + "-\n" + leftPad + fold(str[cutString.length-1..])

createWriter = (txt, styler, stream = stdout) => (msg, { endline = on, margin = off } = {}) =>
        msg += "\n" if endline is on

        theresnospaceanditcannotbereadcorrectlyiftheterminalistoonarrow.")
    .w("This is a normal sentence which has a lot of columns, so it should be
        cut cause it's too long to be read correctly in the terminal.")
    .e("This sentence is also too long. But note that it has a endline,\nso
        it shouldn't be cut because it already fits in the screen.")
    .warn("This is a warning")
    .error("An error occurred", margin: on )
    .info("Be informed")
