Skip to content

Commit 0b7d777

Browse files
authored
Use enum for --color-by options (#80)
* Use Enum for coloring options Also move some stray Enums into git-sim.enums.py. Enums have two advantages here, they: - show all options when using git-sim --help - help avoid typos in the string comparison in git_sim_base_command's color_by() function * update help string for option * use Union[..., None] instead of Optional[...] for backwards compatibility --------- Signed-off-by: Jacob Stopak <jacob@initialcommit.io>
1 parent cc116cb commit 0b7d777

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed

git_sim/__main__.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import pathlib
2-
import typer
1+
import datetime
32
import os
3+
import pathlib
44
import sys
5-
import datetime
65
import time
76

8-
import git_sim.commands
7+
import typer
98

10-
from git_sim.settings import ImgFormat, VideoFormat, settings
9+
import git_sim.commands
10+
from git_sim.settings import ColorByOptions, ImgFormat, VideoFormat, settings
1111

1212
app = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]})
1313

@@ -126,17 +126,17 @@ def main(
126126
settings.all,
127127
help="Display all local branches in the log output",
128128
),
129-
color_by: str = typer.Option(
129+
color_by: ColorByOptions = typer.Option(
130130
settings.color_by,
131-
help="Color commits by parameter, such as author",
131+
help="Color commits by parameter",
132132
),
133133
highlight_commit_messages: bool = typer.Option(
134134
settings.highlight_commit_messages,
135135
help="Make the displayed commit messages more prominent",
136136
),
137137
):
138138
import git
139-
from manim import config, WHITE
139+
from manim import WHITE, config
140140

141141
settings.animate = animate
142142
settings.n = n

git_sim/enums.py

+17
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,20 @@ class StashSubCommand(Enum):
1212
POP = "pop"
1313
APPLY = "apply"
1414
PUSH = "push"
15+
16+
17+
class ColorByOptions(Enum):
18+
author = "author"
19+
branch = "branch"
20+
notlocal1 = "notlocal1"
21+
notlocal2 = "notlocal2"
22+
23+
24+
class VideoFormat(str, Enum):
25+
mp4 = "mp4"
26+
webm = "webm"
27+
28+
29+
class ImgFormat(str, Enum):
30+
jpg = "jpg"
31+
png = "png"

git_sim/git_sim_base_command.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import platform
2-
import sys
31
import os
4-
import tempfile
2+
import platform
53
import shutil
64
import stat
5+
import sys
6+
import tempfile
77

88
import git
99
import manim as m
1010
import numpy
1111
from git.exc import GitCommandError, InvalidGitRepositoryError
1212
from git.repo import Repo
1313

14+
from git_sim.enums import ColorByOptions
1415
from git_sim.settings import settings
1516

1617

@@ -1179,7 +1180,7 @@ def create_zone_text(
11791180
thirdColumnFilesDict[f] = text
11801181

11811182
def color_by(self, offset=0):
1182-
if settings.color_by == "author":
1183+
if settings.color_by == ColorByOptions.author:
11831184
sorted_authors = sorted(
11841185
self.author_groups.keys(),
11851186
key=lambda k: len(self.author_groups[k]),
@@ -1208,17 +1209,17 @@ def color_by(self, offset=0):
12081209
self.recenter_frame()
12091210
self.scale_frame()
12101211

1211-
elif settings.color_by == "branch":
1212+
elif settings.color_by == ColorByOptions.branch:
12121213
pass
12131214

1214-
elif settings.color_by == "notlocal1":
1215+
elif settings.color_by == ColorByOptions.notlocal1:
12151216
for commit_id in self.drawnCommits:
12161217
try:
12171218
self.orig_repo.commit(commit_id)
12181219
except ValueError:
12191220
self.drawnCommits[commit_id].set_color(m.GOLD)
12201221

1221-
elif settings.color_by == "notlocal2":
1222+
elif settings.color_by == ColorByOptions.notlocal2:
12221223
for commit_id in self.drawnCommits:
12231224
if not self.orig_repo.is_ancestor(commit_id, "HEAD"):
12241225
self.drawnCommits[commit_id].set_color(m.GOLD)

git_sim/settings.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
import pathlib
2-
3-
from enum import Enum
42
from typing import List, Union
5-
from pydantic import BaseSettings
6-
7-
8-
class VideoFormat(str, Enum):
9-
mp4 = "mp4"
10-
webm = "webm"
113

4+
from pydantic import BaseSettings
125

13-
class ImgFormat(str, Enum):
14-
jpg = "jpg"
15-
png = "png"
6+
from git_sim.enums import ColorByOptions, ImgFormat, VideoFormat
167

178

189
class Settings(BaseSettings):
@@ -46,7 +37,7 @@ class Settings(BaseSettings):
4637
invert_branches = False
4738
hide_merged_branches = False
4839
all = False
49-
color_by: Union[str, None] = None
40+
color_by: Union[ColorByOptions, None] = None
5041
highlight_commit_messages = False
5142

5243
class Config:

0 commit comments

Comments
 (0)