CSCI 150 - Lab 7 - Line Editor

CSCI 150 Lab 7: Line Editor


Overview

In this lab, you will practice using lists and strings in Python by implementing a very simple text editor, which allows you to create and edit text files one line at a time. This kind of editor, called a line editor, used to be the standard way to interact with a text file in the 1970's, when communication from computers to users tended to be via printers instead of screens.
> Hi there!
> I am adding stuff
> to a file
> print
 0: Hi there!
 1: I am adding stuff
*2: to a file
> append yes I am
> print
 0: Hi there!
 1: I am adding stuff
 2: to a file
*3: yes I am
> find am
> print
 0: Hi there!
*1: I am adding stuff
 2: to a file
 3: yes I am
> replace I was adding stuff
> print
 0: Hi there!
*1: I was adding stuff
 2: to a file
 3: yes I am
> next
> print
 0: Hi there!
 1: I was adding stuff
 2: to a file
*3: yes I am
> insert This is a newly inserted line
> print
 0: Hi there!
 1: I was adding stuff
 2: to a file
*3: This is a newly inserted line
 4: yes I am
> line 0
> delete
> print
*0: I was adding stuff
 1: to a file
 2: This is a newly inserted line
 3: yes I am
> save stuff.txt
Saving as stuff.txt...
> quit

Step 1: Accumulation

Create a new Python file called line_editor.py, and put your name, date, copyright notice, etc. at the top. Also, don't forget to include the line
from typing import *
which you will need in order to write type signatures involving lists.

Define a main() function (and also call it as the very last line in your file) which performs as follows:

For example:
> Hello
> there
> lab 7
> quit
0: Hello
1: there
2: lab 7
This isn't all that exciting yet, but we will build on it to do more interesting things!

Remember to decompose your code into other functions as appropriate. For example, you probably want to make a separate function to print the document at the end.

Step 2: Commands and Printing

In addition to simply entering lines of text to append to the current document, we are going to allow the user to enter various commands. Some commands have an argument and some don't. Since the user can now choose to print whenever they want, you can remove the code that prints the document after they quit. Your editor now might look like this:
> Hello there
> print
0: Hello there
> How are you
> doing today
> print
0: Hello there
1: How are you
2: doing today
> quit

Step 3: Tracking the current line

Modify your editor so that it keeps track of the index of the current line. For example, here's how a sample interaction with your editor might look after you finish implementing this step:
> Hello there
> print
*0: Hello there
> How are you?
> print
 0: Hello there
*1: How are you?
> line zero
zero is not a valid integer.
> line 5
5 is not a valid line number.
> line 0
> print
*0: Hello there
 1: How are you?
> I'm fine thanks
> print
 0: Hello there
 1: How are you?
*2: I'm fine thanks
> quit

Step 4: Files

An editor isn't much good without being able to open and save files! In this step, you should add two commands open and save that let the user load a text document from a file and save the current document to a file, respectively. Below are two example sessions of interacting with your editor after implementing these commands. In the first session, the user enters a few lines and then saves them to a file. In the second session, the user enters a line before loading the previously saved file (notice how it completely overwrites the previous document).
> Hello there
> I am going to save this
> to a file
> print
 0: Hello there
 1: I am going to save this
*2: to a file
> save file.txt
Saving as file.txt...
> quit


> Hello
> print
*0: Hello
> open file.txt
Opening file.txt...
> print
*0: Hello there
 1: I am going to save this
 2: to a file
> quit

Step 5: Editing!

Now we can finally get to some more serious editing! You should add the following commands to your editor:

Step 6: Finding

Finally, let's provide a way for the user to quickly search through a document.

Just for fun

Save a copy of your lab in another file, just in case. Then, try opening line_editor.py itself in your editor, and making a few changes. Rejoice that we now have screens and two-dimensional editor interfaces so you normally don't have to put up with this.

What to turn in

Remember to follow the Python style guide and to run the style checker before turning in your programs!

Grading


© Brent Yorgey, Hendrix College