Implement a command-line editor with Python

Source: Internet
Author: User
Tags line editor

"It looks rather foolish"--preface

But I'm a whole lot of ridiculous. Put on a piece of the current

It looks comfortable, doesn't it? Even if everything is a pretence: the cursor can only be at the end, click on the left and right will exit, a line beyond 75 characters will be associated with the line number disappears, 36 lines of the program will be dashed-and can not be saved, as if a toy typewriter. But what about this? I like it! I want to put it on GitHub--but I'm afraid I'll have to roll back at any moment, so forget it.

I never care about the development, I care about philosophy, all the empty talk in my eyes are valuable. So, when I first started writing, what was I thinking?

--I was thinking, I'm going to write a command-line editor--What's that thing? --a command-line interface that changes every keystroke by one o'clock-and as the legendary MVC approach, there is a model where the only argument is a string that contains the entire text-so the main loop is the display string → receive input → update string. Every step is a huge expense (to the point of guilt), but now is not the time to optimize--and now it can be written roughly. Give the code so far: (a version of the real code)

screen = ""while True:     show_screen()    ch = getch()    update(ch)

Does not work, it is not important. In fact, we find this to be a state machine (literal)--and here the state is just the state of the screen string. The simple structure is gratifying. (I looked at the code and thought that I should add screen to the brackets in Show_screen () and update (CH))

(half a day passed)

Single-president text bug solved. (although I never wrote more than one line of code)

(Another day passed)

Rewrite the logic, still confusing. So publish it first.

(2018-1-1 on Earth, not finished)

Question generalization: "How to write a command-line program?" 】

Since it is a state machine, we can of course List a state table, each time the screen by the state Refresh (#), the action will change the state. For the command line, the only action is a keystroke, and it is limited. The action response is also discrete, one keystroke only needs to change the screen once (#). I'm not going to talk about a text editor before, a small program that fits these ideas nicely--a nine-Gongge-maze program (although I didn't have a model consciousness at the time). 9 rooms, turn left and right or forward. The state is limited and only a limited number of locations can stand. So we can list a table:

Action 1 Action 2 Action 3 ...
Status 1 Action 11 Action 12 Action 13 ......
Status 2 Action 21 Action 22 Action 23 ......
Status 3 Action 31 Action 32 Action 33 ......
...... ...... ...... ...... ......

And the display is the only corresponding to the state. So in theory, the problem is solved by filling out this form. But this is unrealistic, even the Maze program this table does not apply, because the state of the "combination explosion." In the maze procedure, the State is composed of the "room" and the "positive direction" 2 parameters, and everything is a temporary combination, and therefore some combinations are not reachable. And in the case of the editor, the combination is even infinite! In the version I'm working on now, although there is only one argument to the string, it is enough to cause an infinite state. So......

Should be seen in the same state.

I think of the "regression", more precisely, "equivalence division." Consider several or even infinitely multiple states as the same state. In the editor, all edits are in the same state. When the cursor moves around, the cursor in the middle of the time is equivalent, only at both ends of the possible special circumstances, so can be divided into 3 equivalent states ... But development is a process, we do not think so much at first, how to deal with it? It may be a good idea to be progressive. In the initial version (single-line typewriter), the logic is this:

26 letters (case) and spaces
Edit State Adds the received character to the end of the string

A little improvement is the case:

26 letters (case) and spaces BACKSPACE
Edit State Adds the received character to the end of the string Delete the last character of a string

This kind of table cannot represent the actual display, but actually shows that since it is the only function of the state, there is no need to worry about the logical problem ... Although there is no practical value, the analysis at least brings benefits.

(Over the past day, I'm going to rewrite the code ...) )

(2018-1-9 于地球,未完)

Implement a command-line editor with Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.