forked from darknessomi/musicbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrollstring.py
58 lines (47 loc) · 1.57 KB
/
scrollstring.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import absolute_import
from builtins import int
from builtins import chr
from future import standard_library
standard_library.install_aliases()
from time import time
class scrollstring(object):
def __init__(self, content, START):
self.content = content # the true content of the string
self.display = content # the displayed string
self.START = START // 1 # when this instance is created
self.update()
def update(self):
self.display = self.content
curTime = time() // 1
offset = max(int((curTime - self.START) % len(self.content)) - 1, 0)
while offset > 0:
if self.display[0] > chr(127):
offset -= 1
self.display = self.display[3:] + self.display[:3]
else:
offset -= 1
self.display = self.display[1:] + self.display[:1]
# self.display = self.content[offset:] + self.content[:offset]
def __repr__(self):
return self.display
# determine the display length of a string
def truelen(string):
"""
It appears one Asian character takes two spots, but __len__
counts it as three, so this function counts the dispalyed
length of the string.
>>> truelen('abc')
3
>>> truelen('你好')
4
>>> truelen('1二3')
4
>>> truelen('')
0
"""
return len(string) - sum(1 for c in string if c > chr(127)) / 3