@@ -1284,4 +1284,203 @@ end
12841284DemonTools .htmlHeader = htmlHeader
12851285DemonTools .htmlHeaderPattern = htmlHeaderPattern
12861286
1287+ local echoOutputs = {
1288+ Color = {
1289+ [" \27 reset" ] = " <reset>" ,
1290+ [" \27 bold" ] = " <b>" ,
1291+ [" \27 boldoff" ] = " </b>" ,
1292+ [" \27 italics" ] = " <i>" ,
1293+ [" \27 italicsoff" ] = " </i>" ,
1294+ [" \27 underline" ] = " <u>" ,
1295+ [" \27 underlineoff" ] = " </u>" ,
1296+ [" \27 strikethrough" ] = " <s>" ,
1297+ [" \27 strikethroughoff" ] = " </s>" ,
1298+ [" \27 overline" ] = " <o>" ,
1299+ [" \27 overlineoff" ] = " </o>" ,
1300+ },
1301+ Decimal = {
1302+ [" \27 reset" ] = " <r>" ,
1303+ [" \27 bold" ] = " <b>" ,
1304+ [" \27 boldoff" ] = " </b>" ,
1305+ [" \27 italics" ] = " <i>" ,
1306+ [" \27 italicsoff" ] = " </i>" ,
1307+ [" \27 underline" ] = " <u>" ,
1308+ [" \27 underlineoff" ] = " </u>" ,
1309+ [" \27 strikethrough" ] = " <s>" ,
1310+ [" \27 strikethroughoff" ] = " </s>" ,
1311+ [" \27 overline" ] = " <o>" ,
1312+ [" \27 overlineoff" ] = " </o>" ,
1313+ },
1314+ Hex = {
1315+ [" \27 reset" ] = " #r" ,
1316+ [" \27 bold" ] = " #b" ,
1317+ [" \27 boldoff" ] = " #/b" ,
1318+ [" \27 italics" ] = " #i" ,
1319+ [" \27 italicsoff" ] = " #/i" ,
1320+ [" \27 underline" ] = " #u" ,
1321+ [" \27 underlineoff" ] = " #/u" ,
1322+ [" \27 strikethrough" ] = " #s" ,
1323+ [" \27 strikethroughoff" ] = " #/s" ,
1324+ [" \27 overline" ] = " #o" ,
1325+ [" \27 overlineoff" ] = " #/o" ,
1326+ }
1327+ }
1328+
1329+ local echoPatterns = _Echos .Patterns
1330+ local echoProcess = _Echos .Process
1331+
1332+ function DemonTools .toHTML (t , reset )
1333+ reset = reset or {
1334+ background = { 0 , 0 , 0 },
1335+ bold = false ,
1336+ foreground = { 255 , 255 , 255 },
1337+ italic = false ,
1338+ overline = false ,
1339+ reverse = false ,
1340+ strikeout = false ,
1341+ underline = false
1342+ }
1343+ local format = table .deepcopy (reset )
1344+ local result = getHTMLformat (format )
1345+ for _ ,v in ipairs (t ) do
1346+ local formatChanged = false
1347+ if type (v ) == " table" then
1348+ if v .fg then
1349+ format .foreground = {v .fg [1 ], v .fg [2 ], v .fg [3 ]}
1350+ formatChanged = true
1351+ end
1352+ if v .bg then
1353+ format .background = {v .bg [1 ], v .bg [2 ], v .bg [3 ]}
1354+ formatChanged = true
1355+ end
1356+ elseif v == " \27 bold" then
1357+ format .bold = true
1358+ formatChanged = true
1359+ elseif v == " \27 boldoff" then
1360+ format .bold = false
1361+ formatChanged = true
1362+ elseif v == " \27 italics" then
1363+ format .italic = true
1364+ formatChanged = true
1365+ elseif v == " \27 italicsoff" then
1366+ format .italic = false
1367+ formatChanged = true
1368+ elseif v == " \27 underline" then
1369+ format .underline = true
1370+ formatChanged = true
1371+ elseif v == " \27 underlineoff" then
1372+ format .underline = false
1373+ formatChanged = true
1374+ elseif v == " \27 strikethrough" then
1375+ format .strikeout = true
1376+ formatChanged = true
1377+ elseif v == " \27 strikethroughoff" then
1378+ format .strikeout = false
1379+ formatChanged = true
1380+ elseif v == " \27 overline" then
1381+ format .overline = true
1382+ formatChanged = true
1383+ elseif v == " \27 overlineoff" then
1384+ format .overline = false
1385+ formatChanged = true
1386+ elseif v == " \27 reset" then
1387+ format = table .deepcopy (reset )
1388+ formatChanged = true
1389+ end
1390+ v = formatChanged and getHTMLformat (format ) or v
1391+ result = result .. v
1392+ end
1393+ return result
1394+ end
1395+
1396+ local function toEcho (colorType , colors )
1397+ colorType = colorType :lower ()
1398+ local result
1399+ if colorType == " hex" then
1400+ local fg ,bg = " " , " "
1401+ if colors .fg then
1402+ fg = string.format (" %02x%02x%02x" , unpack (colors .fg ))
1403+ end
1404+ if colors .bg then
1405+ bg = string.format (" ,%02x%02x%02x" , unpack (colors .bg ))
1406+ end
1407+ result = string.format (" #%s%s" , fg , bg )
1408+ elseif colorType == " color" then
1409+ local fg ,bg = " " ," "
1410+ if colors .fg then
1411+ fg = closestColor (colors .fg )
1412+ end
1413+ if colors .bg then
1414+ bg = " :" .. closestColor (colors .bg [1 ], colors .bg [2 ], colors .bg [3 ])
1415+ end
1416+ result = string.format (" <%s%s>" , fg , bg )
1417+ elseif colorType == " decimal" then
1418+ local fg ,bg = " " , " "
1419+ if colors .fg then
1420+ fg = string.format (" %d,%d,%d" , unpack (colors .fg ))
1421+ end
1422+ if colors .bg then
1423+ bg = string.format (" :%d,%d,%d" , unpack (colors .bg ))
1424+ end
1425+ result = string.format (" <%s%s>" , fg , bg )
1426+ end
1427+ return result
1428+ end
1429+
1430+ function DemonTools .echoConverter (str , from , to , resetFormat )
1431+ local strType , fromType , toType , resetType = type (str ), type (from ), type (to ), type (resetFormat )
1432+ local errTemplate = " bad argument #{argNum} type ({argName} as string expected, got {argType})"
1433+ local argNum , argName , argType
1434+ local err = false
1435+ if strType ~= " string" then
1436+ argNum = 1
1437+ argName = " str"
1438+ argType = strType
1439+ err = true
1440+ elseif fromType ~= " string" then
1441+ argNum = 2
1442+ argName = " from"
1443+ argType = fromType
1444+ err = true
1445+ elseif toType ~= " string" then
1446+ argNum = 3
1447+ argName = " to"
1448+ argType = toType
1449+ err = true
1450+ elseif resetFormat and resetType ~= " table" then
1451+ argType = resetType
1452+ errTemplate = " bad argument #4 type (optional resetFormat as table of formatting options expected, got {argType})"
1453+ err = true
1454+ end
1455+ if err then
1456+ printError (f (errTemplate ), true , true )
1457+ end
1458+ from = from :title ()
1459+ local t = echoProcess (str , from )
1460+ if not echoPatterns [from ] then
1461+ local msg = " argument #4 (from) must be a valid echo type. Valid types are: " .. table.concat (table .keys (echoPatterns ), " ," )
1462+ end
1463+ local processed = echoProcess (str , from )
1464+ if to :lower () == " html" then
1465+ return DemonTools .toHTML (processed , resetFormat )
1466+ end
1467+ local outputs = echoOutputs [to ]
1468+ if not outputs then
1469+ local msg = " argument #3 (to) must be a valid echo type. Valid types are: " .. table.concat (table .keys (echoOutputs ), " ," )
1470+ printError (msg , true , true )
1471+ end
1472+ local result = " "
1473+ for _ , token in ipairs (processed ) do
1474+ local formatter = outputs [token ]
1475+ if formatter and token :find (" \27 " ) then
1476+ result = result .. formatter
1477+ elseif type (token ) == " table" then
1478+ result = result .. toEcho (to , token )
1479+ else
1480+ result = result .. token
1481+ end
1482+ end
1483+ return result
1484+ end
1485+
12871486return DemonTools
0 commit comments