In this question I'm asking of one specific bit of functionality of readline:
The Readline library includes additional functions to maintain a list of previously-entered command lines, to recall and perhaps reedit those lines
Now on Windows with Visual Studio you ain't need no stinking readline. You can use fgets and arrow keys will happily recall what you typed previously. Of course you can edit these too.
On linux the very same code (fgets in a loop ) does not work like this. Up arrow is shown as ^[[A and left and right arrows also does not allow you navigating the line as shown by experiment and also described here.
My question is, what part of Windows makes the editing possible?
I think it can be either conhost.exe or how fgets, et al, are implemented. Somehow I suspect that it's the former. In any case, I'd like to know how exactly it works if it's documented anywhere, etc. For example, what other keys, apart from arrows have a special meaning and processed differently instead of being returned as par of the buffer fgets writes to.
The documentation for DOSKEY lists the special keys.
I am not aware of any documentation explaining that in 32-bit Windows this functionality is built into the console and that doskey.exe is merely an interface to it. However, it is easy to confirm that this functionality does not depend on the running console application using the C runtime library or having been launched from the command-line shell.
It is a reasonable guess that the actual code implementing this feature lives inside conhost.exe on current versions of Windows, but of course that's an implementation detail, subject to change without notice. From the programmer's perspective, all that matters is that DOSKEY functionality is present on any console window, and is available whenever the application is in cooked mode.
Note that cooked mode is the default setting. Therefore, console applications will have DOSKEY functionality unless the application specifically disables it.
Related
I have made a decent console application in C. The issue is that arrow keys print an escape sequence to the input instead of moving the cursor or navigating previous input. On Windows it works well (same behavior of command prompt).
How can I pull the same thing on Linux?
The C++ programming language is made to be a very efficient language. As such, the language itself (i.e. without including any libraries) can't really do that much, basically you can only get input through argc and argv and you can only send output through the return value.
The standard library helps a lot, giving us access things such as files, allowing us to do a lot more. As console developers it also allows us to use the standard input and the standard output through std::cin and std::cout (also the standard error stream through std::cerr but we usually hope nothing will be written to it). Since we get access to those tools through streams, to our program there's no such thing as "The second line from the top of the console", it's only a long line of letters. As such it's absolutely impossible to do this using only C++ and the standard library.
What about windows then? It can do it! Yes, but that's a windows thing, not a C++ thing (it's being a while since I used windows but I remember it sometimes causing issues with streams), third-parties windows-compatible console emulators can function exactly like Linux terminals (and I guess Linux terminals could work just like windows, I just haven't seen one yet) so we need something to guaranty us this behavior.
Thankfully, one of the main advantage of C/C++ is the sheer number of libraries available to us. An big part of C++ programs don't even use a console anymore, bypassing completely the issue.
What I'd recommend is to find a good library (I sometimes use ncurses although I think it's Linux-only and probably a bit too feature full for your usecase) and use it to control the cursor to make it do what you want
I want to write in C a command line tool with the following requirements:
Few commands (<10)
History management through the arrows key
Capability to delete what I typed previously with baskspace
Capability to add keyborad shortcuts such as ctrl+l to clear the screen
Protable across UNIX systems
I am not asking for code here, but for indications about where to start. I first made some experiments with "scanf" and it quickly become quite complicated. I then looked at ncurses, and it seems also quite low level. Is there any C libraries dedicated to this task, where would you start ?
I then looked at ncurses, and it seems also quite low level
CDK (Curses Development Kit) - high-level wrapper over ncurses. I've had successful experience with it. When you need you always can use ncurses directly.
The GNU Readline library is exactly designed for that.
I am looking for a GNU readline alternative. It comes with a lot of features but only couple of them are useful to me as explained below -
I am working on a interactive command prompt application (display prompt and accept next user command to be run). I want to implement some editing and history feature for the prompt. So when the user presses UP key it should show the last run command. Also, user should be able to navigate using arrow keys if he need to edit any typo or command switches etc.
On windows something similar already exists, if you use fgets or scanf to get the input on cmd prompt it already maintains history and also lets you edit.
Is there a good option available on linux?
This is an admirable goal I think :-)
Perhaps Linenoise, libedit/editline or tecla would fit the bill?
Of those probably libedit is the most widely used - e.g. postgreqsql client shell and various BSD utilities for Kerberos and ntp (although for the upstream sources it may not be the default line editing library for compilation due the to widespread use of libreadline on Linux). There are a couple of slightly different versions of libedit/editline as you'll see if you read some of those references and do some further research.
Cheers, and good luck with your project.
There is replxx, a BSD licensed alternative to readline.
It works in Linux, BSD, Solaris and Windows.
It has support for features you expect from interactive console programs, namely:
line editing
history
syntax highlighting
hints
UTF-8
user defined key bindings (supporting (shift/ctrl)F1 - F12)
multi-threaded print
I think the modern alternative of GNU Readline is Jupyter Notebook. The idea is that you don't create an executable that links to a line editor library. Instead, you should just provide the kernel and the users can choose their own notebook UI, either CUI, web based GUI, or even an IDE like VS Code.
I just started making my first couple of apps for iOS, which involves using XCode for the first time. I'm amazed how effortless coding becomes with the code completion in this. Everything from data types, to function names, to syntax templates, the IDE will suggest to you and write it in if you choose. It makes coding so smooth, and means you never need to struggle to remember what you named some function, or what order the arguments come in.
Until now, I've been using Eclipse to code in C, as well as a little Java. I've seen how to modify the content assist options for Java such that it will give suggestions based on almost any input, which simulates what XCode does. But attempting to do the same thing under the preferences for the C/C++ editor, I'm unable to find similar settings.
Is there anything else natively I can do to configure Eclipse so that it will match, or at least come close to, the same level of code completion in XCode, or am I into plug-in territory? To be clear, I'm referring to the C/C++ editor in Eclipse (specifically C), which I want to imitate XCode's code completion functionality.
You can go to "Content Assist" under Preferences for your language of choice, and make sure Auto Activation is turned on. Then, decrease the delay (100ms works for me), and in the Auto Activation Triggers box, put any characters you want auto activation for. For something like Xcode, that'd be: .abcdefghijklmnopqrstuvwxyz, plus caps if you want them. Voila!
I would like to know how to create console applications with better control of input and output using C. For example, how does the text-editor nano display the file contents in a certain portion of the screen and then keep a list of commands stationary on the bottom. I know you can use the win32 api to do this in Windows, but what is the Unix/Linux equivalent?
More than likely there's a ncurses interface that controls the screen drawing and placement of items on the screen in a certain position. The api is standard across the linux systems and on unix also, some may be implementation defined depending on the variant of curses used under the commercial variants of unix but otherwise some standard ncurses functionality are compatible.
Besides ncurses and depending on the task at hands you may find newt, a library for color text mode, widget-based user interfaces, a suitable alternative also. Sometimes visual results are better with newt.
If you just want to do the low level stuff, you probably want to use the termcap or terminfo library.
If you want to do it the way nano and just about every other interactive terminal app does it, you probably want to use ncurses. That way you will spend less time implementing terminal control logic, and more time on the actual app you are developing.