diff options
| author | Kyren223 <Kyren223@proton.me> | 2024-12-11 18:12:29 +0200 |
|---|---|---|
| committer | Kyren223 <Kyren223@proton.me> | 2024-12-11 18:12:29 +0200 |
| commit | cfa5f95c4e2d7f3fcc9d5c52d3e3682c269df955 (patch) | |
| tree | 541b87d5194130c8ef90b629846d7728e28cc946 | |
| parent | be84087eb446866d1637058ca792bff00edb2f5d (diff) | |
Added sleep tracker python script and keybind to toggle render-markdown
| -rw-r--r-- | .config/nvim/lua/custom/plugins/render-markdown.lua | 3 | ||||
| -rw-r--r-- | hosts/laptop-nixos/home.nix | 5 | ||||
| -rwxr-xr-x | scripts/sleep-tracker.py | 96 |
3 files changed, 103 insertions, 1 deletions
diff --git a/.config/nvim/lua/custom/plugins/render-markdown.lua b/.config/nvim/lua/custom/plugins/render-markdown.lua index 4769afc..a7ae2b7 100644 --- a/.config/nvim/lua/custom/plugins/render-markdown.lua +++ b/.config/nvim/lua/custom/plugins/render-markdown.lua @@ -1,6 +1,9 @@ return { 'MeanderingProgrammer/render-markdown.nvim', event = { 'BufReadPre', 'BufNewFile' }, + keys = { + { '<leader>mt', '<<cmd>RenderMarkdown toggle<cr>'} + }, opts = { render_modes = true, sign = { enabled = false }, diff --git a/hosts/laptop-nixos/home.nix b/hosts/laptop-nixos/home.nix index 2a5535e..3428dfc 100644 --- a/hosts/laptop-nixos/home.nix +++ b/hosts/laptop-nixos/home.nix @@ -45,7 +45,10 @@ go pnpm nodejs_23 - python3Full + (python312.withPackages (pypkgs: [ + pypkgs.matplotlib + pypkgs.pandas + ])) watchexec golangci-lint libnotify diff --git a/scripts/sleep-tracker.py b/scripts/sleep-tracker.py new file mode 100755 index 0000000..305116a --- /dev/null +++ b/scripts/sleep-tracker.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python +import matplotlib.pyplot as plt +import pandas as pd +import argparse +from datetime import datetime + +# Hardcoded file path +FILE_PATH = "/home/kyren/personal/dairy/sleep-tracker.md" + +def parse_time(value, is_evening=False): + if value == "N/A" or value == "Morning" or value == "Evening": + return None + time = datetime.strptime(value, "%I:%M %p") + hour = time.hour + time.minute / 60 + if is_evening and time.hour < 12: + hour += 24 + return hour + +def format_time(value): + if pd.isna(value): + return "N/A" + hour = int(value) % 24 + minute = int((value - int(value)) * 60) + period = "AM" if hour < 12 else "PM" + hour = hour if hour <= 12 else hour - 12 + hour = 12 if hour == 0 else hour + return f"{hour}:{minute:02d} {period}" + +def read_data(file_path): + with open(file_path, 'r') as f: + lines = f.readlines()[4:] # Skip the first four lines + + data = [] + + for line in lines: + parts = [part.strip() for part in line.strip().split('|') if part.strip()] + if len(parts) == 3 and parts[0] != "Date": + date, morning, evening = parts + data_point = [ + date, + parse_time(morning), + parse_time(evening, is_evening=True) + ] + data.append(data_point) + + df = pd.DataFrame(data, columns=['Date', 'Morning', 'Evening']) + df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d', errors='coerce') + return df.dropna(subset=['Date']) + +def filter_data(data, start_date, end_date): + mask = (data['Date'] >= start_date) & (data['Date'] <= end_date) + return data[mask] + +def plot_graph(data): + plt.style.use('dark_background') + plt.figure(figsize=(10, 6)) + plt.plot(data['Date'], data['Morning'], 'r-o', label='Morning') + plt.plot(data['Date'], data['Evening'], 'b-o', label='Evening') + plt.title(f'Time Graph from {data["Date"].min().date()} to {data["Date"].max().date()}') + plt.xlabel('Date') + plt.ylabel('Time (Hours)') + plt.xticks(rotation=0) + plt.grid(True, linestyle='--', alpha=0.6) + plt.legend() + plt.show() + +def print_statistics(data): + for period in ['Morning', 'Evening']: + times = data[period].dropna() + if not times.empty: + print(f"{period}: Min={format_time(times.min())}, Max={format_time(times.max())}, Avg={format_time(times.mean())}, Median={format_time(times.median())}") + else: + print(f"{period}: No data available") + +def main(): + parser = argparse.ArgumentParser(description="Plot time graph from a data file.") + parser.add_argument('--from', dest='start_date', type=str, help='Start date (yyyy-mm-dd)') + parser.add_argument('--to', dest='end_date', type=str, help='End date (yyyy-mm-dd)') + args = parser.parse_args() + + data = read_data(FILE_PATH) + + start_date = pd.to_datetime(args.start_date, format='%Y-%m-%d', errors='coerce') if args.start_date else data['Date'].min() + end_date = pd.to_datetime(args.end_date, format='%Y-%m-%d', errors='coerce') if args.end_date else data['Date'].max() + + filtered_data = filter_data(data, start_date, end_date) + + if filtered_data.empty: + print("No data available for the specified date range.") + return + + print_statistics(filtered_data) + plot_graph(filtered_data) + +if __name__ == '__main__': + main() |
