Skip to content

Commit

Permalink
Merge pull request #122 from joe733/workshop
Browse files Browse the repository at this point in the history
fix: adds granularity to show more languages
  • Loading branch information
yozachar authored May 5, 2023
2 parents 59f35b0 + fbc9196 commit 080a8c9
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 68 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ There are many flags that you can tweak to suit your taste!
| `SHOW_TOTAL` | `false` | `false`, `true` | Show total coding time |
| `SHOW_MASKED_TIME` | `false` | `false`, `true` | Adds total coding time including unclassified languages (overrides: `SHOW_TOTAL`) |
| `LANG_COUNT` | `5` | Any reasonable number | Number of languages to be displayed |
| `STOP_AT_OTHER` | `false` | `false`, `true` | Stop when language marked as `Other` is retrieved (overrides: `LANG_COUNT`) |

# Example

Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ inputs:
description: "Displays total coding time including unclassified languages"
default: "false"
required: false

STOP_AT_OTHER:
description: "Stop data retrieval when language marked 'Other' is reached"
default: "false"
required: false

runs:
using: "docker"
Expand Down
34 changes: 27 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class WakaInput:
show_total_time: str | bool = os.getenv('INPUT_SHOW_TOTAL') or False
show_masked_time: str | bool = os.getenv('INPUT_SHOW_MASKED_TIME') or False
language_count: str | int = os.getenv('INPUT_LANG_COUNT') or 5
stop_at_other: str | bool = os.getenv('INPUT_STOP_AT_OTHER') or False

def validate_input(self):
"""
Expand All @@ -178,6 +179,7 @@ def validate_input(self):
self.show_time = strtobool(self.show_time)
self.show_total_time = strtobool(self.show_total_time)
self.show_masked_time = strtobool(self.show_masked_time)
self.stop_at_other = strtobool(self.stop_at_other)
except (ValueError, AttributeError) as err:
logger.error(err)
return False
Expand All @@ -202,7 +204,11 @@ def validate_input(self):
logger.debug('Using default time range: last_7_days')
self.time_range = 'last_7_days'

if not str(self.language_count).isnumeric():
try:
self.language_count = int(self.language_count)
if self.language_count < -1:
raise ValueError
except ValueError:
logger.warning('Invalid language count')
logger.debug('Using default language count: 5')
self.language_count = 5
Expand Down Expand Up @@ -258,7 +264,7 @@ def make_graph(block_style: str, percent: float, gr_len: int, lg_nm: str = '', /
return graph_bar


def prep_content(stats: dict[str, Any], language_count: int = 5, /):
def prep_content(stats: dict[str, Any], language_count: int = 5, stop_at_other: bool = False, /):
"""
WakaReadme Prepare Markdown
---------------------------
Expand Down Expand Up @@ -290,14 +296,21 @@ def prep_content(stats: dict[str, Any], language_count: int = 5, /):
if not (lang_info := stats.get('languages')):
logger.debug('The API data seems to be empty, please wait for a day')
contents += 'No activity tracked'
return contents
return contents.rstrip('\n')

# make lang content
pad_len = len(
# comment if it feels way computationally expensive
max((str(lng['name']) for lng in lang_info), key=len)
# and then don't for get to set pad_len to say 13 :)
)
if language_count == 0 and not stop_at_other:
logger.debug(
'Set INPUT_LANG_COUNT to -1 to retrieve all language'
+ ' or specify a positive number (ie. above 0)'
)
return contents.rstrip('\n')

for idx, lang in enumerate(lang_info):
lang_name = str(lang['name'])
# >>> add languages to filter here <<<
Expand All @@ -312,7 +325,11 @@ def prep_content(stats: dict[str, Any], language_count: int = 5, /):
f'{lang_time: <16}{lang_bar} ' +
f'{lang_ratio:.2f}'.zfill(5) + ' %\n'
)
if idx >= language_count or lang_name == 'Other':
if language_count == -1:
continue
if stop_at_other and (lang_name == 'Other'):
break
if idx+1 >= language_count > 0: # idx starts at 0
break

logger.debug('Contents were made\n')
Expand Down Expand Up @@ -381,17 +398,20 @@ def churn(old_readme: str, /):
f'Can\'t find `{wk_i.waka_block_pattern}` pattern in readme'
)
return None
# getting content
# getting contents
if not (waka_stats := fetch_stats()):
logger.error('Unable to fetch data, please rerun workflow\n')
sys.exit(1)
# processing content
# preparing contents
try:
generated_content = prep_content(waka_stats, int(wk_i.language_count))
generated_content = prep_content(waka_stats, int(
wk_i.language_count), bool(wk_i.stop_at_other)
)
except (AttributeError, KeyError, ValueError) as err:
logger.error(f'Unable to read API data | {err}\n')
sys.exit(1)
print(generated_content, '\n', sep='')
# substituting old contents
new_readme = re.sub(
pattern=wk_i.waka_block_pattern,
repl=f'{wk_i.start_comment}\n\n```text\n{generated_content}\n```\n\n{wk_i.end_comment}',
Expand Down
Loading

0 comments on commit 080a8c9

Please sign in to comment.