~kris/dots

srice

ref: c8d9c68937f2c331a6bac47481ed40147fe73cba srice/.local/bin/academic/math-utils.py -rw-r--r-- 1.5 KiB
c8d9c689 — Kris Yotam shell migration: fish -> mksh, reorganize bin/, wallpaper cleanup 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
"""
===============================================================
 Script: math-utils.py
 Author: Kris Yotam (aka. khr1st)
 Date:   2025-09-29
 License: MIT License
 Description:
   Shared utility functions for string manipulation and formatting.
   Used across rofi scripts and lecture/course management.

 Inspiration:
   Adapted and extended from Gilles Castel's lecture note workflow.
===============================================================
"""

# -----------------------------
# Constants
# -----------------------------

MAX_LEN = 40


# -----------------------------
# String helpers
# -----------------------------

def beautify(string: str) -> str:
    """
    Replace underscores/hyphens with spaces and title-case the string.
    Example: "riemann-surfaces" -> "Riemann Surfaces"
    """
    return string.replace("_", " ").replace("-", " ").title()


def unbeautify(string: str) -> str:
    """
    Replace spaces with hyphens and lowercase the string.
    Example: "Riemann Surfaces" -> "riemann-surfaces"
    """
    return string.replace(" ", "-").lower()


def generate_short_title(title: str) -> str:
    """
    Shorten a title for compact display.
    - Truncate to MAX_LEN with ellipsis
    - Remove math dollar signs
    - Provide fallback for empty titles
    """
    short_title = title or "Untitled"
    if len(short_title) >= MAX_LEN:
        short_title = short_title[: MAX_LEN - len(" ... ")] + " ... "
    return short_title.replace("$", "")