Page 1 of 1

Creating messages with allias and string functions

Posted: Wed Apr 22, 2015 4:50 pm
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.                                                      |
  |                                                                           |
  +---------------------------------------------------------------------------+

Re: Creating messages with allias and string functions

Posted: Wed Apr 22, 2015 8:35 pm
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

Re: Creating messages with allias and string functions

Posted: Wed Apr 22, 2015 8:46 pm
by azure_glass
Image

Re: Creating messages with allias and string functions

Posted: Wed Apr 22, 2015 11:20 pm
by Akaya
Heh