merentha_fluffos_v2/
merentha_fluffos_v2/bin/
merentha_fluffos_v2/fluffos-2.9-ds2.03/
merentha_fluffos_v2/fluffos-2.9-ds2.03/ChangeLog.old/
merentha_fluffos_v2/fluffos-2.9-ds2.03/Win32/
merentha_fluffos_v2/fluffos-2.9-ds2.03/compat/
merentha_fluffos_v2/fluffos-2.9-ds2.03/compat/simuls/
merentha_fluffos_v2/fluffos-2.9-ds2.03/include/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/clone/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/command/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/data/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/etc/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/include/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/inherit/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/inherit/master/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/log/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/single/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/single/tests/compiler/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/single/tests/efuns/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/single/tests/operators/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/u/
merentha_fluffos_v2/fluffos-2.9-ds2.03/tmp/
merentha_fluffos_v2/fluffos-2.9-ds2.03/windows/
merentha_fluffos_v2/lib/cfg/
merentha_fluffos_v2/lib/cfg/races/
merentha_fluffos_v2/lib/cmds/abilities/
merentha_fluffos_v2/lib/cmds/actions/
merentha_fluffos_v2/lib/cmds/spells/
merentha_fluffos_v2/lib/daemon/include/
merentha_fluffos_v2/lib/daemon/services/
merentha_fluffos_v2/lib/doc/
merentha_fluffos_v2/lib/doc/building/
merentha_fluffos_v2/lib/doc/help/classes/
merentha_fluffos_v2/lib/doc/help/general/
merentha_fluffos_v2/lib/doc/help/races/
merentha_fluffos_v2/lib/doc/help/skills/
merentha_fluffos_v2/lib/doc/help/stats/
merentha_fluffos_v2/lib/doc/man/efuns/
merentha_fluffos_v2/lib/doc/man/lfuns/
merentha_fluffos_v2/lib/doc/news/
merentha_fluffos_v2/lib/doc/old/
merentha_fluffos_v2/lib/doc/old/concepts/
merentha_fluffos_v2/lib/doc/old/lpc/constructs/
merentha_fluffos_v2/lib/doc/old/lpc/types/
merentha_fluffos_v2/lib/domains/ROOMS/
merentha_fluffos_v2/lib/domains/obj/armour/
merentha_fluffos_v2/lib/domains/obj/monsters/
merentha_fluffos_v2/lib/domains/obj/other/
merentha_fluffos_v2/lib/domains/obj/weapons/
merentha_fluffos_v2/lib/realms/petrarch/
merentha_fluffos_v2/lib/save/daemons/
merentha_fluffos_v2/lib/save/rid/
merentha_fluffos_v2/lib/save/users/a/
merentha_fluffos_v2/lib/save/users/p/
merentha_fluffos_v2/lib/save/users/t/
merentha_fluffos_v2/lib/std/login/
merentha_fluffos_v2/lib/std/obj/
merentha_fluffos_v2/win32/
#include <daemons.h>

string right_align(string str, int size) {
  if(!size) return str;
  if(strlen(str)>size) return str;
  while(strlen(str)<size) str=" "+str;
  return str;
}

string strip_colours(string str) {
    mapping cols=COLOUR_D->query_ansi();
    string *colours=keys(cols);
    int i=sizeof(colours);
    while(i--) if(str) str=replace_string(str, "%^"+colours[i]+"%^", "");
    return str;
}

string restore_colours(string str) {
    mapping cols=COLOUR_D->query_ansi();
    string *colours=keys(cols);
    int i=sizeof(colours);
    while(i--) str=replace_string(str,cols[colours[i]], "%^"+colours[i]+"%^");
    return str;
}

varargs string arrange_string(string str, int i) {
    if(!i || i<2) i=75;
    i+=(strlen(str)-strlen(strip_colours(str)));
    if(strlen(str)>=i) return str[0..(i-1)];
    i-=strlen(str);
    while(i--) str+=" ";
    return str;
}  

varargs string wrap(string str, int i, int j) {
    if(!str) return "";
    if(!j) return terminal_colour(str, COLOUR_D->query_ansi(),(i?i:75));
    return restore_colours(terminal_colour(str, COLOUR_D->query_ansi(),(i?i:75)));
}

varargs string center(string str, int i) {
    string *strs=explode(wrap(str, (i?i:75), 1), "\n");
    string tmp;
    int x=sizeof(strs);
    if (!i || i< 10) i=75;
    while(x--) {
        tmp=arrange_string("",to_int((i-strlen(strip_colours(strs[x])))/2));
        strs[x]=tmp+strs[x]+tmp;
    }
    return implode(strs, "\n");
}

varargs string border(string str, string title, int i) {
    string *strs=explode(wrap(str, (i?i-4:71), 1), "\n");
    string tmp, line="|";
    int x=sizeof(strs), y;

    if(!i || i<10) i=75;
    y=i-2;
    while(y--) line+="-"; line+="|";
    i-=4;
    y=0;
    while(y<x) {
      if(strs[y]=="__BORDER_LINE__") {
         strs[y]="%^RESET%^%^BLUE%^"+line;
         y++;
         continue;
      }
      sscanf(strs[y], "%%^%s%%^%s", tmp, strs[y]);
      if(!tmp) tmp="";
      strs[y]="%^RESET%^%^BLUE%^|%^RESET%^ "+arrange_string(replace_string((tmp!=""?"%^"+tmp+"%^":"")+strs[y],"\n", ""), i)+" %^RESET%^%^BLUE%^|";
      y++;
    }
    str="";
    x=i+4;
    while(x--) str+="-";
    tmp=" %^RESET%^%^BLUE%^`"+str[2..<3]+"'";
    if(title)
        str=arrange_string(str, (i-strlen(title))/2)+"%^BOLD%^%^BLUE%^<%^MAGENTA%^ "+title+" %^BOLD%^%^BLUE%^>%^RESET%^%^BLUE%^"+arrange_string(str, (i-strlen(title))/2);
    if( i%2 != strlen(str)%2) str+="-";
    str=" ,"+str[2..<3]+".";
    return "%^RESET%^%^BLUE%^"+str+"\n"+implode(strs, "\n")+"\n"+tmp;
}

varargs string format_page(string *items, int cols, int i) {
    string tmp="";
    int x, y, z=sizeof(items);
    if(!i) i=75;
    if(!cols) cols=2;
    i-=cols+1;
    for(x=0;x<z;x+=cols) {
        for(y=0;y<cols;y++)
            if(z>x+y)
                tmp+=arrange_string(items[x+y], to_int(i/cols));
        if(z>x+y) tmp+="\n";
    }
    return tmp+"\n";
}