Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-134200: Add adaptive global alignment for help text#134308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
lnperry wants to merge7 commits intopython:main
base:main
Choose a base branch
Loading
fromlnperry:gh-134200
Open
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
fix linting
  • Loading branch information
@lnperry
lnperry committedMay 20, 2025
commit5dee87393f6adf9c49075b4b78af24abf30fdf17
46 changes: 23 additions & 23 deletionsLib/argparse.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -210,17 +210,17 @@ def _get_action_details_for_pass(self, section, current_indent_for_section_items
invocation_string = self._format_action_invocation(action_object)
# Length without color codes is needed for alignment.
invocation_length = len(self._decolor(invocation_string))

collected_details.append({
'action': action_object,
'inv_len': invocation_length,
'indent': current_indent_for_section_items,
})
elif hasattr(func_to_call, '__self__') and isinstance(func_to_call.__self__, self._Section):
sub_section_object = func_to_call.__self__

indent_for_subsection_items = current_indent_for_section_items + self._indent_increment

collected_details.extend(
self._get_action_details_for_pass(sub_section_object, indent_for_subsection_items)
)
Expand DownExpand Up@@ -282,34 +282,34 @@ def format_help(self):
formatted_heading_output_part = ""
if self.heading is not SUPPRESS and self.heading is not None:
current_section_heading_indent = ' ' * self.formatter._current_indent

try:
# This line checks if global `_` is defined.
# If `_` is not defined in any accessible scope, it raises NameError.
_
_
except NameError:
# If global `_` was not found, this line defines `_` in the global scope
# as a no-op lambda function.
_ = lambda text_to_translate: text_to_translate

# Now, call `_` directly. It is guaranteed to be defined at this point
# (either as the original gettext `_` or the no-op lambda).
# `xgettext` will correctly identify `_('%(heading)s:')` as translatable.
heading_title_text = _('%(heading)s:') % {'heading': self.heading}

theme_colors = self.formatter._theme
formatted_heading_output_part = (
f'{current_section_heading_indent}{theme_colors.heading}'
f'{heading_title_text}{theme_colors.reset}\n'
)

section_output_parts = [
'\n',
formatted_heading_output_part,
rendered_items_help,
'\n'
]

return self.formatter._join_parts(section_output_parts)

def _add_item(self, func, args):
Expand DownExpand Up@@ -404,7 +404,7 @@ def _calculate_global_help_start_column(self, all_action_details):
for detail in all_action_details:
# The column where this action's invocation string ends.
action_invocation_end_column = detail['indent'] + detail['inv_len']

# An action is "reasonable" if its help text can start on the same line,
# aligned at or before _adaptive_help_start_column, while maintaining minimum padding.
is_reasonable_to_align_with_others = \
Expand All@@ -416,13 +416,13 @@ def _calculate_global_help_start_column(self, all_action_details):
max_endpoint_of_reasonable_actions,
action_invocation_end_column
)

if max_endpoint_of_reasonable_actions > 0:
# At least one action fits the "reasonable" criteria.
# The desired alignment column is after the longest of these "reasonable" actions, plus padding.
desired_global_alignment_column = \
max_endpoint_of_reasonable_actions + min_padding_between_action_and_help

# However, this alignment should not exceed the user's preferred _adaptive_help_start_column.
return min(desired_global_alignment_column, self._adaptive_help_start_column)
else:
Expand All@@ -446,7 +446,7 @@ def format_help(self):
# This value (self._globally_calculated_help_start_col) will be used by _format_action.
self._globally_calculated_help_start_col = \
self._calculate_global_help_start_column(all_action_details)

# Second Pass: Actually format the help.
# This will recursively call _Section.format_help for all sections,
# which in turn call item formatters like _format_action, _format_text.
Expand All@@ -460,7 +460,7 @@ def format_help(self):
# Ensure the help message ends with a single newline and strip any other leading/trailing newlines.
processed_help_output = processed_help_output.strip('\n') + '\n'
return processed_help_output

return "" # Return an empty string if no help content was generated.

def _join_parts(self, part_strings):
Expand DownExpand Up@@ -691,7 +691,7 @@ def _format_action(self, action):
current_action_item_indent_str = ' ' * self._current_indent
globally_determined_help_start_col = self._globally_calculated_help_start_col
min_padding_after_action_invocation = 2
output_parts = []
output_parts = []

# Determine the maximum length the action_invocation_string (decolored) can be
# for its help text to start on the same line, aligned at globally_determined_help_start_col,
Expand All@@ -700,8 +700,8 @@ def _format_action(self, action):
globally_determined_help_start_col - self._current_indent - min_padding_after_action_invocation

action_invocation_line_part = ""
help_should_start_on_new_line = True
help_should_start_on_new_line = True

# The actual column where the first line of help text (and subsequent wrapped lines) will start.
actual_help_text_alignment_column = globally_determined_help_start_col

Expand All@@ -713,10 +713,10 @@ def _format_action(self, action):
# Calculate the number of padding spaces needed to align the help text correctly.
num_padding_spaces = globally_determined_help_start_col - \
(self._current_indent + action_invocation_len_no_color)

action_invocation_line_part = (
f"{current_action_item_indent_str}{action_invocation_string}"
f"{' ' * num_padding_spaces}"
f"{' ' * num_padding_spaces}"
)
help_should_start_on_new_line = False
else:
Expand All@@ -728,11 +728,11 @@ def _format_action(self, action):

if has_help_text:
expanded_help_text = self._expand_help(action)

# Calculate the available width for wrapping the help text.
# The help text block starts at actual_help_text_alignment_column and extends to self._width.
help_text_wrapping_width = max(self._width - actual_help_text_alignment_column, 11)

split_help_lines = self._split_lines(expanded_help_text, help_text_wrapping_width)

if split_help_lines: # Proceed only if splitting the help text yields any lines.
Expand All@@ -747,11 +747,11 @@ def _format_action(self, action):
# Append the first_help_line_content to the last part in output_parts.
# (action_invocation_line_part does not end with \n in this case).
output_parts[-1] += f"{first_help_line_content}\n"

# Add any subsequent wrapped help lines, each indented to actual_help_text_alignment_column.
for line_content in remaining_help_lines_content:
output_parts.append(f"{' ' * actual_help_text_alignment_column}{line_content}\n")

elif not output_parts[-1].endswith('\n'):
# Case: has_help_text was true (action.help existed), but it was empty after strip()
# or _split_lines returned empty. If action_invocation_line_part didn't end with \n
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp