11

I am wondering whether there are libraries in .NET that make it easy to write console user interfaces. For instance, imagine a WinForms application, where the user can:

  • Navigate the menus
  • Respond to dialogs

And the application in return displays several lines of text.

It can be shown, that the GUI in such an application can be mapped to the respective CUI without any problem.

So, if one has to stick to console, then are there any .NET tools to let write such CUI easily?

EDIT1

Let me define a constraint, which should help folks to grasp the idea of CUI. Imagine a machine, to which you can open a remote console, but not RDS. So, running a GUI application remotely on that machine is out of the question, because it will be unable to open any window. However, it is possible to have a remote console, leaving us with two possible flavors of CUI:

  • Character based graphics, like old supermarket terminals
  • Completely text based.

The first options allows to place characters at arbitrary positions on the console window. ncurses is the low level library for *nix systems that allows to do such things. A CUI created in such a way can be pretty expressive and convenient, but for the sake of our discussion let me rule this option out as well, because a remote console is unlikely to support the ability to move around the console window.

So, this leaves us with the text based CUI, the one created by means of printf and scanf only (and the likes).

EDIT2

Another clarification: I mention Windows Forms in the question as an example of simple User Interface, which has nothing special that could not be translated to text based console UI. This is only to illustrate that simple GUI elements like menus and modal dialogues can be modeled in console without resorting to windows.

halfer
  • 19,824
  • 17
  • 99
  • 186
mark
  • 59,016
  • 79
  • 296
  • 580
  • Any mildly complex WinForms application outside of Hello World would be a nightmare to navigate via the CLI. Not sure if this is a hypothetical question or an idea you want to implement...if it is an implementation idea I'd look for other alternatives. – Aaron McIver Feb 22 '11 at 14:43
  • 1
    It's not clear what you're trying to do. Are you trying to write a character-based GUI, a command-line app that supports menus, or something else entirely? – Gabe Feb 22 '11 at 18:21
  • i'm also unclear on what you're after. My first reading thought you wanted WinForms controls to have an alternate ability to render themselves as text. – Ian Boyd Feb 22 '11 at 21:52
  • Do you want a prompt-based interface? (something like the DOS/Command Prompt) – xanatos Feb 23 '11 at 13:10
  • 1
    `For instance, imagine a WinForms application, where the user can: Navigate the menus Respond to dialogs And the application in return displays several lines of text.` **and** `but for the sake of our discussion let me rule this option out as well,`. You are funny, you know? **Imagine this**... **Now forget it!** :-) – xanatos Feb 23 '11 at 13:27
  • @xanatos Is there a concrete question that you want to ask me? – mark Feb 23 '11 at 14:53
  • @mark I put a question ("Do you want a prompt-based interface") and I put a sarcastic (but honest) comment on the fact that you ask a full screen interface and ten lines below you say that you don't truly need a full screen interface, but you need a command line interface. The part before EDIT1 asks one thing and the part after EDIT1 asks something that is quite different (a printf/scanf console). I'll add that often term. emul. support escape sets to control the screen (ANSI/VT100 for example). This isn't supported under the command prompt of Wind. It isn't clear if this would be ok for you – xanatos Feb 23 '11 at 15:23
  • @xanatos I understand that my phrasing may have been unclear - added a clarification as EDIT2. To answer your question - yes, I want a prompt-based interface based solely on printf/scanf (and alikes), but I do not wish to code it from scratch. Just like NDesk.Options solves the issue of command line options, there may be a library that lets one easily define a prompt-based interface with simple menus and input prompts (aka modal dialog). – mark Feb 23 '11 at 16:18
  • @mark If you want to use ANSI/VT100/other standards you can adapt the curses library... Otherwhise I don't think it can be done (unless you truly want a command line interface like DOS). You can't draw anything BELOW the cursor, and the user can input only AT the cursor, and after line 25 there is only scrolling (you don't have any guarantee the screen is "modulus 25" so that after the line 25 (std number of rows in old dos) you return to first line, the dos prompt windows doesn't, it scrolls), so menus and dialogs are impossible. – xanatos Feb 23 '11 at 16:23
  • @xanatos I am fine with truly command line. The menu is a numbered list of options displayed one option per line and then a prompt is given to read the user choice. What is wrong with it? – mark Feb 23 '11 at 19:29
  • @mark A gopher like interface then... You have Console.WriteLine and String.Format, what else do you need? :-) – xanatos Feb 23 '11 at 19:35
  • @xanatos I need a layer above it, which I can give the description of my menus system in some way and it will deal with all the writes and reads. NDesk.Options is a perfect example to demonstrate my point. Why do we need it? After all, we have Environment.GetCommandLine - just parse it and act accordingly. Yet anyone who used NDesk.Options even once cannot imagine working with command line options without it. The same thing is here. Of course, I can start playing with Console writes and reads - I want something ready that does it for me. – mark Feb 23 '11 at 20:29
  • @mark: so, is there something in the Forms for Console Apps library which does not suit your needs? – Maxim Gueivandov Feb 24 '11 at 00:12
  • @maxim I have commented your reply. In short, the remote console I am working with - the one opened using psexec with cmd.exe does not support any VT100 style commands to move cursor around or display colors. My only option is prompt-based interface. – mark Feb 24 '11 at 07:41
  • @mark: have a look at http://consolemenuui.codeplex.com. Other than menus (plus Yes-No dialogs and value prompts which should be easy to implement), I could hardly imagine other possible prompt-based controls. – Maxim Gueivandov Feb 24 '11 at 12:54
  • @Maxim Looks as close to the answer as possible. Please, add it as the answer and I will mark it. – mark Feb 24 '11 at 14:52

