diff --git a/flomo/cli.py b/flomo/cli.py index ee01fe2..0865421 100644 --- a/flomo/cli.py +++ b/flomo/cli.py @@ -152,6 +152,24 @@ def update(session_id: str, tag: str | None, name: str | None): helpers.error_log(str(e)) print(e) +@flomo.command(aliases=["x"]) +@click.argument('file_path') +def export(file_path: str): + """ + Export all sessions to a CSV file. + + Args: + file_path (str): The file path where the CSV will be saved. + """ + try: + db = tracker.Tracker() + db.export(file_path) + db.conn.close() + print(f"Exported sessions to {file_path}") + except Exception as e: + helpers.error_log(f"Export Error: {e}") + print(f"Error exporting to CSV: {e}") + @flomo.command(aliases=["c"]) @click.option( diff --git a/flomo/tracker.py b/flomo/tracker.py index d8b2743..c8498d9 100644 --- a/flomo/tracker.py +++ b/flomo/tracker.py @@ -2,6 +2,7 @@ import datetime import sqlite3 from typing import Tuple +import csv from rich.console import Console from rich.table import Table @@ -107,7 +108,24 @@ def update_session(self, session_id: str, tag: str | None, name: str | None): ) print(f'Name updated to "{name}" for session {session_id}') self.conn.commit() - + + def export(self, file_path: str): + """ + Exports all sessions in the database to a CSV file. + Args: + file_path (str): The path to save the CSV file. + """ + # Fetch all sessions from the database + sessions = self.get_sessions() + + # Define the column headers + headers = ["id", "date_time", "tag", "name", "total_time"] + + # Write to CSV + with open(file_path, mode="w", newline="", encoding="utf-8") as csv_file: + writer = csv.writer(csv_file) + writer.writerow(headers) # Write headers + writer.writerows(sessions) # Write session rows def end_session(session_id: str): db = Tracker() @@ -118,7 +136,6 @@ def end_session(session_id: str): def show_sessions(): db = Tracker() sessions = db.get_sessions() - table = Table(title="You like this?") table.add_column("ID", style="cyan", justify="right") @@ -126,7 +143,6 @@ def show_sessions(): table.add_column("Tag", style="green") table.add_column("Name", style="yellow") table.add_column("Duration", style="red", justify="right") - for session in sessions: _id, datetime, tag, name, duration = session color = helpers.tag_color(tag) @@ -138,3 +154,6 @@ def show_sessions(): console.print(table) db.conn.close() + + +