sample/cal.rb


DEFINITIONS

This source file includes following functions.


   1  #! /usr/bin/env ruby
   2  
   3  # cal.rb: Written by Tadayoshi Funaba 1998-2002
   4  # $Id: cal.rb,v 2.4 2002-06-08 00:40:29+09 tadf Exp $
   5  
   6  require 'date'
   7  require 'getopts'
   8  
   9  $tab =
  10  {
  11    'cn' => true,    # China
  12    'de' => 2342032, # Germany (protestant states)
  13    'dk' => 2342032, # Denmark
  14    'es' => 2299161, # Spain
  15    'fi' => 2361390, # Finland
  16    'fr' => 2299227, # France
  17    'gb' => 2361222, # United Kingdom
  18    'gr' => 2423868, # Greece
  19    'hu' => 2301004, # Hungary
  20    'it' => 2299161, # Italy
  21    'jp' => true,    # Japan
  22    'no' => 2342032, # Norway
  23    'pl' => 2299161, # Poland
  24    'pt' => 2299161, # Portugal
  25    'ru' => 2421639, # Russia
  26    'se' => 2361390, # Sweden
  27    'us' => 2361222, # United States
  28    'os' => false,   # (old style)
  29    'ns' => true     # (new style)
  30  }
  31  
  32  $cc = 'gb'
  33  
  34  def usage
  35    $stderr.puts 'usage: cal [-c iso3166] [-jmty] [[month] year]'
  36    exit 1
  37  end
  38  
  39  def pict(y, m, sg)
  40    d = (1..31).detect{|d| Date.valid_date?(y, m, d, sg)}
  41    fi = Date.new(y, m, d, sg)
  42    fi -= (fi.jd - $k + 1) % 7
  43  
  44    ve  = (fi..fi +  6).collect{|cu|
  45      %w(S M Tu W Th F S)[cu.wday]
  46    }
  47    ve += (fi..fi + 41).collect{|cu|
  48      if cu.mon == m then cu.send($da) end.to_s
  49    }
  50  
  51    ve = ve.collect{|e| e.rjust($dw)}
  52  
  53    gr = group(ve, 7)
  54    gr = trans(gr) if $OPT_t
  55    ta = gr.collect{|xs| xs.join(' ')}
  56  
  57    ca = %w(January February March April May June July
  58            August September October November December)[m - 1]
  59    ca = ca + ' ' + y.to_s if not $OPT_y
  60    ca = ca.center($mw)
  61  
  62    ta.unshift(ca)
  63  end
  64  
  65  def group(xs, n)
  66    (0..xs.size / n - 1).collect{|i| xs[i * n, n]}
  67  end
  68  
  69  def trans(xs)
  70    (0..xs[0].size - 1).collect{|i| xs.collect{|x| x[i]}}
  71  end
  72  
  73  def unite(xs)
  74    if xs.empty? then [] else xs[0] + unite(xs[1..-1]) end
  75  end
  76  
  77  def block(xs, n)
  78    unite(group(xs, n).collect{|ys| trans(ys).collect{|zs| zs.join('  ')}})
  79  end
  80  
  81  def unlines(xs)
  82    xs.collect{|x| x + "\n"}.join
  83  end
  84  
  85  usage unless getopts('jmty', "c:#{$cc}")
  86  
  87  y, m = ARGV.select(1, 0).compact.collect{|x| x.to_i}
  88  $OPT_y ||= (y and not m)
  89  
  90  to = Date.today
  91  y ||= to.year
  92  m ||= to.mon
  93  
  94  usage unless m >= 1 and m <= 12
  95  usage unless y >= -4712
  96  usage if (sg = $tab[$OPT_c]).nil?
  97  
  98  $dw = if $OPT_j then 3 else 2 end
  99  $mw = ($dw + 1) * 7 - 1
 100  $mn = if $OPT_j then 2 else 3 end
 101  $tw = ($mw + 2) * $mn - 2
 102  
 103  $k  = if $OPT_m then 1 else 0 end
 104  $da = if $OPT_j then :yday else :mday end
 105  
 106  print(if not $OPT_y
 107          unlines(pict(y, m, sg))
 108        else
 109          y.to_s.center($tw) + "\n\n" +
 110            unlines(block((1..12).collect{|m| pict(y, m, sg)}, $mn)) + "\n"
 111        end)