Skip to content

Commit 313c23f

Browse files
authored
Merge pull request #3 from NF-coder/dev
Add github languages coloring
2 parents d9bbf42 + 121a686 commit 313c23f

File tree

7 files changed

+113
-40
lines changed

7 files changed

+113
-40
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,36 @@ Then you can use it with
99
```html
1010
<img src="https://your-gh-page-url/out.svg">
1111
```
12+
13+
## Configuring
14+
Example config u can see in `setting.yaml`
15+
```yaml
16+
general:
17+
top_k: 4 # how many sections will be on donut-chart (including "Other" section)
18+
plane:
19+
height: 140 # height of svg
20+
width: 250 # width of svg
21+
coloring:
22+
type: "github" # type of coloring (github or oklch available)
23+
other_color: "#666666" # color of "Other" section
24+
# coloring:
25+
# type: "oklch" # type of coloring (github or oklch available)
26+
# chroma: 0.099 # coloring oklch chroma
27+
# lightness: 0.636 # coloring oklch lightness
28+
# other_color: "#666666" # color of "Other" section
29+
excluded_languages: # list of languages that should be excluded
30+
- Jupyter Notebook # removed because jupyter files are too large
31+
32+
legend:
33+
margin_x: 140 # margin of legend (x-axis)
34+
margin_y: 30 # margin of legend (y-axis)
35+
space_between_captions: 22 # space between legend options
36+
font_color: "#c1c1c1" # font color
37+
38+
diagram:
39+
outer_radius: 55 # outer circle radius
40+
thickness: 12 # size of donut-chart
41+
margin_x: 20 # margin of diagram (x-axis)
42+
margin_y: 15 # margin of diagram (y-axis)
43+
```
44+
About **oklch** u can read [here](https://oklch.com/)

settings.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ general:
44
height: 140
55
width: 250
66
coloring:
7-
type: "oklch"
8-
chroma: 0.099
9-
lightness: 0.636
7+
type: "github"
8+
other_color: "#666666"
9+
# coloring:
10+
# type: "oklch"
11+
# chroma: 0.099
12+
# lightness: 0.636
1013
excluded_languages:
1114
- Jupyter Notebook
1215

src/main.py

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,82 @@
11
import os
2+
from dataclasses import dataclass
23

34
from renderer.renderer import RenderBuilder
45
from oklchUtils.OKLCHUtils import OKLCHUtils
56
from calc import Calc
67
from settings import Settings
78
from fetcher.fetcher import FetchLangStats
89

9-
# Fetch info
10+
# Some settinga
1011
USERNAME = os.environ['USERNAME']
1112
TOKEN = os.environ['GITHUB_TOKEN']
1213
OUTPUT_FILE: str = "./out.svg"
1314
SETTINGS_FILE: str = "./settings.yaml"
1415

1516
SETTINGS = Settings.from_yaml(SETTINGS_FILE)
1617

18+
19+
@dataclass
20+
class LangData:
21+
name: str
22+
size: float
23+
github_color: str
24+
1725
def get_langs_data(
1826
token: str,
1927
username: str,
2028
exclude_langs: list[str] = SETTINGS.GENERAL_SETTINGS.EXCLUDED_LANGUAGES
21-
) -> dict[str, float]:
22-
info = {}
23-
total_size = 0
29+
) -> list[LangData]:
30+
info: dict[str, LangData] = {}
31+
total_size: float = 0
2432

2533
for elem in FetchLangStats(token).fetch_user(username):
2634
if elem.name in exclude_langs: continue
2735

2836
total_size += elem.size
29-
if elem.name not in info: info[elem.name] = elem.size
30-
else: info[elem.name] += elem.size
37+
if elem.name not in info: info[elem.name] = LangData(elem.name, elem.size, elem.github_color)
38+
else: info[elem.name].size += elem.size
3139

32-
return {k: info[k]/total_size for k in info}
40+
return [LangData(
41+
info[k].name,
42+
info[k].size/total_size,
43+
info[k].github_color
44+
) for k in info]
3345

34-
def truncate(langs_arr: list[tuple[float, str]], k: int):
46+
def truncate(langs_arr: list[LangData], k: int):
3547
if len(langs_arr) <= k: return langs_arr
36-
return langs_arr[:k-1] + [(sum(map(lambda x: x[0], langs_arr[k-1:])), "Other")]
48+
return langs_arr[:k-1] + [LangData("Other", sum(map(lambda x: x.size, langs_arr[k-1:])), SETTINGS.GENERAL_SETTINGS.COLORING.OTHER_COLOR)]
3749

38-
def main():
39-
languages_stats = get_langs_data(TOKEN, USERNAME)
50+
def coloring(sorted_percents: list[LangData]) -> list[str]:
51+
coloring_cfg = SETTINGS.GENERAL_SETTINGS.COLORING
52+
53+
if coloring_cfg.TYPE == "oklch":
54+
if hasattr(coloring_cfg, "CHROMA") and hasattr(coloring_cfg, "LIGHTNESS"):
55+
return OKLCHUtils.create_colors_array(
56+
length=len(sorted_percents),
57+
chroma=getattr(coloring_cfg, "CHROMA"),
58+
lightness=getattr(coloring_cfg, "LIGHTNESS")
59+
)
60+
raise ValueError("Invalid oklch coloring config")
61+
elif coloring_cfg.TYPE == "github":
62+
return [elem.github_color for elem in sorted_percents]
63+
64+
raise ValueError("No such coloring config")
4065