8 Answers8

6

How about Mono Curses. http://www.mono-project.com/MonoCurses

kenny
  • 21,522
  • 8
  • 49
  • 87
  • 1
    +1 because while incomplete (it doesn't support the mouse), I could find a use of it. – xanatos Feb 23 '11 at 14:18
  • Curses in general is a good thing, but it is not suitable for me, because remote console may not allow one to play with the cursor location. – mark Feb 23 '11 at 14:55
  • I'll add that it should be quite easy to convert it to use escape sequences. You have to replace the Console class with a Console class that uses escape sequences. If you have a terminal to test it through I think it's two/three days of work (this if they have used the Console class). – xanatos Feb 23 '11 at 15:29
  • @mark, why do you think remote console won't be able to pass simple ANSI commands? And that's what curses is actually doing. At least VT100 commands should be supported everywhere. No additional work is required. – SK-logic Feb 23 '11 at 15:56
  • OK, Let us run Sysinternals' psexec with cmd.exe as the remote command. This gives us remote console. Does it support VT100 commands? I am not even sure how to check it. – mark Feb 23 '11 at 16:20
  • @mark Perhaps it directly support "standard" Console programs? Have you done a test? – xanatos Feb 23 '11 at 19:01
  • What is a "standard" Console program? And what test do you mean? I use psexec with cmd.exe all the time, but I am not sure I understand your point. – mark Feb 23 '11 at 19:30
  • 1
    @mark I mean, have you tried launching one of the Mono Curses demos and see if they work on remote? When you launch a msbuild (my msbuild is quite colored), do you see colors? If you launch the PowerShell in remote, do you see it with a blue background and white charaters and the errors are written in red on black? If yes, then psexec supports colors. To check if it supports cursor movement you have to write a small program. – xanatos Feb 23 '11 at 19:43
  • @xanatos OK, I have run msbuild in the remote console open by psexec. No colors. Tried to run powershell - it even failed to show its prompt (just displayed the banner). From which I conclude that there are not many options for remote console opened through psexec except the prompt-based interface. – mark Feb 24 '11 at 07:12
  • @mark Yeah... So you are REALLY limited to text only. Now at least you are sure :-) – xanatos Feb 24 '11 at 07:17
5

What about Forms for Console Apps?

