Module demontools

Collection of miscellaneous functions and tools which don't necessarily warrant their own module/class

Info:

  • Copyright: 2020 Damian Monogue
  • License: MIT, see LICENSE.lua
  • Author: Damian Monogue

Functions

DemonTools.chunkify(tbl, num_chunks) Takes a list table and returns it as a table of 'chunks'.
DemonTools.ansi2cecho(text) Takes an ansi colored text string and returns a cecho colored one
DemonTools.ansi2decho(text) Takes an ansi colored text string and returns a decho colored one.
DemonTools.ansi2hecho(text) Takes an ansi colored text string and returns a hecho colored one
DemonTools.cecho2decho(text) Takes an cecho colored text string and returns a decho colored one
DemonTools.cecho2ansi(text) Takes an cecho colored text string and returns an ansi colored one
DemonTools.cecho2hecho(text) Takes an cecho colored text string and returns a hecho colored one
DemonTools.decho2cecho(text) Takes an decho colored text string and returns a cecho colored one
DemonTools.decho2ansi(text) Takes an decho colored text string and returns an ansi colored one
DemonTools.decho2hecho(text) Takes an decho colored text string and returns an hecho colored one
DemonTools.decho2html(text) Takes a decho colored text string and returns html.
DemonTools.cecho2html(text) Takes a cecho colored text string and returns html.
DemonTools.hecho2html(text) Takes a hecho colored text string and returns html.
DemonTools.ansi2html(text) Takes an ansi colored text string and returns html.
DemonTools.html2cecho(text) Takes an html colored string of the sort turned out by the DemonTools *2html functions and returns a cecho string
DemonTools.html2decho(text) Takes an html colored string of the sort turned out by the DemonTools *2html functions and returns a decho string
DemonTools.html2ansi(text) Takes an html colored string of the sort turned out by the DemonTools *2html functions and returns an ansi string
DemonTools.html2hecho(text) Takes an html colored string of the sort turned out by the DemonTools *2html functions and returns an hecho string
DemonTools.cecho2string(text) Takes a cecho string and returns it without the formatting
DemonTools.decho2string(text) Takes a decho string and returns it without the formatting
DemonTools.hecho2string(text) Takes a hecho string and returns it without the formatting
DemonTools.html2string(text) Takes an html colored string of the sort turned out by the DemonTools *2html functions and returns a clean string
DemonTools.hecho2ansi(text) Takes an hecho colored text string and returns a ansi colored one
DemonTools.hecho2cecho(text) Takes an hecho colored text string and returns a cecho colored one
DemonTools.hecho2decho(text) Takes an hecho colored text string and returns a decho colored one
DemonTools.append2decho() Takes the currently copy()ed item and returns it as a decho string
DemonTools.consoleToString(options) Dump the contents of a miniconsole, user window, or the main window in one of several formats, as determined by a table of options
DemonTools.displayColors(options) Alternative to Mudlet's showColors(), this one has additional options.
DemonTools.roundInt(number) Rounds a number to the nearest whole integer
DemonTools.scientific_round(number, sig_digits) Rounds a number to a specified number of significant digits
DemonTools.string2color(str) Returns a color table {r,g,b} derived from str.
DemonTools.colorMunge(strForColor, strToColor, format) Returns a colored string where strForColor is run through DemonTools.string2color and applied to strToColor based on format.
DemonTools.colorMungeEcho(strForColor, strToEcho, format, win) Like colorMunge but also echos the result to win.
DemonTools.milliToHuman(milliseconds, tbl) Converts milliseconds to hours:minutes:seconds:milliseconds
DemonTools.getValueAt(variableString) Takes the name of a variable as a string and returns the value.
DemonTools.exists(path) Returns if a file or directory exists on the filesystem
DemonTools.isDir(path) Returns if a path is a directory or not
DemonTools.isWindows() Returns true if running on windows, false otherwise
DemonTools.mkdir_p(path) Creates a directory, creating each directory as necessary along the way.


Functions

DemonTools.chunkify(tbl, num_chunks)
Takes a list table and returns it as a table of 'chunks'. If the table has 12 items and you ask for 3 chunks, each chunk will have 4 items in it

Parameters:

  • tbl table The table you want to turn into chunks. Must be traversable using ipairs()
  • num_chunks number The number of chunks to turn the table into

Usage:

    local dt = require("MDK.demontools")
    testTable = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" }
    display(dt.chunkify(testTable, 3))
    --displays the following
    {
      {
        "one",
        "two",
        "three",
        "four"
      },
      {
        "five",
        "six",
        "seven"
      },
      {
        "eight",
        "nine",
        "ten"
      }
    }
DemonTools.ansi2cecho(text)
Takes an ansi colored text string and returns a cecho colored one

Parameters:

  • text string the text to convert

