Prompt for a filename and write the current contents of the stdout buffer to disk.
(self)
| 884 | return "\n".join(process()) |
| 885 | |
| 886 | def write2file(self) -> None: |
| 887 | """Prompt for a filename and write the current contents of the stdout |
| 888 | buffer to disk.""" |
| 889 | |
| 890 | try: |
| 891 | fn = self.interact.file_prompt(_("Save to file (Esc to cancel): ")) |
| 892 | if not fn: |
| 893 | self.interact.notify(_("Save cancelled.")) |
| 894 | return |
| 895 | except ValueError: |
| 896 | self.interact.notify(_("Save cancelled.")) |
| 897 | return |
| 898 | |
| 899 | path = Path(fn).expanduser() |
| 900 | if path.suffix != ".py" and self.config.save_append_py: |
| 901 | # fn.with_suffix(".py") does not append if fn has a non-empty suffix |
| 902 | path = Path(f"{path}.py") |
| 903 | |
| 904 | mode = "w" |
| 905 | if path.exists(): |
| 906 | new_mode = self.interact.file_prompt( |
| 907 | _( |
| 908 | "%s already exists. Do you want to (c)ancel, (o)verwrite or (a)ppend? " |
| 909 | ) |
| 910 | % (path,) |
| 911 | ) |
| 912 | if new_mode in ("o", "overwrite", _("overwrite")): |
| 913 | mode = "w" |
| 914 | elif new_mode in ("a", "append", _("append")): |
| 915 | mode = "a" |
| 916 | else: |
| 917 | self.interact.notify(_("Save cancelled.")) |
| 918 | return |
| 919 | |
| 920 | stdout_text = self.get_session_formatted_for_file() |
| 921 | |
| 922 | try: |
| 923 | with open(path, mode) as f: |
| 924 | f.write(stdout_text) |
| 925 | except OSError as e: |
| 926 | self.interact.notify(_("Error writing file '%s': %s") % (path, e)) |
| 927 | else: |
| 928 | self.interact.notify(_("Saved to %s.") % (path,)) |
| 929 | |
| 930 | def copy2clipboard(self) -> None: |
| 931 | """Copy current content to clipboard.""" |
nothing calls this directly
no test coverage detected