Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions flomo/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
25 changes: 22 additions & 3 deletions flomo/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime
import sqlite3
from typing import Tuple
import csv

from rich.console import Console
from rich.table import Table
Expand Down Expand Up @@ -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()
Expand All @@ -118,15 +136,13 @@ 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")
table.add_column("Session Date & Time", style="magenta")
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)
Expand All @@ -138,3 +154,6 @@ def show_sessions():
console.print(table)

db.conn.close()