@@ -162,6 +162,39 @@ def decodable(seq: bytes, encoding: str) -> bool:
162162 return True
163163
164164
165+ def _key_name (seq : bytes , encoding : str , keynames : Keynames ) -> str :
166+ if keynames == Keynames .CURSES :
167+ # may not be here (and still not decodable) curses names incomplete
168+ if seq in CURSES_NAMES :
169+ return CURSES_NAMES [seq ]
170+
171+ # Otherwise, there's no special curses name for this
172+ try :
173+ # for normal decodable text or a special curtsies sequence with bytes that can be decoded
174+ return seq .decode (encoding )
175+ except UnicodeDecodeError :
176+ # this sequence can't be decoded with this encoding, so we need to represent the bytes
177+ if len (seq ) == 1 :
178+ return "x%02X" % ord (seq )
179+ # TODO figure out a better thing to return here
180+ else :
181+ raise NotImplementedError (
182+ "are multibyte unnameable sequences possible?"
183+ )
184+ return "bytes: " + "-" .join (
185+ "x%02X" % ord (seq [i : i + 1 ]) for i in range (len (seq ))
186+ )
187+ # TODO if this isn't possible, return multiple meta keys as a paste event if paste events enabled
188+ elif keynames == Keynames .CURTSIES :
189+ if seq in CURTSIES_NAMES :
190+ return CURTSIES_NAMES [seq ]
191+ # assumes that curtsies names are a subset of curses ones
192+ return seq .decode (encoding )
193+ else :
194+ assert keynames == Keynames .BYTES
195+ return seq # type: ignore
196+
197+
165198def get_key (
166199 bytes_ : Sequence [bytes ],
167200 encoding : str ,
@@ -206,47 +239,14 @@ def get_key(
206239 if len (seq ) > MAX_KEYPRESS_SIZE :
207240 raise ValueError ("unable to decode bytes %r" % seq )
208241
209- def key_name () -> str :
210- if keynames == Keynames .CURSES :
211- # may not be here (and still not decodable) curses names incomplete
212- if seq in CURSES_NAMES :
213- return CURSES_NAMES [seq ]
214-
215- # Otherwise, there's no special curses name for this
216- try :
217- # for normal decodable text or a special curtsies sequence with bytes that can be decoded
218- return seq .decode (encoding )
219- except UnicodeDecodeError :
220- # this sequence can't be decoded with this encoding, so we need to represent the bytes
221- if len (seq ) == 1 :
222- return "x%02X" % ord (seq )
223- # TODO figure out a better thing to return here
224- else :
225- raise NotImplementedError (
226- "are multibyte unnameable sequences possible?"
227- )
228- return "bytes: " + "-" .join (
229- "x%02X" % ord (seq [i : i + 1 ]) for i in range (len (seq ))
230- )
231- # TODO if this isn't possible, return multiple meta keys as a paste event if paste events enabled
232- elif keynames == Keynames .CURTSIES :
233- if seq in CURTSIES_NAMES :
234- return CURTSIES_NAMES [seq ]
235- return seq .decode (
236- encoding
237- ) # assumes that curtsies names are a subset of curses ones
238- else :
239- assert keynames == Keynames .BYTES
240- return seq # type: ignore
241-
242242 key_known = seq in CURTSIES_NAMES or seq in CURSES_NAMES or decodable (seq , encoding )
243243
244244 if full and key_known :
245- return key_name ( )
245+ return _key_name ( seq , encoding , keynames )
246246 elif seq in KEYMAP_PREFIXES or could_be_unfinished_char (seq , encoding ):
247247 return None # need more input to make up a full keypress
248248 elif key_known :
249- return key_name ( )
249+ return _key_name ( seq , encoding , keynames )
250250 else :
251251 # this will raise a unicode error (they're annoying to raise ourselves)
252252 seq .decode (encoding )
0 commit comments