Usage:

    dt.ansi2cecho("Test")
     --returns "<ansiRed>Test"
DemonTools.ansi2decho(text)
Takes an ansi colored text string and returns a decho colored one. Handles 256 color SGR codes better than Mudlet's ansi2decho

Parameters:

  • text string the text to convert

Usage:

  • dt.ansi2decho("Test") --returns "<128,0,0>Test"
  • dt.ansi2decho("[38:2::127:0:0mTest") --returns "<127,0,0>Test"
  • ansi2decho("[38:2::127:0:0mTest") -- doesn't parse this format of colors and so returns "[38:2::127:0:0mTest"
DemonTools.ansi2hecho(text)
Takes an ansi colored text string and returns a hecho colored one

Parameters:

  • text string the text to convert

Usage:

    dt.ansi2hecho("Test")
     --returns "#800000Test"
DemonTools.cecho2decho(text)
Takes an cecho colored text string and returns a decho colored one

Parameters:

  • text string the text to convert

Usage:

    dt.cecho2decho("<green>Test") --returns "<0,255,0>Test"
DemonTools.cecho2ansi(text)
Takes an cecho colored text string and returns an ansi colored one

Parameters:

  • text string the text to convert

Usage:

    dt.cecho2ansi("<green>Test") --returns "[38:2::0:255:0mTest"
DemonTools.cecho2hecho(text)
Takes an cecho colored text string and returns a hecho colored one

Parameters:

  • text string the text to convert

Usage:

    dt.cecho2hecho("<green>Test") --returns "#00ff00Test"
DemonTools.decho2cecho(text)
Takes an decho colored text string and returns a cecho colored one

Parameters:

  • text string the text to convert

Usage:

    dt.decho2cecho("<127,0,0:0,0,127>Test") --returns "<ansiRed:ansi_blue>Test"
DemonTools.decho2ansi(text)
Takes an decho colored text string and returns an ansi colored one

Parameters:

  • text string the text to convert

Usage:

    dt.decho2ansi("<127,0,0:0,0,127>Test") --returns "[38:2::127:0:0m[48:2::0:0:127mTest"
DemonTools.decho2hecho(text)
Takes an decho colored text string and returns an hecho colored one

Parameters:

  • text string the text to convert

Usage:

    dt.decho2hecho("<127,0,0:0,0,127>Test") --returns "#7f0000,00007fTest"
DemonTools.decho2html(text)
Takes a decho colored text string and returns html.

Parameters:

  • text string the text to convert
DemonTools.cecho2html(text)
Takes a cecho colored text string and returns html.

Parameters:

  • text string the text to convert
DemonTools.hecho2html(text)
Takes a hecho colored text string and returns html.

Parameters:

  • text string the text to convert
DemonTools.ansi2html(text)
Takes an ansi colored text string and returns html.

Parameters:

  • text string the text to convert
DemonTools.html2cecho(text)
Takes an html colored string of the sort turned out by the DemonTools *2html functions and returns a cecho string

Parameters:

  • text string the text to convert
DemonTools.html2decho(text)
Takes an html colored string of the sort turned out by the DemonTools *2html functions and returns a decho string

Parameters:

  • text string the text to convert
DemonTools.html2ansi(text)
Takes an html colored string of the sort turned out by the DemonTools *2html functions and returns an ansi string

Parameters:

  • text string the text to convert
DemonTools.html2hecho(text)
Takes an html colored string of the sort turned out by the DemonTools *2html functions and returns an hecho string

Parameters:

  • text string the text to convert
DemonTools.cecho2string(text)
Takes a cecho string and returns it without the formatting

Parameters:

  • text the text to transform
DemonTools.decho2string(text)
Takes a decho string and returns it without the formatting

Parameters:

  • text the text to transform
DemonTools.hecho2string(text)
Takes a hecho string and returns it without the formatting

Parameters:

  • text the text to transform
DemonTools.html2string(text)
Takes an html colored string of the sort turned out by the DemonTools *2html functions and returns a clean string

Parameters:

  • text
DemonTools.hecho2ansi(text)
Takes an hecho colored text string and returns a ansi colored one

Parameters:

  • text string the text to convert

Usage:

    dt.hecho2ansi("#7f0000,00007fTest") --returns "[38:2::127:0:0m[48:2::0:0:127mTest"
DemonTools.hecho2cecho(text)
Takes an hecho colored text string and returns a cecho colored one

Parameters:

  • text string the text to convert

Usage:

    dt.hecho2cecho("#7f0000,00007fTest") --returns "<ansiRed:ansi_blue>Test"
DemonTools.hecho2decho(text)
Takes an hecho colored text string and returns a decho colored one

Parameters:

  • text string the text to convert

Usage:

    dt.hecho2decho("#7f0000,00007fTest") --returns "<127,0,0:0,0,127>Test"
