A small diary in your terminal! https://mskf1383.codeberg.page/terminal-diary.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.3 KiB

#!/usr/bin/python3
import os
from sys import argv as sys_argv
from datetime import date, timedelta
try:
from colorama import Back, Fore, Style
lite_mode = False
except ModuleNotFoundError:
print("Warning: We use 'colorama' library to print colored text.\n"
+ "Until it is not installed, all text print with no color.\n"
+ "You can install it using 'pip3 install colorama' command.")
input("Press ENTER to continue")
print("")
lite_mode = True
# Variables
today = date.today()
year = today.year
month = today.month
day = today.day
home_dir = os.path.expanduser("~")
# Generate colored text
def primary(text):
if lite_mode == False:
return Back.BLUE + text + Style.RESET_ALL
return text
def warning(text):
if lite_mode == False:
return Fore.YELLOW + text + Style.RESET_ALL
return text
def danger(text):
if lite_mode == False:
return Fore.RED + text + Style.RESET_ALL
return text
def success(text):
if lite_mode == False:
return Fore.GREEN + text + Style.RESET_ALL
return text
def bright(text):
if lite_mode == False:
return Style.BRIGHT + text + Style.RESET_ALL
return text
# Clear the screen
def clear():
os.system("cls" if os.name == "nt" else "clear")
# Read written notes
def read(date):
date = date.replace("-0", "-")
try:
with open(f"{home_dir}/Notes/{date}.md", "r") as f:
clear()
note = f.read().split("\n")
if len(str(len(note))) == 1:
print(primary(" "), end="")
else:
print(primary(" ") * (len(str(len(note))) - 1), end="")
print(primary("Terminal Diary ") + " "
+ primary(" Read ") + " "
+ primary(f" {date.replace('-', '/')} "))
for line in range(len(note)):
if len(str(len(note))) == 1:
print(primary(f" {line + 1}"), end="")
else:
print(primary(" " * (len(str(len(note)))
- len(str(line + 1)))
+ f"{line + 1}"), end="")
print(" " + note[line])
input("\nPress ENTER to exit")
clear()
except FileNotFoundError:
print(danger("No note!"))
# Write note
def write():
clear()
print(primary(" Termianl Diary ") + " "
+ primary(" Write ") + " "
+ primary(f" {year}/{month}/{day} "))
text = ""
while True:
line = input(primary(" >") + " ") + "\n"
if line == "\n":
line = input(primary(" >") + " ") + "\n"
if line == "\n":
break
text += "\n"
text += line
if text != "":
note_dir = f"{home_dir}/Notes/{year}-{month}-{day}.md"
if os.path.exists(note_dir):
text = "\n" + text
with open(note_dir, "a") as f:
f.write(text)
input(success(f"\nYour note saved in '{note_dir}'.")
+ " Press ENTER to exit")
clear()
else:
input(danger(f"\nYou don't write enything!") + " Press ENTER to exit")
clear()
# Main
if not os.path.exists(f"{home_dir}/Notes"):
os.mkdir(f"{home_dir}/Notes")
if len(sys_argv) == 1:
print(danger("Please give a command!"))
print("run 'diary --help' to get help.")
elif sys_argv[1] == "write":
write()
elif sys_argv[1] == "read":
if len(sys_argv) == 2:
print(danger("Please enter the date!"))
print("run 'diary --help' to get help.")
elif sys_argv[2] == "today":
read(f"{year}-{month}-{day}")
elif sys_argv[2] == "yesterday":
yesterday = today - timedelta(days=1)
read(f"{yesterday.year}-{yesterday.month}-{yesterday.day}")
else:
read(f"{sys_argv[2]}")
elif sys_argv[1] == "--help":
print("usage: diary <command>\n")
print("Commands:")
print(" write To write note")
print(" read <date> To read written notes\n")
print("Instead of <date>:")
print(" today To read today's note")
print(" yesterday To read yesterday's note")
print(" Or the exact time like 2021-1-1")
else:
print(danger("Unknown command!"))
print("run 'diary --help' to get help.")