10 self.each_char do |char|
11 delta = char.bytesize > 1 ? 2 : 1
12 break if rlen + delta > len
16 res += ' ' * (len - rlen) if rlen < len
21 def fromout(date, from, subj)
23 y, m, d = Time.parse(date).to_a.reverse[4, 3] if date
24 y ||= 0; m ||= 0; d ||= 0
25 from ||= "sombody@somewhere"
27 from = from.kconv(Encoding.default_external).kjust(28)
30 subj = subj.kconv(Encoding.default_external).kjust(40)
31 printf "%02d/%02d/%02d [%s] %s\n", y%100, m, d, from, subj
35 def get_mailfile(user)
39 user = ENV['USER'] || ENV['USERNAME'] || ENV['LOGNAME']
42 if file == nil or !File.exist?(file)
43 [ENV['SPOOLDIR'], '/usr/spool', '/var/spool', '/usr', '/var'].each do |m|
44 path = "#{m}/mail/#{user}"
59 file = get_mailfile(ARGV[0])
63 atime = File.atime(file)
64 mtime = File.mtime(file)
65 open(file, "r") do |f|
69 next if /^From / =~ line # skip From-line
70 break if /^$/ =~ line # end of header
72 if /^(?<attr>\S+?):\s*(?<value>.*)/ =~ line
76 header[attr] += "\n" + line.lstrip
81 break if /^From / =~ line
83 outcount += fromout(header['Date'], header['From'], header['Subject'])
86 File.utime(atime, mtime, file)
90 print "You have no mail.\n"
93 system "stty cbreak -echo"
95 system "stty cooked echo"
111 ruby from.rb [-w] [username_or_filename]