Live data from Hacker News

Writing an editor in less than 1000 lines of code, just for fun

antirez.com

101–110 of 146 posts

Re: Writing an editor in less than 1000 lines of code, just for fun

#101
Yay, this is delightful.

I wrote a nano-inspired TTY-based editor, too, and have used it to do virtually all of my coding work for the last couple of years. There's nothing particularly noteworthy about it, technologically, but it does exactly what I want in exactly the way I want it to, and that was enough to make the time spent feel worth my while.

There's something really satisfying about the exercise of creating one's tools. I think there is a lot of value in projects like this whether anyone else ever uses them or not, as an exercise in software craftsmanship.

Looking at this project, I am inspired to see if I could simplify my editor's codebase. I was never fully convinced that ncurses did enough good to justify its complexity; it might be interesting to adopt this author's approach instead.

Re: Writing an editor in less than 1000 lines of code, just for fun

#102

Earlier quoted context omitted.

The syntax highlighter isn't really a parser, per se . It's just a lexer / tokenizer / scanner / what-have-you, which tend to be fairly compact state machines. As an aside, I agree with your edit. As someone who started programming back in the DOS days, whenever I do Web development, I'm always floored by how much it feels like one step forward and two steps back. So many simple things just aren't simple when it come…

FastCGI/SCGI were obsoleted by having app servers simply speak HTTP; the web gateway just needs to function as a simple reverse proxy. HTTP is about as simple as it gets to parse - you can write a passable (not quite production quality, but works) parser in about 10 minutes. A lot of the difficulty in webapps is because every webapp is inherently a distributed system, which are always hard. Single-page apps with no c…

Speaking HTTP is worse in almost every way to using CGI: harder to implement, loses meta information (suppose you put an application at /app/ on your server... CGI handles it well, HTTP doesn't unless you do some extension since your server will see GET / anyway) and is harder to centrally log too.

I think app servers speak HTTP more because it is kinda convenient for developers to run the local server without setting up the http server.. convenience rather than superiority.

Re: Writing an editor in less than 1000 lines of code, just for fun

#103

Can anyone explain how to do a full-screen UI using only VT100 codes? If I had to write a small editor, I'd probably reach for something like Termbox for display.

First, switch it to alternative screen mode... printf("\033?1047h"); then clear and move and such with other similar prints. See the list here : http://invisible-island.net/xterm/ctlseqs/ctlseqs.html

It really is pretty simple. The big benefit of ncurses is if you want to target terminals other than xterm compatible ones, and they are so rare nowadays that you can really just ignore them....

Input is quite a pain though (however, ncurses sometimes get it wrong too), but the same thing applies, you just need to switch to raw mode with tcsetattr and then have some kind of select() loop or something to watch for escape sequences then translate them into interesting key presses.

Mouse events are the same btw: you write out a sequence to turn them on, then the terminal sends them as sequences to stdin.

I wrote a terminal library myself for the D programming language (and a terminal emulator too!), it is kinda ugly code but it isn't hard.

Re: Writing an editor in less than 1000 lines of code, just for fun

#104
post #23

Earlier quoted context omitted.

If you ever happen to bork your kernel badly enough to prevent it from booting, you'll find a classic VI pretty quickly, and probably be grateful for it.

Well, not really. My system doesn't have vi installed. Vim, of course, any time of the day. Stripped Vim (vim-tiny, as Debian calls it), sure, just after installation. But vi -- no, I don't have it installed. For quite a long time Linux didn't have vi even ported . There were several clones, like Elvis and nvi (and Vim, of course). Few years ago I learned that somebody took the effort and actually ported traditional…

You are being needlessly pedantic. The point is that typing "vi" into a terminal runs some application that implements a "vi-ish" interface. Whether it is ported vi or vim or vim-tiny has some effect on how nice that interface is to work with, but I would argue that most vim users would be able to get their configuration fixed or whatever

Re: Writing an editor in less than 1000 lines of code, just for fun

#105

Can anyone explain how to do a full-screen UI using only VT100 codes? If I had to write a small editor, I'd probably reach for something like Termbox for display.

First, switch it to alternative screen mode... printf("\033?1047h"); then clear and move and such with other similar prints. See the list here : http://invisible-island.net/xterm/ctlseqs/ctlseqs.html It really is pretty simple. The big benefit of ncurses is if you want to target terminals other than xterm compatible ones, and they are so rare nowadays that you can really just ignore them.... Input is quite a pain tho…

I wonder if these codes work in cmd.exe now that Windows 10 TH2 supports (some of) the features.

Re: Writing an editor in less than 1000 lines of code, just for fun

#106
post #91

@Antirez please please please let this thing have a future: there is a great need for a modern, simple and usabile CLI editor.

Try vis: https://github.com/martanne/vis

I find it funny that a person named _acme suggests vis :)

Re: Writing an editor in less than 1000 lines of code, just for fun

#107

Can anyone explain how to do a full-screen UI using only VT100 codes? If I had to write a small editor, I'd probably reach for something like Termbox for display.

First, switch it to alternative screen mode... printf("\033?1047h"); then clear and move and such with other similar prints. See the list here : http://invisible-island.net/xterm/ctlseqs/ctlseqs.html It really is pretty simple. The big benefit of ncurses is if you want to target terminals other than xterm compatible ones, and they are so rare nowadays that you can really just ignore them.... Input is quite a pain tho…

Thanks for the info, very useful info; I was most interested to learn that ncurses provides compatibility for terminals that are rare.

Re: Writing an editor in less than 1000 lines of code, just for fun

#108
Off topic, but my biggest takeaway was that I could aspire to be someone who isn't a twenty something, with a family, with superb work life balance, not living in the valley, sitting in a garden without three 27 inch monitors, not using the latest programming language or fad and still writing beautiful code that a lot of the world runs on.

Re: Writing an editor in less than 1000 lines of code, just for fun

#109
post #6

Earlier quoted context omitted.

Presumably h, j, k, l will always get you around in vi?

Yeah but that is so much less usable. Quick, which one is up?

The one under my middle finger on my right hand.

I don't even have to think, it's automatic. Likewise with the other directions.

Re: Writing an editor in less than 1000 lines of code, just for fun

#110
post #92

Earlier quoted context omitted.

He mentioned using cloc to get that metric, so not including comments, formatting, etc.

I think you missed the point: the point is that the correct English usage is "fewer than" when discussing quantifiable items (e.g., lines of code). The title should be "...an editor in fewer than 1000 lines of code."

Thank you for clarifying. I thought for a second it could have been a grammatical nitpick but discounted that since this thread is re: an Antirez post. I personally would be very sad if his writing style became more forced and began to feel like excerpts from The Elements of Style.
Post reply on HN