Skip to content

Commit

Permalink
Collection of updates (#59)
Browse files Browse the repository at this point in the history
* Add reverse fill option for text gauges
* ldoc change
* Use better getValueAt, do not discard string.gsub return to appease linter
* Add updateHook option to SUG to allow for modifying the gauge based on values
* Fix error when wrapping strings with newlines already present. Add 'truncate' option to cut the string to length if it is longer than the width setting. Add headerTitle to display column header with same look as title. forceHeaderSeparator to show the column header separator even if normal row separators are turned off. Add missing setters for tablemaker. Make tablemaker auto update on changes when autoupdate is turned on.
* Bump version
* Try using 4.17.2 as the mudlet version for now in github actions.
  • Loading branch information
demonnic committed Apr 17, 2023
1 parent bf97877 commit 522d89f
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 69 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ jobs:
uses: demonnic/test-in-mudlet@v1.2
with:
pretestPackage: ${{ github.workspace }}/build/MDK.mpackage
mudletVersion: Mudlet-4.17.2
2 changes: 1 addition & 1 deletion mfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"package": "MDK",
"version": "2.8.3",
"version": "2.8.4",
"author": "Demonnic",
"title": "Collection of useful objects/classes",
"icon": "computer.png",
Expand Down
29 changes: 4 additions & 25 deletions src/resources/demontools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,32 +93,11 @@ local htmlHeaderPattern = [=[ <!DOCTYPE HTML PUBLIC "%-//W3C//DTD HTML 4.01 Tra
<body><span>
]=]

-- internal function, recursively digs for a value within subtables if possible
local function digForValue(dataFrom, tableTo)
if dataFrom == nil or table.size(tableTo) == 0 then
return dataFrom
else
local newData = dataFrom[tableTo[1]]
table.remove(tableTo, 1)
return digForValue(newData, tableTo)
end
end

-- Internal function, used to turn a string variable name into a value
local function getValueAt(accessString)
if accessString == "" then
return nil
end
local tempTable = accessString:split("%.")
local accessTable = {}
for i, v in ipairs(tempTable) do
if tonumber(v) then
accessTable[i] = tonumber(v)
else
accessTable[i] = v
end
end
return digForValue(_G, accessTable)
local ok, err = pcall(loadstring("return " .. tostring(accessString)))
if ok then return err end
return nil, err
end

-- internal sorting function, sorts first by hue, then luminosity, then value
Expand Down Expand Up @@ -832,7 +811,7 @@ function string.tocolor(self)
local strTable = {}
local part1 = {}
local part2 = {}
self:gsub(".", function(c)
_ = self:gsub(".", function(c)
table.insert(strTable, c)
end)
for index, value in ipairs(strTable) do
Expand Down
10 changes: 5 additions & 5 deletions src/resources/echofile.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
--- set of functions for echoing files to things. Uses a slightly hacked up version of f-strings for interpolation/templating
---@module echofile
---@author Damian Monogue <demonnic@gmail.com>
---@copyright 2021 Damian Monogue
---@copyright 2016 Hisham Muhammad (https://github.com/hishamhm/f-strings/blob/master/LICENSE)
---@license MIT, see LICENSE.lua
-- @module echofile
-- @author Damian Monogue <demonnic@gmail.com>
-- @copyright 2021 Damian Monogue
-- @copyright 2016 Hisham Muhammad (https://github.com/hishamhm/f-strings/blob/master/LICENSE)
-- @license MIT, see LICENSE.lua
local echofile = {}

-- following functions fiddled with from https://github.com/hishamhm/f-strings/blob/master/F.lua and https://hisham.hm/2016/01/04/string-interpolation-in-lua/
Expand Down
177 changes: 168 additions & 9 deletions src/resources/ftext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ function ftext.xwrap(text, limit, type)
table.insert(lines, take)
table.insert(strLines, take)
end
if leave == "\n" then
table.insert(lines, leave)
table.insert(strLines, leave)
leave = ""
end
end
end
end
Expand Down Expand Up @@ -189,12 +194,25 @@ end
-- <td class="tg-2">Should the endcap be reversed on the right? IE [[ becomes ]]</td>
-- <td class="tg-2">true</td>
-- </tr>
-- <td class="tg-1">truncate</td>
-- <td class="tg-1">Cut the string to width. Is superceded by wrap being true.</td>
-- <td class="tg-1">false</td>
-- </tr>
-- </tbody>
-- </table>
function ftext.fText(str, opts)
local options = ftext.fixFormatOptions(str, opts)
if options.wrap and (options.strLen > options.effWidth) then
local wrapped = ftext.xwrap(str, options.effWidth, options.formatType)
local wrapped = ""
if str:find("\n") then
for _,line in ipairs(str:split("\n")) do
local newline = "\n"
if _ == 1 then newline = "" end
wrapped = wrapped .. newline .. ftext.xwrap(line, options.effWidth, options.formatType)
end
else
wrapped = ftext.xwrap(str, options.effWidth, options.formatType)
end
local lines = wrapped:split("\n")
local formatted = {}
options.fixed = false
Expand Down Expand Up @@ -225,6 +243,9 @@ function ftext.fixFormatOptions(str, opts)
if options.wrap == nil then
options.wrap = true
end -- wrap by default.
if options.truncate == nil then
options.truncate = false
end -- do not truncate by default
options.formatType = options.formatType or "" -- by default, no color formatting.
options.width = options.width or 80 -- default 80 width
options.cap = options.cap or "" -- no cap by default
Expand Down Expand Up @@ -294,6 +315,12 @@ end
-- internal function, processes a single line of the wrapped string.
function ftext.fLine(str, opts)
local options = ftext.fixFormatOptions(str, opts)
local truncate, strLen, width = options.truncate, options.strLen, options.width
if truncate and strLen > width then
local wrapped = ftext.xwrap(str, options.effWidth, options.formatType)
local lines = wrapped:split("\n")
str = lines[1]
end
local leftCap = options.leftCap
local rightCap = options.rightCap
local leftPadLen = options.leftPadLen
Expand Down Expand Up @@ -638,13 +665,23 @@ function TextFormatter:setNoGap(noGap)
self.options.noGap = noGap
end

--- Enables truncation (cutting to length). You still need to ensure wrap is disabled, as it supercedes.
function TextFormatter:enableTruncate()
self.options.truncate = true
end

--- Disables truncation (cutting to length). You still need to ensure wrap is enabled if you want it to wrap.
function TextFormatter:disableTruncate()
self.options.truncate = false
end

--- Format a string based on the stored options
-- @tparam string str The string to format
function TextFormatter:format(str)
return ftext.fText(str, self.options)
end

--- Creates and returns a new TextFormatter. For valid options, please see https://github.com/demonnic/fText/wiki/fText
--- Creates and returns a new TextFormatter.
-- @tparam table options the options for the text formatter to use when running format()
-- <br><br>Table of options
-- <table class="tg">
Expand Down Expand Up @@ -716,6 +753,10 @@ end
-- <td class="tg-2">Should the endcap be reversed on the right? IE [[ becomes ]]</td>
-- <td class="tg-2">true</td>
-- </tr>
-- <td class="tg-1">truncate</td>
-- <td class="tg-1">Cut the string to width. Is superceded by wrap being true.</td>
-- <td class="tg-1">false</td>
-- </tr>
-- </tbody>
-- </table>
-- @usage
Expand Down Expand Up @@ -766,6 +807,9 @@ local TableMaker = {
autoEcho = false,
title = "",
printTitle = false,
headerTitle = false,
forceHeaderSeparator = false,
autoEchoConsole = "main",
}

function TableMaker:checkPosition(position, func)
Expand Down Expand Up @@ -1058,6 +1102,8 @@ function TableMaker:echo(message, echoType, ...)
end
if consoleType == "string" then
console = self.autoEchoConsole
elseif consoleType == "nil" then
console = "main"
else
console = self.autoEchoConsole.name
end
Expand Down Expand Up @@ -1202,8 +1248,9 @@ function TableMaker:makeHeader()
for _, v in ipairs(self.columns) do
table.insert(columnEntries, v:format(v.options.name))
end
local divWithNewlines = string.format("\n%s", self:createRowDivider())
columnHeaders = string.format("\n%s%s%s%s", ec, table.concat(columnEntries, sep), ec, self.separateRows and divWithNewlines or '')
local divWithNewlines = self.headerTitle and header or self:createRowDivider()
divWithNewlines = "\n" .. divWithNewlines
columnHeaders = string.format("\n%s%s%s%s", ec, table.concat(columnEntries, sep), ec, (self.separateRows or self.forceHeaderSeparator) and divWithNewlines or '')
end
local title = self:makeTitle(totalWidth, header)
header = string.format("%s%s%s", header, title, columnHeaders)
Expand Down Expand Up @@ -1231,39 +1278,139 @@ function TableMaker:createRowDivider()
end

--- set the title of the table
-- @tparam string title The title of the table.
function TableMaker:setTitle(title)
self.title = title
if self.autoEcho then self:assemble() end
end

--- set the rowSeparator for the table
-- @tparam string char The row separator to use
function TableMaker:setRowSeparator(char)
self.rowSeparator = char
if self.autoEcho then self:assemble() end
end

--- set the edgeCharacter for the table
-- @tparam string char The edge character to use
function TableMaker:setEdgeCharacter(char)
self.edgeCharacter = char
if self.autoEcho then self:assemble() end
end

--- set the foot character for the table
-- @tparam string char The foot character to use
function TableMaker:setFootCharacter(char)
self.footCharacter = char
if self.autoEcho then self:assemble() end
end

--- set the head character for the table
-- @tparam string char The head character to use
function TableMaker:setHeadCharacter(char)
self.headCharacter = char
if self.autoEcho then self:assemble() end
end

--- set the column separator character for the table
-- @tparam string char The separator character to use
function TableMaker:setSeparator(char)
self.separator = char
if self.autoEcho then self:assemble() end
end

--- set the title color for the table
-- @tparam string color The title color to use. Should match the color type of the tablemaker (cecho by default)
function TableMaker:setTitleColor(color)
self.titleColor = color
if self.autoEcho then self:assemble() end
end

--- set the title color for the table
-- @tparam string color The separator color to use. Should match the color type of the tablemaker (cecho by default)
function TableMaker:setSeparatorColor(color)
self.separatorColor = color
if self.autoEcho then self:assemble() end
end

--- set the title color for the table
-- @tparam string color The frame color to use. Should match the color type of the tablemaker (cecho by default)
function TableMaker:setFrameColor(color)
self.frameColor = color
if self.autoEcho then self:assemble() end
end

--- Force a separator between the header and first row, even if the row separator is disabled for the overall table
function TableMaker:enableForceHeaderSeparator()
self.forceHeaderSeparator = true
if self.autoEcho then self:assemble() end
end

--- Do not force a separator between the header and first row, even if the row separator is disabled for the overall table
function TableMaker:disableForceHeaderSeparator()
self.forceHeaderSeparator = false
if self.autoEcho then self:assemble() end
end

--- Enable using the title separator for the column headers as well
function TableMaker:enableHeaderTitle()
self.headerTitle = true
if self.autoEcho then self:assemble() end
end

--- Disable using the title separator for the column headers as well
function TableMaker:disableHeaderTitle()
self.headerTitle = false
if self.autoEcho then self:assemble() end
end

--- enable printing the title of the table
function TableMaker:enablePrintTitle()
self.printTitle = true
if self.autoEcho then self:assemble() end
end

--- enable printing the title of the table
--- disable printing the title of the table
function TableMaker:disablePrintTitle()
self.printTitle = false
if self.autoEcho then self:assemble() end
end

--- enable printing of the column headers
function TableMaker:enablePrintHeaders()
self.printHeaders = true
if self.autoEcho then self:assemble() end
end

--- disable printing of the column headers
function TableMaker:disablePrintHeaders()
self.printHeaders = false
if self.autoEcho then self:assemble() end
end

--- enable printing the separator line between rows
function TableMaker:enableRowSeparator()
self.separateRows = true
if self.autoEcho then self:assemble() end
end

--- enable printing the separator line between rows
function TableMaker:disableRowSeparator()
self.separateRows = false
if self.autoEcho then self:assemble() end
end

--- enables making cells which incorporate insertLink/insertPopup
function TableMaker:enablePopups()
self.autoEcho = true
self.allowPopups = true
if self.autoEcho then self:assemble() end
end

--- enables autoEcho so that when assemble is called it echos automatically
function TableMaker:enableAutoEcho()
self.autoEcho = true
self:assemble()
end

--- disables autoecho. Cannot be used if allowPopups is set
Expand All @@ -1278,6 +1425,7 @@ end
--- Enables automatically clearing the miniconsole we echo to
function TableMaker:enableAutoClear()
self.autoClear = true
if self.autoEcho then self:assemble() end
end

--- Disables automatically clearing the miniconsole we echo to
Expand All @@ -1294,12 +1442,13 @@ function TableMaker:setAutoEchoConsole(console)
end
local consoleType = type(console)
if consoleType ~= "string" and consoleType ~= "table" then
error(funcName .. " ArgumentError: console as string or Geyser.MiniConsole expected, got " .. consoleType)
elseif consoleType == "table" and console.type ~= "miniConsole" then
error(funcName .. " ArgumentError: console as string or a Geyser MiniConsole or UserWindow expected, got " .. consoleType)
elseif consoleType == "table" and not (console.type == "miniConsole" or console.type == "userwindow") then
error(funcName .. " ArgumentError: console received was a table and may be a Geyser object, but console.type is not miniConsole, it is " ..
console.type)
end
self.autoEchoConsole = console
if self.autoEcho then self:assemble() end
end

--- Assemble the table. If autoEcho is enabled/set to true, will automatically echo. Otherwise, returns the formatted string to echo the table
Expand Down Expand Up @@ -1356,8 +1505,8 @@ function TableMaker:textAssemble()
return sheet
end

--- Creates and returns a new TableMaker. See https://github.com/demonnic/fText/wiki/TableMaker for valid entries to the options table.
-- see https://github.com/demonnic/tempwiki/wiki/fText%3A-TableMaker%3A-Examples for usage
--- Creates and returns a new TableMaker.
-- see https://github.com/demonnic/MDK/wiki/fText%3A-TableMaker%3A-Examples for usage
-- @tparam table options table of options for the TableMaker object
-- <br><br>Table of new options
-- <table class="tg">
Expand Down Expand Up @@ -1455,6 +1604,16 @@ end
-- <td class="tg-1">Should we print the title of the table at the very tip-top?</td>
-- <td class="tg-1">false</td>
-- </tr>
-- <tr>
-- <td class="tg-2">headerTitle</td>
-- <td class="tg-2">Use the same separator for the column headers as the title/top, rather than the row separator</td>
-- <td class="tg-2">formatColor</td>
-- </tr>
-- <tr>
-- <td class="tg-1">forceHeaderSeparator</td>
-- <td class="tg-1">Force a separator between the column headers and the first row, even if rowSeparator is false.</td>
-- <td class="tg-1">false</td>
-- </tr>
-- </tbody>
-- </table>
function TableMaker:new(options)
Expand Down
2 changes: 1 addition & 1 deletion src/resources/mdkversion.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.8.3
2.8.4
Loading

0 comments on commit 522d89f

Please sign in to comment.