DemonTools.append2decho()
Takes the currently copy()ed item and returns it as a decho string
DemonTools.consoleToString(options)
Dump the contents of a miniconsole, user window, or the main window in one of several formats, as determined by a table of options

Parameters:

  • options table Table of options which controls which console and how it returns the data.
    option name description default
    format What format to return the text as? 'h' for html, 'c' for cecho, 'a' for ansi, 'd' for decho, and 'x' for hecho "d"
    win what console/window to dump the buffer of? "main"
    start_line What line to start dumping the buffer from? 0
    end_line What line to stop dumping the buffer on? Last line of the console
    includeHtmlWrapper If the format is html, should it include the front and back portions required to make it a functioning html page? true
DemonTools.displayColors(options)
Alternative to Mudlet's showColors(), this one has additional options.

Parameters:

  • options table table of options which control the output of displayColors
    option name description default
    cols Number of columsn wide to display the colors in 4
    search If not the empty string, will check colors against string.find using this property.
    IE if set to "blue" only colors which include the word 'blue' would be listed
    ""
    sort If true, sorts alphabetically. Otherwise sorts based on the color value false
    echoOnly If true, colors will not be clickable links false
    window What window/console to echo the colors out to. "main"
    removeDupes If true, will remove snake_case entries and 'gray' in favor of 'grey' true
    columnSort If true, will print top-to-bottom, then left-to-right. false is like showColors true
    justText If true, will echo the text in the color and leave the background black.
    If false, the background will be the colour(like showColors).
    false
    color_table Table of colors to display. If you provide your own table, it must be in the same format as Mudlet's own color_table color_table
DemonTools.roundInt(number)
Rounds a number to the nearest whole integer

Parameters:

  • number the number to round off

Usage:

  • dt.roundInt(8.3) -- returns 8
  • dt.roundInt(10.7) -- returns 11
DemonTools.scientific_round(number, sig_digits)
Rounds a number to a specified number of significant digits

Parameters:

  • number number the number to round
  • sig_digits number the number of significant digits to keep

Usage:

  • dt.scientific_round(1348290, 3) -- will return 1350000
  • dt.scientific_found(123.3452, 5) -- will return 123.34
DemonTools.string2color(str)
Returns a color table {r,g,b} derived from str. Will return the same color every time for the same string.

Parameters:

  • str string the string to turn into a color.

Usage:

    dt.string2color("Demonnic") --returns { 131, 122, 209 }
DemonTools.colorMunge(strForColor, strToColor, format)
Returns a colored string where strForColor is run through DemonTools.string2color and applied to strToColor based on format.

Parameters:

  • strForColor string the string to turn into a color using DemonTools.string2color
  • strToColor string the string you want to color based on strForColor
  • format What format to use for the color portion. "d" for decho, "c" for cecho, or "h" for hecho. Defaults to "d"

Usage:

    dt.colorMunge("Demonnic", "Test") --returns "<131,122,209>Test"
DemonTools.colorMungeEcho(strForColor, strToEcho, format, win)
Like colorMunge but also echos the result to win.

Parameters:

  • strForColor string the string to turn into a color using DemonTools.string2color
  • strToEcho string the string you want to color and echo based on strForColor
  • format What format to use for the color portion. "d" for decho, "c" for cecho, or "h" for hecho. Defaults to "d"
  • win the window to echo to. You must provide the format if you want to change the window. Defaults to "main"
DemonTools.milliToHuman(milliseconds, tbl)
Converts milliseconds to hours:minutes:seconds:milliseconds

Parameters:

  • milliseconds number the number of milliseconds to convert
  • tbl boolean if true, returns the time as a key/value table instead

Usage:

  • dt.milliToHuman(37194572) --returns "10:19:54:572"
  • display(dt.milliToHuman(37194572, true))
    {
      minutes = 19,
      original = 37194572,
      hours = 10,
      milliseconds = 572,
      seconds = 54
    }
DemonTools.getValueAt(variableString)
Takes the name of a variable as a string and returns the value. "health" will return the value in varable health, "gmcp.Char.Vitals" will return the table at gmcp.Char.Vitals, etc

Parameters:

  • variableString string the string name of the variable you want the value of

Usage:

    currentHP = 50
     dt.getValueAt("currentHP") -- returns 50
DemonTools.exists(path)
Returns if a file or directory exists on the filesystem

Parameters:

  • path string the path to the file or directory to check
DemonTools.isDir(path)
Returns if a path is a directory or not

Parameters:

  • path string the path to check
DemonTools.isWindows()
Returns true if running on windows, false otherwise
DemonTools.mkdir_p(path)
Creates a directory, creating each directory as necessary along the way.

Parameters:

  • path string the path to create
generated by LDoc 1.4.6 Last updated 2021-08-31 18:42:52