Skip to content

Instantly share code, notes, and snippets.

@bartoleo
Last active January 30, 2019 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bartoleo/aab5ad2a8164eeae527b to your computer and use it in GitHub Desktop.
Save bartoleo/aab5ad2a8164eeae527b to your computer and use it in GitHub Desktop.
Groovy format text separated by a character/string in fixed width columns
def formatTextInColumns(def text, def separator, boolean mantainSeparator){
if (!text){
return ""
}
def columnSize = []
def result = ""
text.eachLine{
it.split("\\"+separator).eachWithIndex{element, index ->
if (element.length()>columnSize[index]?:0){
columnSize[index]=element.length()
}
}
}
text.eachLine{
it.split("\\"+separator).eachWithIndex{element, index ->
result += element + ' ' * ((columnSize[index]?:0)-element.length()) + (mantainSeparator?separator:" ")
}
result += System.lineSeparator
}
return result
}
@PinGwynn
Copy link

If you are using jenkins groovy, it has a bug (https://issues.jenkins-ci.org/browse/JENKINS-46988), then just replace "eachLine" to "split('\n').each".
Thanks to the author for this gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment