Advertisement
420rulez

advanced word wrap - awk

Aug 1st, 2015
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 7.49 KB | None | 0 0
  1.   # Name:     ccho
  2.   # Author:   420rulez
  3.   # Version:  1.0.0 (base)
  4.   # Intent:   awk "print" extension to replace php style tags (ex "<php></php>") with shell compatible varients.
  5.   #           allowing more controllable & system independant output.
  6.   # Requires: none.
  7.   # Usage:    This function is a awk extension, & must be invoked either in an awk script, or with "awk -f".
  8.   #           usage format is: ccho(<text>,<newline(1|0)>,<enablecolor(1|0)>).
  9.   function ccho(ccho_text,ccho_color,ccho_nline,ccho_enable){
  10.     if(ccho_nline==1){
  11.       ccho_nline = "\n"
  12.     }else{
  13.       ccho_nline = ""
  14.     }
  15.     if(ccho_enable==1){
  16.       if(ccho_color==1)  ccho_color = "\e";else ccho_color = "\\e"
  17.       gsub(/<\/[eE]>/, ccho_color "[39;49;0m",     ccho_text)         ; gsub(/<([mM]8|\/[cC])>/, ccho_color "[21;24m",ccho_text)        ; gsub(/<\/[mM]>/, ccho_color "[21;22;23;24;25m",      ccho_text)
  18.       gsub(/<([cC]9|\/[cC])>/, ccho_color "[39m"  ccho_text)         ; gsub(/<([bB]9|\/[bB])>/, ccho_color "[49m", ccho_text)         ; gsub(/<([mM]9|\/[mM])>/,z "[21;24m",       ccho_text)
  19.       gsub(/<[cC]7>/,          ccho_color "[37m", ccho_text)         ; gsub(/<[bB]7>/,          ccho_color "[47m", ccho_text)         ; gsub(/<[mM]7>/,  ccho_color "[7m",                  ccho_text)
  20.       gsub(/<[cC]6>/,          ccho_color "[36m", ccho_text)         ; gsub(/<[bB]6>/,          ccho_color "[46m", ccho_text)         ; gsub(/<[mM]6>/,  ccho_color "[1;4;7m",              ccho_text)
  21.       gsub(/<[cC]5>/,          ccho_color "[35m", ccho_text)         ; gsub(/<[bB]5>/,          ccho_color "[45m", ccho_text)         ; gsub(/<[mM]5>/,  ccho_color "[4;7m",                ccho_text)
  22.       gsub(/<[cC]4>/,          ccho_color "[34m", ccho_text)         ; gsub(/<[bB]4>/,          ccho_color "[44m", ccho_text)         ; gsub(/<[mM]4>/,  ccho_color "[4m",                  ccho_text)
  23.       gsub(/<[cC]3>/,          ccho_color "[33m", ccho_text)         ; gsub(/<[bB]3>/,          ccho_color "[43m", ccho_text)         ; gsub(/<[mM]3>/,  ccho_color "[1;4m",                ccho_text)
  24.       gsub(/<[cC]2>/,          ccho_color "[32m", ccho_text)         ; gsub(/<[bB]2>/,          ccho_color "[42m", ccho_text)         ; gsub(/<[mM]2>/,  ccho_color "[1;7m",                ccho_text)
  25.       gsub(/<[cC]1>/,          ccho_color "[31m", ccho_text)         ; gsub(/<[bB]1>/,          ccho_color "[41m", ccho_text)         ; gsub(/<[mM]1>/,  ccho_color "[1m",                  ccho_text)
  26.       gsub(/<[cC]0>/,          ccho_color "[30m", ccho_text)         ; gsub(/<[bB]0>/,          ccho_color "[40m", ccho_text)         ; gsub(/<[mM]0>/,  ccho_color "[0m",                  ccho_text)
  27.     }
  28.     printf("%s" ccho_nline,ccho_text)
  29.     ccho_text   = ""
  30.     ccho_nline  = ""
  31.     ccho_enable = ""
  32.     ccho_color  = ""
  33.     return
  34.   }
  35.   # Name:     wrap
  36.   # Author:   420rulez
  37.   # Version:  1.1.6 (release/minimal)
  38.   # Intent:   wrap any un/formatted input text either from string or file, & wrap/format accordingly, allowing
  39.   #           all basic options & even allow SuperBox formatting options (depending on release type), & no external
  40.   #           requirements.
  41.   # Requires: ccho (awk sub-function)/lcho (echo replacement/SuperBox builtin)
  42.   # Usage:    this function is a minimalistic build for building directly into addons/scripts to decrease dependabilities
  43.   #           in said scripts. as such, no verifications are included in this version.
  44.   #           If a full-featured wrap function is required, please see the SuperBox wrap builtin.
  45.   #           Usage format is: wrap(txt,len,join,marg,offs,btyp,sep,pres,ucol,usep,spec)  #             where:
  46.   #              <txt>   - input text to be wrapped, newline character should previously be replaced with "</NL>" tag.
  47.   #              <len>   - length to wrap specified input to, numaric values only, & obviously greater then 1.
  48.   #              <join>  - join lines if possible to fill lines up to specified width.
  49.   #              <marg>  - align all lines left by {N} characters if a number is specified, or use string otherwise.
  50.   #              <offs>  - similar to <leftmargin>, but only adjust padding on continued lines.
  51.   #              <btype> - break on byte (1), break on word (0).
  52.   #              <sep>   - set field seperator to use on output, does not effect input.
  53.   #              <pres>  - enable (1)/disable (0) preserving leading whitespace on beginning of line.
  54.   #              <ucol>  - enable (1)/disable (0) processing lcho (ccho) color/format tags.
  55.   #              <usep>  - set field seperator to use on input (& output if unspecified).
  56.   #              <spec>  - enable pattern break, if a word ends with an entry from this string, print line & move to next.
  57.   #                        auto enabled if specified, must be one ASCII character per entry, seperated by a colon (":").
  58.   #                        for example: ".:!" would start a new line if an entry ends with either "." or "!".
  59.   #
  60.   function wrap(txt,len,join,marg,offs,btyp,sep,pres,ucol,usep,spec){
  61.     lcnt = split(txt,line,/<\/NL>/)
  62.     line = ""
  63.     if(usep!="")
  64.       FS = sep
  65.     for(a=1;a<=lcnt;a++){
  66.       if(pres==1||pres==2){
  67.         for(b=1;b<length(line[a]);b++)
  68.           if(substr(line[a],b,1)!~/[    ]/)                  break
  69.           if(b>1)                                   line = line substr(line[a],1,b-1)
  70.       }
  71.       if(offs!=""&&gensub(/[0-9]/,"","g",offs)=="") offs = sprintf("%" offs "s","")
  72.       if(marg!=""&&gensub(/[0-9]/,"","g",marg)=="") marg = sprintf("%" marg "s","")
  73.       len = len - ( length(marg) + length(offs) )
  74.       wcnt = split(line[a],word)
  75.       if(wcnt==0&&join!=1){
  76.         ccho(marg line,1,1,ucol)
  77.       }else{
  78.         for(b=1;b<=wcnt;b++){
  79.           if(line!=""&&gensub(/[    ]/,"","g",line)!="")
  80.             line = line sep
  81.           if(length(gensub(/<([bBcCmM][0-9]|\/[eEcCbBmM])>/,"","g",line))+length(gensub(/<([bBcCmM][0-9]|\/[bBcCeEmM])>/,"","g",word[b]))>len){
  82.             if(btyp!=1){
  83.              ccho(marg line,1,1,ucol)
  84.              line = offs word[b]
  85.             }else{
  86.               if(ucol!=1){
  87.                 printf("%s%s%s\n",marg line substr(word[b],1,len-length(gensub(/<([bBcCmM][0-9]|\/[bBcCeEmM])>/,"","g",line))))
  88.                 line = offs substr(word[b],len-length(gensub(/<([bBcCmM][0-9]|\/[bBcCeEmM])>/,"","g",line))+1)
  89.               }else{
  90.                 tmp[1] = len - length(gensub(/<([bBcCmM][0-9]|\/[bBcCeEmM])>/,"","g",line))
  91.                 tmp[2] = word[b]
  92.                 tmp[3] = ""
  93.                 while(tmp[1]>0){
  94.                   if(substr(tmp[2],1,4)~/<([bBcCmM][0-9]|\/[bBcCeEmM])>/){
  95.                     tmp[3] = tmp[3] substr(tmp[2],1,4)
  96.                     tmp[2] =        substr(tmp[2],5)
  97.                   }else{
  98.                     tmp[3] = tmp[3] substr(tmp[2],1,1)
  99.                     tmp[2] =        substr(tmp[2],2)
  100.                     tmp[1]   --
  101.                   }
  102.                 }
  103.                 ccho(marg line tmp[3],1,1,ucol)
  104.                 line = offs tmp[2]
  105.               }
  106.             }
  107.           }else{
  108.             if(spec!=""&&index(":" spec ":",":" substr(gensub(/<([bBcCmM][0-9]|\/[bBcCeEmM])>/,"","g",word[b]),length(gensub(/<([bBcCmM][0-9]|\/[bBcCeEmM])>/,"","g",word[b]))) ":")!=0){
  109.                 ccho(marg line word[b],1,1,ucol)
  110.               line = offs
  111.             }else{
  112.               line = line word[b]
  113.             }
  114.           }
  115.         }
  116.       }
  117.       if(join!=1&&gensub(/[     ]/,"","g",line)!=""){
  118.         ccho(marg line,1,1,ucol)
  119.         line = offs
  120.       }
  121.     }
  122.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement