add utils
This commit is contained in:
@@ -12,6 +12,33 @@ from inkycal.modules.template import inkycal_module
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class TaskEntry:
|
||||
"""Class representing a task entry."""
|
||||
|
||||
def __init__(self, title, project=None, parent_project=None, subtasks=None):
|
||||
self.title = title
|
||||
self.project = project
|
||||
self.consumed_time = 0 # in minutes
|
||||
self.subtasks = []
|
||||
def add_subtask(self, subtask):
|
||||
"""Add a subtask to the task entry."""
|
||||
self.subtasks.append(subtask)
|
||||
def mock_task_list():
|
||||
"""Generate a mock task list for testing purposes."""
|
||||
finetune = TaskEntry("3 new models finetune work", project="AISentry")
|
||||
generate_data = TaskEntry("Generate training data", project="AISentry")
|
||||
function_development = TaskEntry("Function development", project="AISentry")
|
||||
finetune.add_subtask(generate_data)
|
||||
finetune.add_subtask(function_development)
|
||||
|
||||
check_llama = TaskEntry("Check Llama model performance", project="llama.cpp")
|
||||
transform = TaskEntry("Transformers library exploration", project="llama.cpp")
|
||||
check_llama.add_subtask(transform)
|
||||
|
||||
research_work = TaskEntry("Research new AI techniques", project="AISentry")
|
||||
meeting = TaskEntry("Team meeting", project="General")
|
||||
return [finetune, check_llama, research_work, meeting]
|
||||
|
||||
def get_ip_address():
|
||||
"""Get public IP address from external service."""
|
||||
try:
|
||||
@@ -97,12 +124,35 @@ class Today(inkycal_module):
|
||||
|
||||
# additional configuration
|
||||
self.timezone = get_system_tz()
|
||||
|
||||
# 选择字体:优先使用支持中文的 NotoSansCJK,否则使用 NotoSans
|
||||
self._font_family = self._select_font_family()
|
||||
self.num_font = ImageFont.truetype(
|
||||
fonts['NotoSans-SemiCondensed'], size=self.fontsize
|
||||
fonts[self._font_family], size=self.fontsize
|
||||
)
|
||||
|
||||
# give an OK message
|
||||
logger.debug(f'{__name__} loaded')
|
||||
|
||||
def _select_font_family(self) -> str:
|
||||
"""选择合适的字体族(支持中文优先)"""
|
||||
# 优先级:NotoSansCJKsc (支持中文) > NotoSans
|
||||
preferred_fonts = [
|
||||
'NotoSansCJKsc-Regular',
|
||||
'NotoSans-SemiCondensed'
|
||||
]
|
||||
|
||||
for font_name in preferred_fonts:
|
||||
if font_name in fonts:
|
||||
logger.debug(f'Selected font: {font_name}')
|
||||
return font_name
|
||||
|
||||
# 如果都不存在,使用第一个可用字体
|
||||
return list(fonts.keys())[0]
|
||||
|
||||
def _get_font(self, size: int) -> ImageFont.FreeTypeFont:
|
||||
"""获取指定大小的字体"""
|
||||
return ImageFont.truetype(fonts[self._font_family], size=size)
|
||||
|
||||
@staticmethod
|
||||
def flatten(values):
|
||||
@@ -143,9 +193,7 @@ class Today(inkycal_module):
|
||||
# Edit left section - show today's date
|
||||
now = arrow.now(tz=self.timezone)
|
||||
month_height = int(im_height * 0.15)
|
||||
month_font = ImageFont.truetype(
|
||||
fonts['NotoSans-SemiCondensed'], size=int(self.fontsize * 1.5)
|
||||
)
|
||||
month_font = self._get_font(int(self.fontsize * 1.5))
|
||||
write(
|
||||
im_black,
|
||||
(0, 0),
|
||||
@@ -157,9 +205,7 @@ class Today(inkycal_module):
|
||||
|
||||
date_height = int(im_height * 0.5)
|
||||
date_y = month_height
|
||||
large_font = ImageFont.truetype(
|
||||
fonts['NotoSans-SemiCondensed'], size=int(self.fontsize * 4)
|
||||
)
|
||||
large_font = self._get_font(int(self.fontsize * 4))
|
||||
date_time = arrow.now()
|
||||
day = date_time.day
|
||||
print(str(day))
|
||||
@@ -174,9 +220,7 @@ class Today(inkycal_module):
|
||||
|
||||
weekday_y = month_height + date_height
|
||||
weekday_height = int(im_height * 0.15)
|
||||
weekday_font = ImageFont.truetype(
|
||||
fonts['NotoSans-SemiCondensed'], size=int(self.fontsize * 2)
|
||||
)
|
||||
weekday_font = self._get_font(int(self.fontsize * 2))
|
||||
write(
|
||||
im_black,
|
||||
(0, weekday_y),
|
||||
@@ -214,19 +258,7 @@ class Today(inkycal_module):
|
||||
# Edit right section - show today's events
|
||||
if self.show_events:
|
||||
# 导入日历解析器
|
||||
from inkycal.modules.ical_parser import iCalendar
|
||||
|
||||
parser = iCalendar()
|
||||
|
||||
if self.ical_urls:
|
||||
parser.load_url(self.ical_urls)
|
||||
if self.ical_files:
|
||||
parser.load_from_file(self.ical_files)
|
||||
|
||||
# 获取今天的事件
|
||||
today_start = now.floor('day')
|
||||
today_end = now.ceil('day')
|
||||
upcoming_events = parser.get_events(today_start, today_end, self.timezone)
|
||||
upcoming_events = True
|
||||
|
||||
# 计算右侧可用空间
|
||||
right_x = left_section_width + 5 # 留5px边距
|
||||
@@ -237,43 +269,61 @@ class Today(inkycal_module):
|
||||
text_bbox = self.font.getbbox("hg")
|
||||
line_height = text_bbox[3] + line_spacing
|
||||
max_lines = im_height // line_height
|
||||
|
||||
from inkycal.modules.super_productivity_utils import get_today_tasks
|
||||
|
||||
|
||||
json_file_path = '/workspaces/Inkycal/inkycal/modules/super_productivity.json'
|
||||
task_list = get_today_tasks(json_file_path)
|
||||
|
||||
if upcoming_events:
|
||||
# 显示事件
|
||||
cursor = 0
|
||||
for event in upcoming_events[:max_lines]:
|
||||
if cursor >= max_lines:
|
||||
break
|
||||
|
||||
y_pos = cursor * line_height
|
||||
|
||||
# 显示时间
|
||||
time_str = event['begin'].format(self.time_format, locale=self.language)
|
||||
time_width = int(self.font.getlength(time_str) * 1.1)
|
||||
# Split to 2 parts
|
||||
# Left part: title
|
||||
# Right part: Project name
|
||||
current_line = 0
|
||||
for idx, event in enumerate(task_list):
|
||||
if current_line >= max_lines:
|
||||
break # 超出显示范围,停止绘制
|
||||
|
||||
# 写任务标题
|
||||
write(
|
||||
im_black,
|
||||
(right_x, y_pos),
|
||||
(time_width, line_height),
|
||||
time_str,
|
||||
(right_x, current_line * line_height),
|
||||
(int(right_usable_width * 0.7), line_height),
|
||||
event.title,
|
||||
font=self.font,
|
||||
alignment='left'
|
||||
)
|
||||
|
||||
# 显示事件标题
|
||||
event_x = right_x + time_width + 5
|
||||
event_width = right_usable_width - time_width - 5
|
||||
# 写项目名称
|
||||
project_name = event.project_name if event.project_name else "Inbox"
|
||||
|
||||
write(
|
||||
im_black,
|
||||
(event_x, y_pos),
|
||||
(event_width, line_height),
|
||||
event['title'],
|
||||
im_colour,
|
||||
(right_x + int(right_usable_width * 0.7), current_line * line_height),
|
||||
(int(right_usable_width * 0.3), line_height),
|
||||
project_name,
|
||||
font=self.font,
|
||||
alignment='left'
|
||||
alignment='right'
|
||||
)
|
||||
|
||||
cursor += 1
|
||||
current_line += 1
|
||||
if event.subtasks:
|
||||
for sub_idx, subtask in enumerate(event.subtasks):
|
||||
if subtask.is_done:
|
||||
continue
|
||||
if current_line + sub_idx + 1 >= max_lines:
|
||||
break # 超出显示范围,停止绘制
|
||||
# 写子任务标题,缩进显示
|
||||
write(
|
||||
im_black,
|
||||
(right_x + 10, current_line * line_height),
|
||||
(int(right_usable_width * 0.7) - 10, line_height),
|
||||
f"- {subtask.title}",
|
||||
font=self.font,
|
||||
alignment='left'
|
||||
)
|
||||
current_line += 1 # 更新主循环的索引
|
||||
pass
|
||||
else:
|
||||
# 没有事件时显示提示
|
||||
write(
|
||||
|
||||
Reference in New Issue
Block a user