Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Name: ccho
- # Author: 420rulez
- # Version: 1.0.0 (base)
- # Intent: awk "print" extension to replace php style tags (ex "<php></php>") with shell compatible varients.
- # allowing more controllable & system independant output.
- # Requires: none.
- # Usage: This function is a awk extension, & must be invoked either in an awk script, or with "awk -f".
- # usage format is: ccho(<text>,<newline(1|0)>,<enablecolor(1|0)>).
- function ccho(ccho_text,ccho_color,ccho_nline,ccho_enable){
- if(ccho_nline==1){
- ccho_nline = "\n"
- }else{
- ccho_nline = ""
- }
- if(ccho_enable==1){
- if(ccho_color==1) ccho_color = "\e";else ccho_color = "\\e"
- 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)
- 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)
- gsub(/<[cC]7>/, ccho_color "[37m", ccho_text) ; gsub(/<[bB]7>/, ccho_color "[47m", ccho_text) ; gsub(/<[mM]7>/, ccho_color "[7m", ccho_text)
- 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)
- 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)
- gsub(/<[cC]4>/, ccho_color "[34m", ccho_text) ; gsub(/<[bB]4>/, ccho_color "[44m", ccho_text) ; gsub(/<[mM]4>/, ccho_color "[4m", ccho_text)
- 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)
- 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)
- gsub(/<[cC]1>/, ccho_color "[31m", ccho_text) ; gsub(/<[bB]1>/, ccho_color "[41m", ccho_text) ; gsub(/<[mM]1>/, ccho_color "[1m", ccho_text)
- gsub(/<[cC]0>/, ccho_color "[30m", ccho_text) ; gsub(/<[bB]0>/, ccho_color "[40m", ccho_text) ; gsub(/<[mM]0>/, ccho_color "[0m", ccho_text)
- }
- printf("%s" ccho_nline,ccho_text)
- ccho_text = ""
- ccho_nline = ""
- ccho_enable = ""
- ccho_color = ""
- return
- }
- # Name: wrap
- # Author: 420rulez
- # Version: 1.1.6 (release/minimal)
- # Intent: wrap any un/formatted input text either from string or file, & wrap/format accordingly, allowing
- # all basic options & even allow SuperBox formatting options (depending on release type), & no external
- # requirements.
- # Requires: ccho (awk sub-function)/lcho (echo replacement/SuperBox builtin)
- # Usage: this function is a minimalistic build for building directly into addons/scripts to decrease dependabilities
- # in said scripts. as such, no verifications are included in this version.
- # If a full-featured wrap function is required, please see the SuperBox wrap builtin.
- # Usage format is: wrap(txt,len,join,marg,offs,btyp,sep,pres,ucol,usep,spec) # where:
- # <txt> - input text to be wrapped, newline character should previously be replaced with "</NL>" tag.
- # <len> - length to wrap specified input to, numaric values only, & obviously greater then 1.
- # <join> - join lines if possible to fill lines up to specified width.
- # <marg> - align all lines left by {N} characters if a number is specified, or use string otherwise.
- # <offs> - similar to <leftmargin>, but only adjust padding on continued lines.
- # <btype> - break on byte (1), break on word (0).
- # <sep> - set field seperator to use on output, does not effect input.
- # <pres> - enable (1)/disable (0) preserving leading whitespace on beginning of line.
- # <ucol> - enable (1)/disable (0) processing lcho (ccho) color/format tags.
- # <usep> - set field seperator to use on input (& output if unspecified).
- # <spec> - enable pattern break, if a word ends with an entry from this string, print line & move to next.
- # auto enabled if specified, must be one ASCII character per entry, seperated by a colon (":").
- # for example: ".:!" would start a new line if an entry ends with either "." or "!".
- #
- function wrap(txt,len,join,marg,offs,btyp,sep,pres,ucol,usep,spec){
- lcnt = split(txt,line,/<\/NL>/)
- line = ""
- if(usep!="")
- FS = sep
- for(a=1;a<=lcnt;a++){
- if(pres==1||pres==2){
- for(b=1;b<length(line[a]);b++)
- if(substr(line[a],b,1)!~/[ ]/) break
- if(b>1) line = line substr(line[a],1,b-1)
- }
- if(offs!=""&&gensub(/[0-9]/,"","g",offs)=="") offs = sprintf("%" offs "s","")
- if(marg!=""&&gensub(/[0-9]/,"","g",marg)=="") marg = sprintf("%" marg "s","")
- len = len - ( length(marg) + length(offs) )
- wcnt = split(line[a],word)
- if(wcnt==0&&join!=1){
- ccho(marg line,1,1,ucol)
- }else{
- for(b=1;b<=wcnt;b++){
- if(line!=""&&gensub(/[ ]/,"","g",line)!="")
- line = line sep
- if(length(gensub(/<([bBcCmM][0-9]|\/[eEcCbBmM])>/,"","g",line))+length(gensub(/<([bBcCmM][0-9]|\/[bBcCeEmM])>/,"","g",word[b]))>len){
- if(btyp!=1){
- ccho(marg line,1,1,ucol)
- line = offs word[b]
- }else{
- if(ucol!=1){
- printf("%s%s%s\n",marg line substr(word[b],1,len-length(gensub(/<([bBcCmM][0-9]|\/[bBcCeEmM])>/,"","g",line))))
- line = offs substr(word[b],len-length(gensub(/<([bBcCmM][0-9]|\/[bBcCeEmM])>/,"","g",line))+1)
- }else{
- tmp[1] = len - length(gensub(/<([bBcCmM][0-9]|\/[bBcCeEmM])>/,"","g",line))
- tmp[2] = word[b]
- tmp[3] = ""
- while(tmp[1]>0){
- if(substr(tmp[2],1,4)~/<([bBcCmM][0-9]|\/[bBcCeEmM])>/){
- tmp[3] = tmp[3] substr(tmp[2],1,4)
- tmp[2] = substr(tmp[2],5)
- }else{
- tmp[3] = tmp[3] substr(tmp[2],1,1)
- tmp[2] = substr(tmp[2],2)
- tmp[1] --
- }
- }
- ccho(marg line tmp[3],1,1,ucol)
- line = offs tmp[2]
- }
- }
- }else{
- if(spec!=""&&index(":" spec ":",":" substr(gensub(/<([bBcCmM][0-9]|\/[bBcCeEmM])>/,"","g",word[b]),length(gensub(/<([bBcCmM][0-9]|\/[bBcCeEmM])>/,"","g",word[b]))) ":")!=0){
- ccho(marg line word[b],1,1,ucol)
- line = offs
- }else{
- line = line word[b]
- }
- }
- }
- }
- if(join!=1&&gensub(/[ ]/,"","g",line)!=""){
- ccho(marg line,1,1,ucol)
- line = offs
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement