tkfrom.rb
来自「ruby的源代码」· RB 代码 · 共 133 行
RB
133 行
#! /usr/local/bin/rubyrequire "parsedate"require "base64"include ParseDateclass Mail def Mail.new(f) if !f.kind_of?(IO) f = open(f, "r") me = super(f) f.close else me = super end return me end def initialize(f) @header = {} @body = [] while f.gets() $_.chop! next if /^From / # skip From-line break if /^$/ # end of header if /^(\S+):\s*(.*)/ @header[attr = $1.capitalize] = $2 elsif attr sub(/^\s*/, '') @header[attr] += "\n" + $_ end end return if ! $_ while f.gets() break if /^From / @body.push($_) end end def header return @header end def body return @body endendif ARGV.length == 0 if ENV['MAIL'] ARGV[0] = ENV['MAIL'] elsif ENV['USER'] ARGV[0] = '/usr/spool/mail/' + ENV['USER'] elsif ENV['LOGNAME'] ARGV[0] = '/usr/spool/mail/' + ENV['LOGNAME'] endendrequire "tk"list = scroll = nilTkFrame.new{|f| list = TkListbox.new(f) { yscroll proc{|idx| scroll.set *idx } relief 'raised'# geometry "80x5" width 80 height 5 setgrid 'yes' pack('side'=>'left','fill'=>'both','expand'=>'yes') } scroll = TkScrollbar.new(f) { command proc{|idx| list.yview *idx } pack('side'=>'right','fill'=>'y') } pack}root = Tk.rootTkButton.new(root) { text 'Dismiss' command proc {exit} pack('fill'=>'both','expand'=>'yes')}root.bind "Control-c", proc{exit}root.bind "Control-q", proc{exit}root.bind "space", proc{exit}$outcount = 0;for file in ARGV next if File.exist?(file) atime = File.atime(file) mtime = File.mtime(file) f = open(file, "r") begin until f.eof mail = Mail.new(f) date = mail.header['Date'] next unless date from = mail.header['From'] subj = mail.header['Subject'] y = m = d = 0 y, m, d = parsedate(date) if date from = "sombody@somewhere" unless from subj = "(nil)" unless subj from = decode_b(from) subj = decode_b(subj) list.insert 'end', format('%-02d/%02d/%02d [%-28.28s] %s',y,m,d,from,subj) $outcount += 1 end ensure f.close File.utime(atime, mtime, file) list.see 'end' endendlimit = 10000if $outcount == 0 list.insert 'end', "You have no mail." limit = 2000endTk.after limit, proc{ exit}Tk.mainloop
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?