Golf Me a Text Editor
(similar challenges: Create a simple line editor and Line editor (more text here))
I seem to have forgotten which program to use to open, read, and write text files. Something like Noteleaf or VSBro or ... You see my conundrum. But never fear! I know some people who can write one. But I'm a bit limited on space, so shorter programs would be much appreciated.
The Challenge
Create a program, in your language of choice, that can open, read, and write plaintext files. The program should take input from your language's equivalent of STDIN and write the contents of the file to STDOUT (or equivalent). The program should close the file after the user is done with it (when they type 'q'). If the file does not exist, then the program should create a new one with the given filename. Editing is done line-by-line. The user gives the line number they wish to edit, and then give the new content of that line. If there is no content given, the line should be removed. Additional specifications are listed below.
Commands
filename?>
- opens the specified file. If a file with the given name can not be found, create a new file. If the user types DELETE
, then the program should prompt with delete?>
.
delete?>
- "deletes" the file. This is done by blanking it (i.e. replacing its contents with ""
), which will suffice for this challenge. If the file can not be found, then print FILE NOT FOUND
.
line?>
- the given line number is where the text will be changed when content?>
is given. If q
is entered, then close the file and exit. If a
is entered, then append a new empty line to the end of the file. If the index is out of bounds (i.e. does not exist (the text files can be 1
or 0
based indexed)) then print Error!
.
content?>
- input that is given here will replace that of the previous content of the line given earlier (line?>
). If empty then delete that line (remove content from the line and shift all lines after it up one).
After each filename?>
and content?>
the contents of the file should be printed, as well as --
followed by the number of lines in the file.
Examples
A (rather bad) example program in Python3 can be found here.
Example deletions:
filename?>DELETE
delete?>passwords.txt
filename?>DELETE
delete?>evidence.txt
FILE NOT FOUND
Example run:
filename?>passwords.txt
password
admin
1234
********
p@$$w0rd
c0d3_1$_l!f3
--
6
line?>3
content?>12345678
password
admin
12345678
********
p@$$w0rd
c0d3_1$_l!f3
--
6
line?>4
content?>
password
admin
12345678
p@$$w0rd
c0d3_1$_l!f3
--
6
line?>a
password
admin
12345678
p@$$w0rd
c0d3_1$_l!f3
--
7
line?>9
Error!
line?>1
content?>passcode
passcode
admin
12345678
p@$$w0rd
c0d3_1$_l!f3
--
7
line?>5
content?>
password
admin
12345678
p@$$w0rd
--
6
line?>q
Remember, this is code-golf, so the shortest answer in bytes wins!