| 427 | return len(self.get_reader().history) |
| 428 | |
| 429 | def read_history_file(self, filename: str = gethistoryfile()) -> None: |
| 430 | # multiline extension (really a hack) for the end of lines that |
| 431 | # are actually continuations inside a single multiline_input() |
| 432 | # history item: we use \r\n instead of just \n. If the history |
| 433 | # file is passed to GNU readline, the extra \r are just ignored. |
| 434 | history = self.get_reader().history |
| 435 | |
| 436 | with open(os.path.expanduser(filename), 'rb') as f: |
| 437 | is_editline = f.readline().startswith(b"_HiStOrY_V2_") |
| 438 | if is_editline: |
| 439 | encoding = "unicode-escape" |
| 440 | else: |
| 441 | f.seek(0) |
| 442 | encoding = "utf-8" |
| 443 | |
| 444 | lines = [line.decode(encoding, errors='replace') for line in f.read().split(b'\n')] |
| 445 | buffer = [] |
| 446 | for line in lines: |
| 447 | if line.endswith("\r"): |
| 448 | buffer.append(line+'\n') |
| 449 | else: |
| 450 | line = self._histline(line) |
| 451 | if buffer: |
| 452 | line = self._histline("".join(buffer).replace("\r", "") + line) |
| 453 | del buffer[:] |
| 454 | if line: |
| 455 | history.append(line) |
| 456 | self.set_history_length(self.get_current_history_length()) |
| 457 | |
| 458 | def write_history_file(self, filename: str = gethistoryfile()) -> None: |
| 459 | maxlength = self.saved_history_length |