Created
February 7, 2016 06:20
-
-
Save winebarrel/8e4e188d93a255d88491 to your computer and use it in GitHub Desktop.
Usage: wrap.rb any.txt 30
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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