Class: BitClock

Inherits:
Object
  • Object
show all
Defined in:
lib/bitclock.rb

Overview

! /bin/ruby

Class Method Summary collapse

Class Method Details

.calc_constantsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bitclock.rb', line 44

def self.calc_constants
  ($height, $width) = `stty size`.split(/\s/).map(&:to_i)
  colpos = [39,33,27]
  colpos = [39] if $height / $width.to_f < 0.1282
  i = 0
  $cols = 0

  colpos.each {|i| $cols = i; break if i < $width}
  $coef = $width / $cols
  rest = $width % $cols
  $frnt = (rest/2.0).ceil
  $back = $width - $cols*$coef - $frnt
  $coev = ($coef/2.0).ceil

  while ($coev > $height)
    if i < colpos.length - 1
      $cols = colpos[i]
      i += 1
      $coef = $width / $cols
      rest = $width % $cols
    else
      $coef -= 1
      rest = $width - $cols * $coef
    end
    $coev = ($coef/2.0).ceil
    $frnt = (rest/2.0).ceil
    $back = $width - $cols*$coef - $frnt
  end

  $top = ($height - $coev * 5) / 2
  $bottom = $height - $top - (5*$coev) - 2
  $ltrs = ($cols - 9) / 6
end

.calc_pix(num, line) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bitclock.rb', line 78

def self.calc_pix num, line
  ltr = $nums[num]
  rel = [[0,3,3,4,4],[nil,3,3,4,4],[1,3,5,4,6],[nil,5,5,6,6],[2,5,5,6,6]]
  out = []
  $ltrs.times {out << " "}
  #$full = num.to_s
  rel[line].each_with_index do |r,i|
    next unless r
    if ltr[r] == 1 and i == 0
      out.map! {$full}
      break
    elsif ltr[r] == 1 and i < 3
      out[0] = $full
    elsif ltr[r] == 1
      out[-1] = $full
    end
  end
  return out
end


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/bitclock.rb', line 98

def self.print_clock time, pts
  puts "\e[H\e[2J"
  tarr = time.strftime("%H:%M:%S").split(":")
  lines = []
  5.times do |i|
    row = ""
    tarr.each_with_index do |num,ind|
      clr = $clr[ind]
      BitClock.calc_pix(num[0],i).each {|pix| row += "#{clr}#{pix * $coef}\e[0m"}
      row += " " * $coef
      BitClock.calc_pix(num[1],i).each {|pix| row += "#{clr}#{pix * $coef}\e[0m"}
      break if ind == 2
      row += " " * $coef
      if pts and [1,3].include?(i)
        row += "\e[1m\e[30m#{($full * $coef)}\e[0m"
      else
        row += " " * $coef
      end
      row += " " * $coef
    end
    lines[i] = ""
    $frnt.times {lines[i] += " "}
    lines[i] += row
    $back.times {lines[i] += " "}
  end
  $top.times {print " "*$width}
  5.times {|i| $coev.times {print lines[i]}}
  $bottom.times {print " "*$width}
  print " "*($width-2)
end

.quit?Boolean

check if user wants to quit (‘q’ key does not work)

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bitclock.rb', line 29

def self.quit?
  begin
    while c = STDIN.read_nonblock(1)
      return true if 'qQ'.split.include? c
    end
    false
  rescue Errno::EINTR
    false
  rescue Errno::EAGAIN
    false
  rescue EOFError
    true
  end
end

.run(argv) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/bitclock.rb', line 129

def self.run argv
  td = argv.grep(Float)
  argv -= td
  td = td.any? ? (td.first * 60 * 60) : 0
  case argv.length
  when 3
    $clr = argv
  when 2
    $clr = [argv[0],argv[0],argv[1]]
  when 1
    $clr = [argv[0],argv[0],argv[0]]
  else
    $clr = ["\e[0m\e37m", "\e[0m\e37m", "\e[0m\e37m"]
  end
  while Time.now.strftime("%L").to_i < 999
  end
  BitClock.calc_constants
  sleep 0.35
  pts = -1
  loop do
    if BitClock.quit?
      print "\r"+" "*$width
      break
    end
    begin
      sleep 0.5
      BitClock.print_clock (Time.now + td), [false,true][(pts+1)/2]
      pts *= -1
    rescue $catch
      print "\r"+" "*$width
      break
    end
    BitClock.calc_constants unless "#{$height} #{$width}\n" == `stty size`
  end
end