Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit afbbdd6

Browse files
timhoffmanntzer
authored andcommitted
Fix short formatting for display
1 parent 37d29df commit afbbdd6

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

lib/matplotlib/backend_tools.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,13 +1026,24 @@ class ToolHelpBase(ToolBase):
10261026
default_keymap = rcParams['keymap.help']
10271027
image = 'help.png'
10281028

1029+
1030+
@staticmethod
1031+
def format_shortcut(keysequence):
1032+
"""
1033+
Converts a shortcut string from the notation used in rc config to the
1034+
standard notation for displaying shortcuts, e.g. 'ctrl+a' -> 'Ctrl+A'.
1035+
"""
1036+
def repl(match):
1037+
s = match.group(0)
1038+
return 'Shift+' + s if len(
1039+
s) == 1 and s.isupper() else s.capitalize()
1040+
if len(keysequence) == 1:
1041+
return keysequence # do not modify single characters
1042+
return re.sub(r"\w{2,}|(?<=\+)\w", repl, keysequence)
1043+
10291044
def _format_tool_keymap(self, name):
10301045
keymaps = self.toolmanager.get_tool_keymap(name)
1031-
# Capitalize "ctrl+a" -> "Ctrl+A" but leave "a" as is.
1032-
return ", ".join(re.sub(r"\w{2,}|(?<=\+)\w",
1033-
lambda m: m.group(0).capitalize(),
1034-
keymap)
1035-
for keymap in keymaps)
1046+
return ", ".join(self.format_shortcut(keymap) for keymap in keymaps)
10361047

10371048
def _get_help_text(self):
10381049
entries = []
@@ -1053,7 +1064,8 @@ def _get_help_html(self):
10531064
continue
10541065
rows.append(fmt.format(
10551066
name, self._format_tool_keymap(name), tool.description))
1056-
return ("<table><thead>" + rows[0] + "</thead>"
1067+
return ("<style>td {padding: 0px 4px}</style>"
1068+
"<table><thead>" + rows[0] + "</thead>"
10571069
"<tbody>".join(rows[1:]) + "</tbody></table>")
10581070

10591071

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
3+
from matplotlib.backend_tools import ToolHelpBase
4+
5+
6+
@pytest.mark.parametrize('rc_shortcut,expected', [
7+
('home', 'Home'),
8+
('backspace', 'Backspace'),
9+
('f1', 'F1'),
10+
('ctrl+a', 'Ctrl+A'),
11+
('ctrl+A', 'Ctrl+Shift+A'),
12+
('a', 'a'),
13+
('A', 'A'),
14+
('ctrl+shift+f1', 'Ctrl+Shift+F1'),
15+
('1', '1'),
16+
('cmd+p', 'Cmd+P'),
17+
('cmd+1', 'Cmd+1'),
18+
])
19+
def test_format_shortcut(rc_shortcut, expected):
20+
assert ToolHelpBase.format_shortcut(rc_shortcut) == expected

0 commit comments

Comments
 (0)