Skip to content

Instantly share code, notes, and snippets.

@namuol
Created May 1, 2012 03:56
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 namuol/2564838 to your computer and use it in GitHub Desktop.
Save namuol/2564838 to your computer and use it in GitHub Desktop.
Simple algorithm to split a line of text based on a desired line width. For fixed-width fonts only.
split_to_lines = (str, max_width) ->
lines = []
words = str.split(' ')
current_line = ''
for word in words
if current_line.length + word.length > max_width
lines.push current_line
current_line = word + ' '
else
current_line += word + ' '
lines.push current_line
return lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment