allow saving a file with a new name

This commit is contained in:
Anthony Sottile
2021-09-11 14:02:02 -04:00
parent 43a650925b
commit c49e722498
2 changed files with 16 additions and 4 deletions

View File

@@ -494,17 +494,17 @@ class Screen:
else:
self.file.filename = filename
if os.path.isfile(self.file.filename):
if not os.path.isfile(self.file.filename):
sha256: Optional[str] = None
else:
with open(self.file.filename, encoding='UTF-8', newline='') as f:
*_, sha256 = get_lines(f)
else:
sha256 = hashlib.sha256(b'').hexdigest()
contents = self.file.nl.join(self.file.buf)
sha256_to_save = hashlib.sha256(contents.encode()).hexdigest()
# the file on disk is the same as when we opened it
if sha256 not in (self.file.sha256, sha256_to_save):
if sha256 not in (None, self.file.sha256, sha256_to_save):
self.status.update('(file changed on disk, not implemented)')
return PromptResult.CANCELLED

View File

@@ -162,6 +162,18 @@ def test_save_via_ctrl_o_set_filename(run, tmpdir):
assert f.read() == 'hello world\n'
def test_save_via_ctrl_o_new_filename(run, tmpdir):
f = tmpdir.join('f')
f.write('wat\n')
with run(str(f)) as h, and_exit(h):
h.press('^O')
h.await_text('enter filename: ')
h.press_and_enter('new')
h.await_text('saved! (1 line written)')
assert f.read() == 'wat\n'
assert tmpdir.join('fnew').read() == 'wat\n'
@pytest.mark.parametrize('key', ('^C', 'Enter'))
def test_save_via_ctrl_o_cancelled(run, key):
with run() as h, and_exit(h):