Alternative (aka ugly workaround) solution could be to create a web application (which will give you more flexibility over your UI) and access it with a browser which runs under DOS. That web application could be hosted on an external web server (IIS), or hosted in your console application which could also launch a DOS-based browser on start-up for user convenience.

Maxim Gueivandov
  • 2,370
  • 1
  • 20
  • 33
  • +1 Because it is quite beautiful. – xanatos Feb 23 '11 at 15:24
  • +1 For an interesting link, but again the ability to place cursor arbitrary on the remote console is not always available. The safest thing is prompt-based interface. As for web service - nice concept, but somewhat of an overkill, don't you think? – mark Feb 24 '11 at 07:13
4

Have a look at consolemenuui.codeplex.com. Other than menus (plus Yes-No dialogs and value prompts which should be easy to implement), I could hardly imagine other possible prompt-based controls.

Maxim Gueivandov
  • 2,370
  • 1
  • 20
  • 33
2

A typical approach is to split your application into two parts: a backend, which can be operated via CLI, and a GUI frontend (may be more than one, e.g., web, winforms, a pure text CLI, whatever). They can communicate using a simple protocol, or a DSL - frontend asks the backend to execute commands, and the backend forces the frontend to execute commands in turn (so, the protocol must be asyncronous). If some complex visualisation is required, it is still possible to prepare it in a backend and execute in a frontent. An obvious thing to avoid doing this way is streaming video, of course.

Probably the best example of such a design is Wolfram Mathematica.

So, answering your question, you won't need any specific libraries, it is a matter of design, coding itself is trivial. The only thing you may need is a tool that will simplify your DSLs implementation. Antlr is just fine, but with C# it is easy to parse simple languages without any external parser generators.

SK-logic
  • 9,605
  • 1
  • 23
  • 35
0

Something like this maybe?

ConsoleRaw Example Image

I am new to C# and coding in general but grew up with a c64 and love the retro style, so I'm writing a overkill memopad/consoleprogram. It draws a menu using an array and a box around it, same goes for text files.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Great effort this is actually what I am looking for. Are you going to be hosting any of the code/libraries on GitHub? I think it would be great to share your knowledge and experience and yes I am retro kid as well grew up on some of the old computers (when computing was fun) – Trevor Sep 28 '17 at 10:59
0

You can try to capture/redirect the output of your own hidden console window to your applications's Windows Forms. Once you have the text you need, you can print it with PictureBox.DrawString or a similar method.

Here's more info about how to capture/redirect console output.

Community
  • 1
  • 1
0

There's a wonderful library for handling command line options, called NDesk.Options. Being included in Mono, it is also known as Mono.Options.
It doesn't provide any additional interaction capabilities, however this is the closest I found to elegantly dealing with console from .NET app.

string data = null;
bool help   = false;
int verbose = 0;
var p = new OptionSet {
    { "file=",      v => data = v },
    { "v|verbose",  v => { ++verbose } },
    { "h|?|help",   v => help = v != null },
};

List<string> extra = p.Parse (args);

As for libraries that encapsulate console UI programming, I didn't find any, although I'd like to hear about one.

Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
  • I know and use NDesk.Options all the time. It is indeed great for command line options, but has nothing to do with CUI. – mark Feb 23 '11 at 12:48
-1

This is a link to a library (Console.Forms) that attempts to create a Console equivalent to System.Windows.Forms:

https://code.google.com/p/console-forms/

It is far from complete, but I think it is going in the direction you're talking about. While there are labels and a half-finished text-box, there are no menus yet. It does demonstrate opening a dialog in front of a main form.

  • There is no source code associated with the project, or well....anything really! – Lorenz Lo Sauer Jul 04 '16 at 11:06
  • There used to be, but it is emptier now -- looks like it was cleared out. Lo Sauer: My comments to the author were still there:1) https://code.google.com/archive/p/console-forms/issues/2 2) https://code.google.com/archive/p/console-forms/issues/1 However, all source-code and downloads are gone.--user1941371 – user1941371 Jul 05 '16 at 11:36