Skip to content

Commit

Permalink
Only include the first line of descriptions in help pages (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
FasterSpeeding authored Aug 28, 2023
1 parent 6c94f50 commit 77387b1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 6 additions & 4 deletions tanchan/_internal/localisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,18 @@ def __init__(

self._localise_id = f"{_TYPE_TO_STR[cmd_type]}:{name}:{field_type}"

def to_string(self) -> str:
def to_hashable(self) -> str:
"""Make a string representation of this localised value.
This is used for ensure synced states.
This is used for ensuring pagination states match.
"""
if not self.localised_values:
return self.default_value
return self.default_value.split("\n", 1)[0]

else:
return f"{self.default_value};{sorted(self.localised_values.items())!r}"
# Only care about the first line for pagination.
descriptions = [(key, value.split("\n", 1)[0]) for key, value in self.localised_values.items()]
return f"{self.default_value};{sorted(descriptions)!r}"

def localise(self, locale: hikari.Locale, localiser: typing.Optional[tanjun.dependencies.AbstractLocaliser]) -> str:
"""Get the localised value for a context.
Expand Down
8 changes: 5 additions & 3 deletions tanchan/components/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,13 @@ def _localise(
self, locale: typing.Optional[hikari.Locale], localiser: typing.Optional[tanjun.dependencies.AbstractLocaliser]
) -> tuple[str, str]:
if locale is None:
description = "\n".join(f"{name}: {field.default_value}" for name, field in self._fields)
description = "\n".join(f"{name}: " + field.default_value.split("\n", 1)[0] for name, field in self._fields)
title = f"{self._category_name.default_value} commands"
return title, description

description = "\n".join(f"{name}: {field.localise(locale, localiser)}" for name, field in self._fields)
description = "\n".join(
f"{name}: " + field.localise(locale, localiser).split("\n", 1)[0] for name, field in self._fields
)
title = self._category_name.localise(locale, localiser)
return title, description

Expand Down Expand Up @@ -410,7 +412,7 @@ def reload(self, client: tanjun.abc.Client, help_config: config.HelpConfig, /) -
page_number += 1
pages.append(_Page(self, page_number, cateory, commands[10 * index : 10 * (index + 1)]))

items_repr = ((" ".join(k), v.to_string()) for k, v in descriptions.items())
items_repr = ((" ".join(k), v.to_hashable()) for k, v in descriptions.items())
items_repr = ",".join(map(":".join, sorted(items_repr, key=lambda v: v[0]))).encode()
self._descriptions = descriptions
self._hash = "md5-" + hashlib.md5(items_repr, usedforsecurity=False).hexdigest()
Expand Down

0 comments on commit 77387b1

Please sign in to comment.