summaryrefslogtreecommitdiff
path: root/equote
blob: 3fb5a85b40a4ea4ea06019408c15af199ded8975 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env ruby

LINELENGTH = (ARGV.shift.chomp || 72).to_i
CHAR = ARGV.shift.chomp || ">"

lines = $stdin.readlines

lines = lines.map { |line|
  if line.length > (LINELENGTH-2) then
    newlines = []
    while line.length > (LINELENGTH-2) do
      i = LINELENGTH-2
      while line[i] != " " do
        i -= 1
      end
      newlines << line[0...i]
      line = line[i..-1]
    end
    newlines << line
    newlines
  else
    line
  end
}

lines = lines.flatten

lines = lines.map { |line|
  "#{CHAR} #{line}"
}

puts lines