简体   繁体   中英

How can I code command line interface?

I'm a GUI developer. I have used many of GUI frameworks including WindowsForms, Flash, HTML, AppKit, UIKit. I can say I know how to code GUI.

Suddenly I got an interest about command-line-interface. Like VI or Emacs. Because I started programming with GUI, I don't have any experience or knowledge about CLI. Of course, I used VI, and unix shells, however I don't know how to implement them. And I want to know it.

For GUIs, most basic construct for all graphical stuff is lowest-level drawing method. For software implementations, it's CPU based drawing method. For hardware accelerated implementations, it's OpenGL based method. All GUI stuffs are built on top of them. What can I expect for CLI? How can I draw text on console and blink the caret?

I just need first step. Like a function name or library name. For example, something like setPixel() function. Any help will be appreciated.

PS I prefer General Unix system. Primarily POSIX. And currently I'm using Mac OS X. Of course language is C.

If you want to dive head-on, learn ncurses and type away. This is THE way to write console gfx apps.

If you want to learn the basics, ANSI escape codes . This is the mechanics of how ncurses interfaces with the terminal doing its magics. It's quite manageable by itself and an interesting subject to learn - you just print out an escape sequence and the cursor moves to specific point on screen or whatever you print out is printed in inverse video and so on. And from there, it's crude semigraphics using upper half of 256 ASCII codes and so on.

Look into http://en.wikipedia.org/wiki/Readline because it gives you things like command line history, tab completion etc...

Also ncurse http://en.wikipedia.org/wiki/Ncurses for console based dialog screens.

In which language are you experimenting ? For which os ? ncurses, as already stated, is a good lib for unix like systems, but if you're under ms windows, I guess things will get a little bit different.

Another thing to keep in mind is that depending on your OS, you may have to learn & understand termcap entries or inherited DOS legacy.

For *nix you have to check some graph library if you want to experiment with pixels and alikes. If it's just normal terminal interface you need, ncurses IS the starting point (until you want to do it all by hands with termcap and escapes).

There're plenty of frameworks for developing text/terminal based UI, more or less they give everything you need. But if you want to trace a line from pixel (1,1) to pixel (10, 24), I think you should focus on graphics libs. (OpenGL in *nix env is easily obtained via MESA library).

On win32, well, DirectX, but they're a reall hassle.

C有一个库,可以更好地操作终端: ncurses

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM