from pexpect import replwrap
from fortrace.utility.console_applications.console_application import KeyCodes
from fortrace.utility.console_applications.text_editor.text_editor import (
GenericConsoleTextEditor,
)
[docs]
class Nano(GenericConsoleTextEditor):
"""Nano text editor."""
def __init__(self, pty: replwrap.REPLWrapper):
super().__init__(pty, "nano")
[docs]
def save_file(self):
self._virsh.sendcontrol("s")
self._document_saved = True
[docs]
def close(self):
if not self._document_saved:
self.save_file()
self._virsh.sendcontrol("x")
self._pty.prompt = self._parent_prompt.prompt
self._pty.continuation_prompt = self._parent_prompt.continuation_prompt
self._virsh.expect(self._pty.prompt) # skip to next prompt
[docs]
def replace(self, old: str, new: str, count: int = -1):
self.send_key_combination(KeyCodes.ALT + KeyCodes.R) # start replacing session
self._virsh.sendline(old)
self._virsh.sendline(new)
if count != -1:
for _ in range(count):
self._virsh.send("y")
else:
self._virsh.send("a")
[docs]
def go_to_line(self, line_num: int):
raise NotImplementedError(
"This method does not work currently, as pty will timeout after nano is closed"
)
self.send_key_combination(KeyCodes.ALT + KeyCodes.G)
self._virsh.sendline(str(line_num))
[docs]
def delete_current_line(self):
self.send_key_combination(KeyCodes.ALT + KeyCodes.DEL)