This book is new. If you'd like to go through it, then join the Learn Code Forum to get help while you attempt it. I'm also looking for feedback on the difficulty of the projects. If you don't like public forums, then email firstname.lastname@example.org to talk privately.
Exercise 50: vi
I am going to prison for this exercise. You have a module that implements features used in both ed and sed. Obviously the next step is to implement the most hated and useful text editor in the history of the world: vi. If you knew Lisp you could implement Emacs, but nobody has time to create a whole new operating system pretending to be a text editor. Life is too short to hold down three keys and hit tab all day.
The goal of this exercise is not to do a dead accurate copy of vi. That's a very large project, but if you feel like trying it then go for it. Your goal in this project is to do one more reuse of your ed module and to play around with the Python curses module. The curses module lets you do old-school text terminal window and graphic manipulation. Actually "graphics" should be in quotes because there are very little actual graphics in curses.
You'll use curses to create a baby vi implementation that lets you open files, run ed and sed commands using your module, and use curses to display them to the terminal screen. You'll also find out that attempting to automatically test this is going to be very difficult. You'll get extra points if you can figure out how to do a fake curses testing framework, but that's going to require some magical wizard skills with a Unix pty system (I think).
The better way to make this testable is to put as much of your vi into Python modules as possible so that you can test the code without having to run the curses screen system. When I say "modules" I don't really mean a fully baked Python module that you install with pip like you did with the ed module. I mean modules right in the code for the vi and then imported into your project.
The way to think of this project is to separate the code that controls the view (curses) from the rest of the code so that you can plug in your own fake view for the testing. That then leaves a small amount of functionality that you can test manually by actually running your vi.
We will not be implementing all of vi. I need to really make this clear because actual vi is old and very complex, so doing a full master copy of it will take a long time. You are really only doing the following:
- Take your ed module.
- Create a curses UI to it.
- Make it work on multiple files.
That's more or less all you're doing, so the first thing you should focus on is how curses works. Read the curses documentation to find out how it works and write as many test hacks as necessary to understand it.
Once you have a handle on curses you'll then need to learn how to use vi. I've included a vi crash course video for this exercise that you can watch, and there are a few vi cheat sheets online to reference. I suggest you watch my tutorial to vi and try to use the real vi to edit code during this session. You'll actually have a good idea of how vi works from your implementation of ed and sed. In theory, vi is just "visual ed," so you're mostly just giving ed a better UI.
- How did the Finite State Machine from your ed implementation match up with the one you used in this vi implementation? Assuming you used that design.
- How hard would it be to do a GUI version of this rather than curses. I don't recommend you do that, but research it and see what it'd take.