Quick and Powerful Highlighting Script

Share your scripts and packages with other Mudlet users.
Post Reply
rahsael
Posts: 3
Joined: Tue Mar 03, 2020 11:10 pm

Quick and Powerful Highlighting Script

Post by rahsael »

This is useful for users to add highlights on the fly. It's been added to our increasingly feature-rich Carrion Fields client by popular request. Special thanks to demonnic for making the colorCheck() function much more elegant. It's entirely possible this script could be streamlined further, but it works!

Syntax:
highlight <color> <text> (uses any color in Mudlet color tables, correcting for most typos)
highlight remove <highlight number> (removes a single highlight)
highlight clear (wipes all highlights)
highlight (lists all highlights)

The table "highlights" can be saved to an external file to keep highlights between sessions.

Code: Select all

--Pattern: ^(?:highl?i?g?h?t?)(?: (\w+)\s?(.*))?$
local highlight_color = matches[2]
local highlight_text = matches[3]
local highlight_replace_bool = false

if highlight_color == nil or highlight_color == "" then return listHighlights() end

if highlight_color == "clear" then
  echo("Clearing all highlights.\r\n\r\n")
  for _, highlight in ipairs(highlights) do
    killTrigger(highlight["trigger"])
  end
  highlights = {}
  listHighlights()
  return
  end

if highlight_color == "remove" and tonumber(highlight_text) then
  local highlight_number = tonumber(highlight_text)

  if highlight_number > #highlights then 
  echo("Invalid highlight number.\r\n")
  return listHighlights()
  else
    local success = true
  end

  cecho("<gray>Removing highlight: <"..highlights[highlight_number]["color"]..">"..highlights[highlight_number]["text"].."\r\n")

  for highlight,_ in ipairs(highlights) do  
    if highlight == highlight_number then
      killTrigger(highlights[highlight]["trigger"])
    end
    
    if highlight >= highlight_number and highlight < #highlights then
      highlights[highlight] = highlights[highlight + 1]
    end
    
    if highlight == #highlights then highlights[highlight] = nil end
  end
return listHighlights()
  elseif highlight_color == "remove" and not tonumber(highlight_text) then
    return cecho("\r\n<gray>Syntax: <white>highlight remove <highlight number>\r\n\r\n")
end

if not colorCheck(highlight_color) then return cecho(highlight_color.."<gray>You have chosen an invalid color.\r\nTry <red>Red<white>, <cyan>Cyan<gray>, <green>Green<gray>, <ForestGreen>ForestGreen<gray>, <blue>Blue<gray>, <sky_blue>SkyBlue<gray>, <violet>Violet<gray>, <maroon>Maroon<gray>, <gold>Gold<gray>, <yellow>Yellow<gray>, <dim_gray>DimGray<gray> or <white>White<gray>.\r\n<steel_blue>See all available colors by typing <white>showcolors<steel_blue> or <white>showcolors <color family name><steel_blue>.\r\n\r\n") end

if highlight_text == nil or highlight_text == "" then return cecho("<gray>No highlight text provided.\r\nSyntax: <white>highlight <color> <text>\r\n\r\n") end

-- Setting up highlight_data to insert into the table "highlights"
highlight_color = colorCheck(highlight_color)
highlight_trigger = tempTrigger(highlight_text, function() selectString(highlight_text, 1) fg(highlight_color) resetFormat() end)
local highlight_data = {number = #highlights+1, text = highlight_text, color = highlight_color, trigger = highlight_trigger}  

for _, highlight in ipairs(highlights) do
  if highlight.text == highlight_text then
    cecho("<gray>Replacing highlight: ")
    highlight_data.number = highlight.number
    highlight.color = highlight_color
    highlight_replace_bool = true
  end
end

if highlight_replace_bool == false then
  cecho("<gray>New highlight: ")
  highlights[highlight_data.number] = highlight_data
end
cecho('<white>"'..highlight_text..'"<gray> in <'..highlight_color..'>'..highlight_color..'<white>.\r\n')
cecho('<gray>Use the <white>highlight<gray> command to manage your highlights.\r\n\r\n')  

--functions

function colorCheck(color)
  local color = color:lower()
  color = color:gsub("_", "")
  for color_name,_ in pairs(color_table) do
    if color_name:lower() == color then
      return color_name
    end
  end
  return false
end

function listHighlights()
  cecho("<white>Your current highlights:\r\n")

  for index, data in ipairs(highlights) do
    cecho("<white>(<orange_red>"..index.."<white>) <dim_gray>» <"..data["color"]..">"..data["text"].."\r\n") 
    end
  
  cecho("\r\n<white>highlight <color> <text> <gray>to highlight a phrase\r\n<white>highlight remove <number> <gray>to delete a highlight\r\n<white>highlight clear<gray> to clear all highlights\r\n\r\n")
end

rahsael
Posts: 3
Joined: Tue Mar 03, 2020 11:10 pm

Re: Quick and Powerful Highlighting Script

Post by rahsael »

Please see an updated version, plus aliases with variables, here: viewtopic.php?f=6&t=22856

Post Reply