Skip to content

Instantly share code, notes, and snippets.

@ppazos
Created June 10, 2024 13:18
Show Gist options
  • Save ppazos/4f1bf79a89d92ef896d13b8a32120813 to your computer and use it in GitHub Desktop.
Save ppazos/4f1bf79a89d92ef896d13b8a32120813 to your computer and use it in GitHub Desktop.
Generates a paragraph text from a long line, limiting line size and number of words per line
def s = "An EHR_STATUS resource needs to be always created and committed in the new EHR. This resource MAY be also supplied by the client as the request body. If not supplied, a default EHR_STATUS will be used by the service with following attributes"
def ss = s.split(" ")
println ss
def wlimit = 4
def climit = 23
def out = ''
def line = ''
def words_in_line = 0
for (int i=0; i<ss.size(); i++)
{
if (line.size() + ss[i].size() > climit)
{
out += line + "\n"
line = ss[i] + ' '
words_in_line = 1
}
else
{
if ((words_in_line + 1) % wlimit == 0)
{
out += line + ss[i] + "\n"
line = ''
words_in_line = 0
}
else
{
line += ss[i] +' '
words_in_line++
}
}
}
println out
@ppazos
Copy link
Author

ppazos commented Jun 10, 2024

// Simpler version limiting just line size in chars
def s = "An EHR_STATUS resource needs to be always created and committed in the new EHR. This resource MAY be also supplied by the client as the request body. If not supplied, a default EHR_STATUS will be used by the service with following attributes"

def ss = s.split(" ")

println ss


def climit = 23

def out = ''
def line = ''

for (int i=0; i<ss.size(); i++)
{
  if (line.size() + ss[i].size() > climit)
  {
    out += line + "\n"
    line = ss[i] + ' '
  }
  else
  {
    line += ss[i] +' '
  }
}

println out

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