Skip to content

Instantly share code, notes, and snippets.

@GINK03
Created December 30, 2020 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GINK03/4cf3201a9b39dedad9e7c982105b0b91 to your computer and use it in GitHub Desktop.
Save GINK03/4cf3201a9b39dedad9e7c982105b0b91 to your computer and use it in GitHub Desktop.
minimam_google_calendar_cui
#!/usr/bin/env python
"""
Example application for the 'blessed' Terminal library for python.
This isn't a real progress bar, just a sample "animated prompt" of sorts that demonstrates the
separate move_x() and move_yx() capabilities, made mainly to test the `hpa' compatibility for
'screen' terminal type which fails to provide one, but blessed recognizes that it actually does, and
provides a proxy.
"""
from __future__ import print_function
# std imports
import sys
# local
from blessed import Terminal
from google_cals_base import base
import datetime
from dataclasses import dataclass
from dataclasses import asdict
import pandas as pd
import numpy as np
import time
@dataclass
class Event:
d: datetime
summary: float
week: str
def build_events():
base_events = base()
events = []
for r in range(len(base_events)):
start = base_events[r]['start'].get('dateTime', base_events[r]['start'].get('date'))
d = datetime.datetime.strptime(start, "%Y-%m-%dT%H:%M:00+09:00")
events.append(Event(d=d, summary=base_events[r]["summary"], week=d.strftime("%a")))
# eventsでソートして
events.sort(key=lambda x: x.d)
# 一週間以内でfilterする
events = list(filter(lambda x: x.d - datetime.datetime.now() <= datetime.timedelta(days=7), events))
events = pd.DataFrame([asdict(event) for event in events])
events["day"] = events["d"].dt.strftime("%Y-%m-%d %a")
events["hour"] = events["d"].dt.strftime("%H")
return events.pivot(index="hour", columns="day", values="summary")
def main():
"""Program entry point."""
term = Terminal()
assert term.hpa(1) != u'', (
'Terminal does not support hpa (Horizontal position absolute)')
col, offset = 1, 1
ROW_SIZE = 25
events = build_events()
for r in range(ROW_SIZE):
print()
sys.stdout.flush()
X = np.empty((25, len(events.columns)), dtype="<U256")
X[:, :] = np.nan
X[0, :] = events.columns.tolist()
with term.cbreak():
for x, column in enumerate(events.columns):
aday = events[[column]]
aday = aday.reset_index()
for hour, summary in zip(aday["hour"], aday[column]):
X[int(hour), x] = summary if pd.notna(summary) else np.nan
for row in range(X.shape[0]):
y = term.height - 26+row
if row-1 == -1:
continue
sys.stderr.write(term.move_yx(y, 0) + f"{row-1: ^02d}|")
sys.stderr.flush()
cur_row = 1
cur_col = 0
inp = None
while inp != "q":
if inp == "j":
cur_row += 1
cur_row = min(X.shape[0], cur_row)
elif inp == "k":
cur_row -= 1
cur_row = max(0, cur_row)
elif inp == "h":
cur_col -= 1
cur_col = max(0, cur_col)
elif inp == "l":
cur_col += 1
cur_col = min(X.shape[1], cur_col)
buff = ""
for row in range(X.shape[0]):
for col in range(X.shape[1]):
y = term.height - 26+row
x = 5+(term.width//X.shape[1])*col
width = term.width//X.shape[1]-1
text = X[row, col]
if row == cur_row and col == cur_col:
buff += (term.move_yx(y, x) +
term.underline +
term.bold +
"{text:{fill}{align}{width}}|".format(
text=(str(text)[:width-6] if text != "nan" else "-"),
fill=" ",
align="<" if text != "nan" and row != 0 else "^",
width=width-1)
+ term.normal)
buff += term.green + term.underline + term.bold + term.move_yx(term.height, 0) + f"{text: ^{term.width//2}}"+ term.normal
elif row == cur_row or col == cur_col:
buff += (term.move_yx(y, x) +
term.bold +
"{text:{fill}{align}{width}}|".format(
text=(str(text)[:width-6] if text != "nan" else "-"),
fill=" ",
align="<" if text != "nan" and row != 0 else "^",
width=width-1)
+ term.normal)
else:
buff += (term.move_yx(y, x) + "{text:{fill}{align}{width}}|".format(
text=str(text)[:width-6] if text != "nan" else "-",
fill=" ",
align="<" if text != "nan" and row != 0 else "^",
width=width-1))
sys.stderr.write(buff)
sys.stderr.flush()
inp = term.inkey(0.04)
print(file=sys.stderr)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment