v0.0.1
Finds and replaces common indent, hard-wraps text, generates text tables and table-based complex text layouts. Can work on text that contains terminal escape sequences.
Repository
Current version released
2 years ago
indent_and_wrap
Finds and replaces common indent in text, and hard-wraps text.
Example
import {indentAndWrap} from 'https://deno.land/x/indent_and_wrap@v0.0.1/mod.ts';
Exported symbols
- calcLines() - Count number of lines in text string, and determine column number of the last character.
- findCommonIndent() - Scan text string, and find leading space characters, that are common across all lines.
- indentAndWrap() - Indent or unindent and wrap text.
calcLines
function calcLines(text: string, from=0, to=Number.MAX_SAFE_INTEGER, tabWidth=4)
Count number of lines in text string, and determine column number of the last character.
This function only considers text substring from from
to to
.
findCommonIndent
function findCommonIndent(text: string, ignoreFirstIndent=false)
Scan text string, and find leading space characters, that are common across all lines.
If ignoreFirstIndent
is set, then the leading space on the first line is not counted, so the provided text string can be trimmed.
This function uses fast algorithm that avoids splitting text to lines array.
indentAndWrap
function indentAndWrap(text: string, options?: IndentAndWrapOptions, knownCommonIndent?: string)
type IndentAndWrapOptions =
{ indent?: string;
ignoreFirstIndent?: boolean;
wrapWidth?: number;
tabWidth?: number;
endl?: string;
};
This function does:
- Replaces new line characters (
\n
,\r\n
or\r
) tooptions.endl
, or if itās not set to\n
. - If
options.indent
is set, it determines common indent characters across all lines, and replaces them withoptions.indent
string. This can lead to indent increase or decrease. Ifoptions.ignoreFirstIndent
is set, will look for common indent starting at second line, so the text can be trimmed. If you already know the common indent (e.g. you calledfindCommonIndent()
), you can provide it asknownCommonIndent
to save some calculation time. IfknownCommonIndent
doesnāt match the result offindCommonIndent()
, the behavior is undefined. - If
options.wrapWidth
is set, it insertsoptions.endl
, so thereāre no lines longer thanoptions.wrapWidth
columns. Columns are calculated with respect tooptions.tabWidth
(default 4).