Skip to content

Instantly share code, notes, and snippets.

@winebarrel
Created February 7, 2016 06:20
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 winebarrel/8e4e188d93a255d88491 to your computer and use it in GitHub Desktop.
Save winebarrel/8e4e188d93a255d88491 to your computer and use it in GitHub Desktop.
Usage: wrap.rb any.txt 30
#!/usr/bin/env ruby
trap('PIPE', 'EXIT')
filnum = ARGV[0]
col = Integer(ARGV[1])
File.read(filnum).lines.each do |line|
state = 0
print line.split(//).slice_before {|ch|
n = ch.ord <= 0xFF ? 1 : 2
if (state + n) > col
state = n
true
else
state += n
false
end
}.map(&:join).join("\n")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment