Creating messages with allias and string functions

Post Reply
azure_glass
Posts: 97
Joined: Wed Jul 25, 2012 12:35 pm

Creating messages with allias and string functions

Post by azure_glass »

Hi, i want to do something like that:

I have long text i allias:

Pattern:
^/message (.*)$

Code:
matches[2] = MessageVariable

-------------------------------------------------------------

Input: - Long text without "enters" () copied from wiki to alias
/message This article focuses on poetry written in English from the United Kingdom: England, Scotland, Wales, and Northern Ireland (and Ireland before 1922). However, though the whole of Ireland was politically part of the United Kingdom between January 1801 and December 1922, it is controversial to describe Irish literature as British.[citation needed] For some this includes works by authors from Northern Ireland. The article does not include poetry from other countries where the English language is spoken.
--- Magic thing what code do:

Output: - text cant go over 80 characters . So i must part text to fit in "|" the font is regular so i want to text look clean like lower.

My biggest problem is with "spaces" in parting. How code recognize when take full words of maximum lenght not passing 74 characters (i must live room for " | " and " |")

Code: [show] | [select all] lua
  +---------------------------------------------------------------------------+
  |                                                                           |
  |  This article focuses on poetry written in English from the United        |
  |  Kingdom: England, Scotland, Wales, and Northern Ireland (and Ireland     |
  |  before 1922). However, though the whole of Ireland was politically part  |
  |  of the United Kingdom between January 1801 and December 1922, it is      |
  |  controversial to describe Irish literature as British.[citation needed]  |
  |  For some this includes works by authors from Northern Ireland. The       |
  |  article does not include poetry from other countries where the English   |
  |  language is spoken.                                                      |
  |                                                                           |
  +---------------------------------------------------------------------------+
English is not my native language. If you don't understand what im writing ask. :)
Ubuntu 17.04, Mudlet 3.1

User avatar
Belgarath
Posts: 232
Joined: Fri Jul 26, 2013 7:19 am
Discord: macjabeth#7149

Re: Creating messages with allias and string functions

Post by Belgarath »

Lo there. Belgy to the rescue!
Code: [show] | [select all] lua
function message (text)
  local function split (str, max_line_length)
    local lines = {}
    local line
    str:gsub('(%s*)(%S+)', function (spc, word)
        if not line or #line + #spc + #word > max_line_length then
          table.insert(lines, line)
          line = word
        else
          line = line .. spc .. word
        end
      end)
    table.insert(lines, line)
    return lines
  end
  local lines = split(text, 74)
  print("+" .. string.rep("-", 78) .. "+")
  print("|" .. string.rep(" ", 78) .. "|")
  for i, line in ipairs(lines) do
    print("| " .. string.format("%-76s", line) .. " |")
  end
  print("|" .. string.rep(" ", 78) .. "|")
  print("+" .. string.rep("-", 78) .. "+")
end

azure_glass
Posts: 97
Joined: Wed Jul 25, 2012 12:35 pm

Re: Creating messages with allias and string functions

Post by azure_glass »

Image
English is not my native language. If you don't understand what im writing ask. :)
Ubuntu 17.04, Mudlet 3.1

User avatar
Akaya
Posts: 414
Joined: Thu Apr 19, 2012 1:36 am

Re: Creating messages with allias and string functions

Post by Akaya »

Heh

Post Reply