Class: Screen

Inherits:
Object
  • Object
show all
Includes:
Curses
Defined in:
lib/pingman/screen.rb

Constant Summary collapse

TOP_OFFSET =
0
LETF_OFFSET =
2
RIGHT_OFFSET =
5
TIME_LOW =
1
HOSTNAME_WIDTH =
15
RESULT_WIDTH =
15
COL_OFFSET =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScreen

Returns a new instance of Screen.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pingman/screen.rb', line 17

def initialize
  init_screen
  start_color
  Curses.init_pair 1, COLOR_WHITE, COLOR_GREEN
  Curses.init_pair 2, COLOR_WHITE, COLOR_RED
  curs_set 0
  noecho
  sleep 0.05
  @lines = []
  @current_cols = cols
  @hostname = Socket.gethostname
  @hostaddr = Socket.getaddrinfo(Socket::gethostname, nil, Socket::AF_INET)[0][3]
  update
end

Instance Attribute Details

#linesObject

Returns the value of attribute lines.



6
7
8
# File 'lib/pingman/screen.rb', line 6

def lines
  @lines
end

Instance Method Details



32
33
34
35
36
37
38
39
40
41
42
43
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
# File 'lib/pingman/screen.rb', line 32

def print_header(line_num)
  title = 'Ping Man'
  setpos(line_num, (cols - title.length) / 2)
  addstr(title)
  line_num += 1

  host = "#{@hostname}(#{@hostaddr})"
  setpos(line_num, LETF_OFFSET)
  addstr(host)
  line_num += 1

  usage = 'Keytype: [a] Display addr, [i] ping interval, [t] ping timeout, [q]uit'
  setpos(line_num, LETF_OFFSET)
  addstr(usage)
  line_num += 1

  line_num += 1

  offset = LETF_OFFSET
  setpos(line_num, offset)
  addstr('HOSTNAME')
  offset += HOSTNAME_WIDTH
  setpos(line_num, offset)
  addstr('ADDRESS')
  offset = cols - RIGHT_OFFSET - RESULT_WIDTH
  setpos(line_num, offset)
  addstr('RESULT')
  offset -= 'SNT'.length + COL_OFFSET
  setpos(line_num, offset)
  addstr('SNT')
  offset -= 'AVG'.length + COL_OFFSET
  setpos(line_num, offset)
  addstr('AVG')
  offset -= 'RTT'.length + COL_OFFSET
  setpos(line_num, offset)
  addstr('RTT')
  offset -= 'LOSS%'.length + COL_OFFSET
  setpos(line_num, offset)
  addstr('LOSS%')
  line_num += 1
end


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/pingman/screen.rb', line 74

def print_line(line)
  setpos(@lines_low + line.row, 0)
  deleteln
  refresh
  insertln
  attron(A_BLINK) unless line.res
  setpos(@lines_low + line.row, LETF_OFFSET - 2)
  addstr('> ') if line.sending
  offset = LETF_OFFSET
  setpos(@lines_low + line.row, offset)
  addstr(line.hostname.to_s)
  offset += HOSTNAME_WIDTH
  setpos(@lines_low + line.row, offset)
  addstr(line.address.to_s)
  offset = cols - RIGHT_OFFSET - RESULT_WIDTH
  setpos(@lines_low + line.row, offset)
  addstr(line.log.slice(0, 15).map{|c| c ? '.' : '!'}.join)
  offset -= 'SNT'.length + COL_OFFSET
  setpos(@lines_low + line.row, offset + 'SNT'.length - line.snt.to_s.length)
  addstr(line.snt.to_s)
  offset -= 'AVG'.length + COL_OFFSET
  avg = (line.avg * 1000).round.to_s
  setpos(@lines_low + line.row, offset + 'AVG'.length - avg.length)
  addstr(avg)
  offset -= 'RTT'.length + COL_OFFSET
  rtt = (line.rtt * 1000).round.to_s
  setpos(@lines_low + line.row, offset + 'RTT'.length - rtt.length)
  addstr(rtt)
  offset -= 'LOSS%'.length + COL_OFFSET
  loss = "#{sprintf('%.1f',line.loss * 100)}%"
  setpos(@lines_low + line.row, offset + 'LOSS%'.length - loss.length)
  addstr(loss)
  offset -= '   OK   '.length + COL_OFFSET
  setpos(@lines_low + line.row, offset)
  if line.res
    attron(color_pair(1))
    addstr('   OK   ')
  else
    attron(color_pair(2))
    addstr('   NG   ')
  end
  attroff(A_COLOR)
  attroff(A_BLINK)
end


119
120
121
122
123
# File 'lib/pingman/screen.rb', line 119

def print_lines
  @lines.each do |line|
    print_line line
  end if @lines
end


125
126
127
128
129
130
# File 'lib/pingman/screen.rb', line 125

def print_time(line_num)
  time = "[last update #{Time.now.strftime("%m-%d %H:%M:%S")}]"
  setpos(line_num, cols - time.length - RIGHT_OFFSET)
  addstr(time)
  line_num += 1
end

#updateObject



132
133
134
135
136
137
# File 'lib/pingman/screen.rb', line 132

def update
  clear
  @lines_low = print_header(TOP_OFFSET)
  print_lines
  refresh
end

#update_line(line) ⇒ Object



139
140
141
142
143
144
145
146
147
# File 'lib/pingman/screen.rb', line 139

def update_line(line)
  if @current_cols != cols
    @current_cols = cols
    update
  end
  print_line line
  print_time TIME_LOW
  refresh
end