-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitl
More file actions
executable file
·164 lines (127 loc) · 3.88 KB
/
gitl
File metadata and controls
executable file
·164 lines (127 loc) · 3.88 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.13"
# dependencies = ["rich"]
# ///
import subprocess
import sys
from datetime import datetime
from subprocess import check_output
from rich.console import Console
from rich.text import Text
DEFAULT_LINES = 8
COLORS = {
"hash": (90, 70, 140),
"date": (102, 91, 255),
"time": (79, 164, 255),
"dim": (60, 60, 80),
"at": (70, 70, 90),
"msg": (220, 218, 240),
"paren": (100, 100, 120),
"head": (255, 249, 86),
"main": (104, 244, 255),
"branch": (79, 255, 249),
}
def make_fader(day_idx):
fade = max(0.85**day_idx, 0.45) if day_idx > 0 else 1.0
def apply(name):
r, g, b = COLORS[name]
return f"rgb({int(r * fade)},{int(g * fade)},{int(b * fade)})"
return apply
def parse_commit(line):
h, year, month, day, hour, minute, msg, branches = line.split(";", 7)
return {
"hash": h,
"date": (int(year), int(month), int(day)),
"time": (hour, minute),
"month_day": (month, day),
"msg": msg,
"branches": branches.strip().strip("()"),
}
def format_branches(branches, fade):
text = Text()
if not branches:
return text
text.append(" (", style=fade("paren"))
for i, branch in enumerate(branches.split(",")):
branch = branch.strip()
if i > 0:
text.append(", ", style=fade("paren"))
if " -> " in branch:
_, branch = branch.split(" -> ")
text.append("HEAD", style=f"bold {fade('head')}")
text.append(" -> ", style=fade("paren"))
is_origin = branch.startswith("origin/")
name = branch[7:] if is_origin else branch
base_style = "bold underline" if is_origin else "bold"
color = "head" if name == "HEAD" else "main" if name in ("main", "master") else "branch"
text.append(name, style=f"{base_style} {fade(color)}")
text.append(")", style=fade("paren"))
return text
def format_datetime(commit, now, fade):
text = Text()
year, month, day = commit["date"]
month_str, day_str = commit["month_day"]
hour, minute = commit["time"]
if year < now.year:
text.append(f"{year}-{month_str}{day_str}", style=fade("time"))
else:
same_month = month == now.month
same_day = same_month and day == now.day
if same_day:
text.append(f"{month_str}{day_str}", style=fade("dim"))
elif same_month:
text.append(month_str, style=fade("dim"))
text.append(day_str, style=fade("date"))
else:
text.append(f"{month_str}{day_str}", style=fade("date"))
text.append("@", style=fade("at"))
text.append(f"{hour}{minute}", style=fade("time"))
return text
def get_commits(n, console):
try:
log = check_output(
["git", "log", "-n", str(n), "--pretty=format:%h;%cd;%s;%d", "--date=format:%Y;%m;%d;%H;%M"],
stderr=subprocess.DEVNULL,
).decode()
except Exception:
console.print("gitl: not a git repository (or no commits yet)", style="red")
return []
return [parse_commit(line) for line in log.splitlines()] if log else []
def assign_day_indices(commits):
current_day = None
day_idx = -1
for commit in commits:
if commit["date"] != current_day:
current_day = commit["date"]
day_idx += 1
commit["day_idx"] = day_idx
def main():
console = Console()
now = datetime.now()
today = (now.year, now.month, now.day)
if len(sys.argv) > 1:
try:
commits = get_commits(int(sys.argv[1]), console)
except ValueError:
console.print("Usage: gitl [n_lines]", style="red")
sys.exit(1)
else:
commits = get_commits(100, console)
today_count = sum(1 for c in commits if c["date"] == today)
commits = commits[: max(DEFAULT_LINES, today_count)]
if not commits:
return
assign_day_indices(commits)
for commit in commits:
fade = make_fader(commit["day_idx"])
text = Text()
text.append(commit["hash"], style=f"bold {fade('hash')}")
text.append(" ")
text.append_text(format_datetime(commit, now, fade))
text.append(" ")
text.append(commit["msg"], style=fade("msg"))
text.append_text(format_branches(commit["branches"], fade))
console.print(text)
if __name__ == "__main__":
main()