Ill assume youre speaking of the encapsulation i did and not the slapchopped slav jank below and to that i say thank you
the slavjank isn't that terrible as a piece of code except the fact that it looks like it hogs a whole cpu core
also what you call "encapsulation" is more often called "refactoring into a function"
your file helper functions are fairly ok, but i have one nitpick with them: they have too much commenting. that's not actually a bad problem to have, but often beginners will do stuff like
x = x + 4 # add 4 to x and store it in x
when making comments. you always have to ask yourself: is this comment just verbosely explaining what the code is already explaining? for example, when you say
for line in file.read().splitlines():
do you really need to explain what a for loop is and how it reads the file and splits the lines into a list? is
# we return the buffer so that other functions can use it.
really helpful?
you should always comment your code to explain why you're doing things instead of what you're doing. here's an example: you should replace the
#this is our bufer array
comment. (seriously... what else could this line possibly be, other than our buffer array? somebody else's? this is not a very helpful comment!) i recommend replacing it with something like
# array containing arrays of numbers corresponding to character codes
so you actually know what your buffer is, instead of just saying "dis my bufer array it is very nice". but that's not even the best place to put that; you could instead remove the comment entirely and put that information in the function's docstring, where the function is supposed to be documented. certain really smart text editors can do stuff like pop up the docstring on hover, but they can't do that for random comments unless they are told to open the source code of the function
also you could probably replace the
buffer.append([])
with
return [[]] # return a single blank line
and move that return statement at the bottom up into the try block. makes it less tangly.
otherwise, your plan to refactor everything into functions is admirable. if you feel really brave, you should try turning the cursor and the logical window for scrolling into classes. classes allow you to make objects, which you already use a lot of. classes and objects can make your code a lot cleaner and easier to understand, if you use them right. i assume
cursor.getrow()
looks better to you than some random variable called
current_r
? it will probably make your
handle_keypress(buffer, screen.getch())
function a lot better. and if you make enough stuff into classes and separate the state enough, you could probably do epic things like multiple files being edited in little windows in your terminal window, since you can make as many objects you want from a class you make. you could end up with 3 EditorWindow objects and they all get their own buffer, and when you switch around between them you switch between their own cursor objects! then your main function is just a skeleton that reads keypresses for the sole purpose of sending them off to the appropriate windows
a good first class candidate might be to make it so your buffer is attached to the file name it's supposed to save to. one variable to deal with is a lot better than two. you can then change the opening and saving functions into functions like
buffer = Buffer() buffer.load_from_file(...)
word to the wise: don't feel bad if you end up rewriting the whole thing a few times
I've decided to buy a serial to usb, so I can at least have something to print to
all hail printf(), the universal debugger