I'm writing a program to represent the "qlocktwo" clock in python.
This is the code: The most important part to the question is the last four lines (I've left the rest to negate any confusion).
#!/usr/bin/python3
from datetime import datetime
import num2words
import string
grid = "itlisasampmacquarterdctwentyfivexhalfstenftopasterunineonesixthreefourfivetwoeightelevenseventwelvetenseoclock"
now = datetime.now()
minute = int(now.strftime("%M"))
hour = int(now.strftime("%H"))
def checkTime(hour, minute):
if hour >= 12:
hour -= 12
t = "past"
if hour == 0:
a = "oclock"
if minute > 30:
a = 60 - minute
t = "to"
hour += 1
return a, t, hour
elif minute <= 30:
a = minute
t = "past"
return a, t, hour
def round(a):
for i in range(1, 7):
i = i * 5
for j in range(i-2, i + 3):
if a == j:
return i
time = checkTime(hour, 0)
if time[0] != 0:
outMin = num2words.num2words(round(time[0])).replace("-", " ")
else:
outMin = "oclock"
outHour = num2words.num2words(time[2])
output = (f"it is {outMin} {time[1]} {outHour}").split(" ")
newGrid = grid
for i in output:
newGrid = newGrid.replace(i, f"\033[92m{i}\033[0m")
for i in [newGrid[i:i+10] for i in range(0, len(newGrid), 10)]:
print(i)
At the end, I've used this answer to split based on the number of characters (in my case, 10), but this also ends up counting hidden characters, such as terminal colors, so it splits on them as well.
So this is the output I get:
https://i.stack.imgur.com/O7wEe.jpg
Whereas, this is the output I want (but with the colors remaining):
https://i.stack.imgur.com/ib13N.jpg
Is there a way to split solely on visible characters?