-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCursesTimer.py
More file actions
181 lines (143 loc) · 4.51 KB
/
Copy pathCursesTimer.py
File metadata and controls
181 lines (143 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
73
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
118
119
120
121
122
123
124
125
126
127
128
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import curses
import timeUtil
import Utils
class CursesTimer:
'''
cursesを使ったタイマーを扱うクラス
'''
def __init__(self):
self.sec = 0
self.func = 0
#funcが0ならストップウォッチ1ならタイマー
self.strings = ""
self.termLines = Utils.Utils.get_terminal_lines(self)
self.termColumns = Utils.Utils.get_terminal_columns(self)
self.needCentering = True
def setStrings(self, s):
'''
秒数の前に表示する文字を追加する
事前にinitStrings()をcallしておくこと
'''
self.strings += s
def initStrings(self):
'''
self.stringsを初期化する
'''
self.strings = ""
def setSecond(self, second):
'''
秒数を受け取り、self.secにセットする。
引数 :self
:second 秒数
タイマーは受け取った秒数動作する
'''
self.sec = second
def setTimer(self, second):
'''
秒数を受け取りfuncを1に
引数 :self
:second 秒数
'''
self.setSecond(second)
self.func = 1
def setStopwatch(self):
'''
funcを0にしてsecondに-1を
'''
self.func = 0
self.sec = -1
def interrupt(self, w):
'''
文字列の入力を検知
引数 :self
:cursesのインスタンス
何か入力があった場合は入力された文字列を返す
'''
ch = w.getch()
if ch is not -1:
return ch
else:
return True
def centering(self, w):
if self.needCentering is True and self.termLines > 10:
w.move(int(self.termLines/2)-10, 0)
def display_time(self, w, now, string):
'''
時間を表示する
引数 :self
:cursesのインスタンス
:秒数
'''
self.centering(w)
if self.func == 0:
s += str(now) + string
w.addstr(s)
elif self.func == 1 and now < self.sec:
#s += " " + self.modify_time(round((self.sec - now), 1)) + string
s = self.make_strings(now, string)
w.addstr(s)
w.refresh()
def make_strings(self, now, string):
p = Utils.Utils()
t = p.progress_bar(now/self.sec) + " " +\
self.modify_time(round((self.sec - now), 1)) + " " +\
self.strings + \
"\n" +\
string
return t
def modify_time(self, time):
'''
分表示に整形してかえす
'''
m = int(time//60)
s = round(time%60, 1)
ret = str(m) + "min"
ret = ret + str(s) + "sec"
return ret
def curses_main(self):
'''
タイマー本体
cursesを起動して、setSecondで指定された秒数数える
もし途中で中断したらFalseを返す
'''
bFlag = False
t = timeUtil.Timer() #タイマーの初期化
w = curses.initscr() #cursesの初期化
curses.noecho()
curses.cbreak()
now = 0
w.timeout(0)
#セットされた時間になるまで0.1秒ごとに経過時間を表示する
while now < self.sec or self.sec == -1:
now = t.getModifiedTime()
self.display_time(w,now, " press 's' key: stop, press 'p' key: pause")
timeUtil.time.sleep(0.1)
w.clear()
ch = self.interrupt(w)
if ch is not True:
if ch == ord('s'):
bFlag = True
break
elif ch == ord('p'):
t.pause()
w.timeout(-1)
self.display_time(w, now, " press 's' key: stop, press else key: restart")
ch = w.getch()
if ch == ord('s'):
bFlag = True
break
w.timeout(0)
w.clear()
t.restart()
curses.nocbreak()
w.keypad(0)
curses.echo()
curses.endwin()
return bFlag == False
def start_Timer(self, sec):
self.setTimer(sec)
#curses_mainを起動
return self.curses_main()
def start_Stopwatch(self):
self.setStopwatch()
return self.curses_main()