41-
sorted_percents = sorted(
42-
[(percent, name) for percent,name in zip(languages_stats.values(), languages_stats.keys())],
43-
key=lambda x: x[0],
66+
def main():
67+
sorted_langs = sorted(
68+
get_langs_data(TOKEN, USERNAME),
69+
key=lambda x: x.size,
4470
reverse=True
4571
)
4672

47-
sorted_percents = truncate(sorted_percents, SETTINGS.GENERAL_SETTINGS.TOP_K)
73+
sorted_percents = truncate(sorted_langs, SETTINGS.GENERAL_SETTINGS.TOP_K)
4874

4975
_ = Calc(
5076
outer_radius=SETTINGS.DIAGRAM_SETTINGS.OUTER_RADIUS,
5177
thickness=SETTINGS.DIAGRAM_SETTINGS.THICKNESS,
52-
percent_array=[elem[0] for elem in sorted_percents],
53-
sections_colors_array=OKLCHUtils.create_colors_array(
54-
length=len(sorted_percents),
55-
chroma=SETTINGS.GENERAL_SETTINGS.COLORING.CHROMA,
56-
lightness=SETTINGS.GENERAL_SETTINGS.COLORING.LIGHTNESS
57-
),
78+
percent_array=[elem.size for elem in sorted_percents],
79+
sections_colors_array=coloring(sorted_percents),
5880
renderer=RenderBuilder(
5981
height=SETTINGS.GENERAL_SETTINGS.PLANE.HEIGHT,
6082
width=SETTINGS.GENERAL_SETTINGS.PLANE.WIDTH,
@@ -69,7 +91,7 @@ def main():
6991
),
7092
margin_x=SETTINGS.DIAGRAM_SETTINGS.MARGIN_X,
7193
margin_y=SETTINGS.DIAGRAM_SETTINGS.MARGIN_Y,
72-
names_array=[elem[1] for elem in sorted_percents]
94+
names_array=[elem.name for elem in sorted_percents]
7395
)
7496

7597
with open(OUTPUT_FILE, "w+", encoding="utf-8-sig") as f:

src/settings/Coloring.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from dataclasses import dataclass
2+
from typing import Union
3+
4+
@dataclass
5+
class OKLCHColoring():
6+
TYPE = "oklch"
7+
CHROMA: float
8+
LIGHTNESS: float
9+
OTHER_COLOR: str
10+
11+
@dataclass
12+
class GithubColoring():
13+
TYPE = "github"
14+
OTHER_COLOR: str

src/settings/GeneralSettings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from dataclasses import dataclass
2-
from .coloring.OKLCHColoring import OKLCHColoring
2+
from .Coloring import *
33

44
@dataclass
55
class GeneralSettings:
66
TOP_K: int
77
PLANE: "PlaneSubsettings"
8-
COLORING: OKLCHColoring
8+
COLORING: OKLCHColoring | GithubColoring
99
EXCLUDED_LANGUAGES: list[str]
1010

1111
@dataclass

src/settings/Settings.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from .GeneralSettings import GeneralSettings, PlaneSubsettings
55
from .LegendSettings import LegendSettings
66
from .DiagramSettings import DiagramSettings
7-
from .coloring.OKLCHColoring import OKLCHColoring
7+
from .Coloring import *
88

99
class Settings:
1010
GENERAL_SETTINGS: GeneralSettings
@@ -18,16 +18,15 @@ def from_yaml(cls, path: str) -> Self:
1818

1919
inst = cls()
2020

21+
coloring = select_coloring(data["general"]["coloring"])
22+
2123
inst.GENERAL_SETTINGS = GeneralSettings(
2224
data["general"]["top_k"],
2325
PlaneSubsettings(
2426
data["general"]["plane"]["height"],
2527
data["general"]["plane"]["width"]
2628
),
27-
OKLCHColoring(
28-
data["general"]["coloring"]["chroma"],
29-
data["general"]["coloring"]["lightness"]
30-
),
29+
coloring,
3130
data["general"]["excluded_languages"]
3231
)
3332

@@ -45,4 +44,13 @@ def from_yaml(cls, path: str) -> Self:
4544
data["diagram"]["margin_y"]
4645
)
4746

48-
return inst
47+
return inst
48+
49+
def select_coloring(data: dict) -> OKLCHColoring | GithubColoring:
50+
t = data.get("type")
51+
if t == "oklch":
52+
return OKLCHColoring(data["chroma"], data["lightness"], data["other_color"])
53+
elif t == "github":
54+
return GithubColoring(data["other_color"])
55+
56+
raise ValueError(f"Unknown coloring type: {t!r}")

src/settings/coloring/OKLCHColoring.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)