October 25, 2012
On Fri, Oct 26, 2012 at 12:27:38AM +0200, Jens Mueller wrote:
> Walter Bright wrote:
[...]
> > A module that only sets the console color is a little too light to be a phobos entry.
> > 
> > A more comprehensive module that included:
> > 
> > 1. getting mouse input
> 
> Anybody an idea how to this on Linux?

You can only do this in terminals that support it. XTerm, I believe, has escape sequences that you can send to turn on mouse tracking. Those will show up as special escape sequences on stdin, which will have to be intercepted and removed from the program's normal input stream. Other modern terminals probably have their own way of doing mouse tracking. AFAIK, there isn't any standard for this, so you'll have to stick with terminal-specific code.

(Which is why I recommended earlier that this module must be modular so that support for specific terminals can be plugged in easily -- for the initial stab, something very simple such as vt100 support may be good enough, as long as it's easy to add support for new terminals.)


> > 2. getting size of the console
> 
> This is easy if you just mean the number of lines and columns.

I think that's good enough. A text-mode app doesn't need to know pixel sizes. (If it does, it really should be using a real GUI toolkit instead.)


> > 3. moving the cursor around
> 
> This also assuming you just want something like, move 10 lines up, 3 lines right etc.

Handling absolute positions (x columns y rows from upper left corner of terminal) is a must. Pixel positions is not necessary (and probably impossible -- text terminals AFAIK don't provide that kind of info).


> > 4. drawing boxes in the console window
> 
> Should this be done by moving the cursor and inserting characters such that you have a box in the end?

I think the implementation details are unimportant, as long as there's a
way to say "draw a box at (x,y) with dimensions (w,h)" and the module
will do whatever is necessary to accomplish that.


> > 5. setting the contents of the title bar
> 
> The title bar of what?

The terminal window. Many X11 terminals, like xterm and rxvt, support an escape sequence that lets you set/change what's displayed on the title bar of the window.

(Personally, though, I don't like this feature; I find it very annoying. But many people like it, so it should still be supported, I think.)


> > 6. supporting cut/paste
> 
> Don't know how to do this? Anybody a starting point?

I'm not sure what Walter is referring to specifically. Usually cut and paste is supported directly by the terminal, and the program running in the terminal doesn't get to control it. It only sees pasted text as a sudden large chunk of data on stdin. I don't think this is the job of the console module (Walter, correct me if I'm wrong).


> > 7. getting no-echo raw input
> 
> Tried this but couldn't make it work yet. This is useful for passwords prompts, right?

Not just that, but also for fully-interactive programs that want to capture every keystroke immediately (instead of waiting for the user to hit Enter). Like games and stuff. Or menu-driven systems where the user can navigate between menus and items without needing to hit Enter each time.

For Unix terminals, you need to send certain escape sequences (specific to the terminal) to enable what is called 'raw' mode or 'cbreak' mode. (Googling for 'cbreak' should give you useful references.) This will cause the terminal to immediately transmit keystrokes, instead of buffering them until the user hits Enter.

Also, make sure that there is a way to turn off this mode after the program is finished, otherwise the terminal may become unusable when it returns to the shell prompt. :)


> > 8. setting the size of the cursor
> 
> The size of the cursor? Why should I want to change its size?
[...]

I think Walter is referring to the DOS/Windows-specific feature that certain BIOS calls allow you to change the starting/ending scanlines of the cursor. I suppose some people like to see a big flashing white box for their cursor and others prefer just a flashing underscore, but personally, I don't see this as a must-have feature. I think some terminals don't even support such a setting.

In any case, I think this is a low-priority nice-to-have thing. The important stuff are cursor positioning, box drawing, incremental updates, cbreak mode, etc..


T

-- 
A linguistics professor was lecturing to his class one day. "In English," he said, "A double negative forms a positive. In some languages, though, such as Russian, a double negative is still a negative. However, there is no language wherein a double positive can form a negative." A voice from the back of the room piped up, "Yeah, yeah."
October 26, 2012
On Thursday, 25 October 2012 at 22:27:52 UTC, Jens Mueller wrote:
>> 5. setting the contents of the title bar
> The title bar of what?

Here's how you do it on xterm:

writefln("\033]0;%s\007", title);

On Windows it is an api function:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686050%28v=vs.85%29.aspx

>> 6. supporting cut/paste
>
> Don't know how to do this? Anybody a starting point?

I wouldn't worry about it up front, the system will give simple stuff for you and getting it right is a bit of a pain because you have to bring in other api calls (on both Windows and Linux; linux will need some X11 calls I believe.)

>> 7. getting no-echo raw input
>
> Tried this but couldn't make it work yet. This is useful for passwords prompts, right?


Here's a brief example for unix:

http://arsdnet.net/dcode/input.d

On Windows I'm almost certain it is actually easier but I haven't done that for a while and don't remember it right now.


The reason my code there puts it in a struct is so it is automatically put back to the previous mode when you go out of scope.


>> 8. setting the size of the cursor
> The size of the cursor? Why should I want to change its size?

There's a way to do this on  windows i'm pretty sure and it is hit and miss on linux - sometimes works, sometimes doesn't.

But a reason you might want it is to visually indicate the difference between insert and overwrite mode.

In a text editor for instance, a short cursor means insert. If you press the insert key on the keyboard, you switch to replace mode, and a taller cursor can show that.


>
> Jens


October 26, 2012
On Fri, Oct 26, 2012 at 03:35:42AM +0200, Adam D. Ruppe wrote:
> On Thursday, 25 October 2012 at 22:27:52 UTC, Jens Mueller wrote:
> >>5. setting the contents of the title bar
> >The title bar of what?
> 
> Here's how you do it on xterm:
> 
> writefln("\033]0;%s\007", title);
> 
> On Windows it is an api function:
> 
> http://msdn.microsoft.com/en-us/library/windows/desktop/ms686050%28v=vs.85%29.aspx

And just to add, on Posix it's probably good to do something similar to Windows, where exiting the program will reset the title back to what it originally was. There's an xterm escape sequence that lets you read the current title string from stdin, so that can be used to save the original title.


> >>6. supporting cut/paste
> >
> >Don't know how to do this? Anybody a starting point?
> 
> I wouldn't worry about it up front, the system will give simple stuff for you and getting it right is a bit of a pain because you have to bring in other api calls (on both Windows and Linux; linux will need some X11 calls I believe.)
[...]

I don't think you can assume X11 calls will work. You may be running on GNU screen, or on a remote SSH connection.

Regardless, I think on Linux this feature is not needed, since most terminals will give the user a way to cut and paste independently of the program running in them, so implementing this in a console library is probably redundant.


T

-- 
A bend in the road is not the end of the road unless you fail to make the turn. -- Brian White
October 26, 2012
On Friday, 26 October 2012 at 01:35:43 UTC, Adam D. Ruppe wrote:
> On Thursday, 25 October 2012 at 22:27:52 UTC, Jens Mueller wrote:
>>> 5. setting the contents of the title bar
>> The title bar of what?
>
> Here's how you do it on xterm:
>
> writefln("\033]0;%s\007", title);
>

Yeah, the problem is it does not work in all terminals.

October 26, 2012
H. S. Teoh wrote:
> On Fri, Oct 26, 2012 at 12:27:38AM +0200, Jens Mueller wrote:
> > Walter Bright wrote:
> [...]
> > > A module that only sets the console color is a little too light to be a phobos entry.
> > > 
> > > A more comprehensive module that included:
> > > 
> > > 1. getting mouse input
> > 
> > Anybody an idea how to this on Linux?
> 
> You can only do this in terminals that support it. XTerm, I believe, has escape sequences that you can send to turn on mouse tracking. Those will show up as special escape sequences on stdin, which will have to be intercepted and removed from the program's normal input stream. Other modern terminals probably have their own way of doing mouse tracking. AFAIK, there isn't any standard for this, so you'll have to stick with terminal-specific code.
> 
> (Which is why I recommended earlier that this module must be modular so that support for specific terminals can be plugged in easily -- for the initial stab, something very simple such as vt100 support may be good enough, as long as it's easy to add support for new terminals.)

So there exists no portable library abstracting mouse input?
If that's the case then being modular is the only option. How would you
design it? I don't plan to implement this. But leaving the door open for
others should be possible.

[snip]
> > > 3. moving the cursor around
> > 
> > This also assuming you just want something like, move 10 lines up, 3 lines right etc.
> 
> Handling absolute positions (x columns y rows from upper left corner of terminal) is a must. Pixel positions is not necessary (and probably impossible -- text terminals AFAIK don't provide that kind of info).

Okay. But this is no problem to build on top. I forget to mention that there is one sequence that move the cursor a specific line/column.

> > > 4. drawing boxes in the console window
> > 
> > Should this be done by moving the cursor and inserting characters such that you have a box in the end?
> 
> I think the implementation details are unimportant, as long as there's a
> way to say "draw a box at (x,y) with dimensions (w,h)" and the module
> will do whatever is necessary to accomplish that.

That means this can also be built on top of cursor moving.

> > > 5. setting the contents of the title bar
> > 
> > The title bar of what?
> 
> The terminal window. Many X11 terminals, like xterm and rxvt, support an escape sequence that lets you set/change what's displayed on the title bar of the window.
> 
> (Personally, though, I don't like this feature; I find it very annoying. But many people like it, so it should still be supported, I think.)

I have to look up that. I find it strange because the Linux console does not have a title bar. But if that is accessible using tinfo I will add this. I find it strange, too. I think changing the title of a window is more like a GUI thing.

> > > 7. getting no-echo raw input
> > 
> > Tried this but couldn't make it work yet. This is useful for passwords prompts, right?
> 
> Not just that, but also for fully-interactive programs that want to capture every keystroke immediately (instead of waiting for the user to hit Enter). Like games and stuff. Or menu-driven systems where the user can navigate between menus and items without needing to hit Enter each time.
> 
> For Unix terminals, you need to send certain escape sequences (specific to the terminal) to enable what is called 'raw' mode or 'cbreak' mode. (Googling for 'cbreak' should give you useful references.) This will cause the terminal to immediately transmit keystrokes, instead of buffering them until the user hits Enter.
> 
> Also, make sure that there is a way to turn off this mode after the program is finished, otherwise the terminal may become unusable when it returns to the shell prompt. :)

Thanks for the pointers.
So these are then two things. First noecho and the other one is raw
input.

> > > 8. setting the size of the cursor
> > 
> > The size of the cursor? Why should I want to change its size?
> [...]
> 
> I think Walter is referring to the DOS/Windows-specific feature that certain BIOS calls allow you to change the starting/ending scanlines of the cursor. I suppose some people like to see a big flashing white box for their cursor and others prefer just a flashing underscore, but personally, I don't see this as a must-have feature. I think some terminals don't even support such a setting.
> 
> In any case, I think this is a low-priority nice-to-have thing. The important stuff are cursor positioning, box drawing, incremental updates, cbreak mode, etc..

Can you say something about incremental updates? Any pointers?

Jens
October 26, 2012
Adam D. Ruppe wrote:
> On Thursday, 25 October 2012 at 22:27:52 UTC, Jens Mueller wrote:
> >>5. setting the contents of the title bar
> >The title bar of what?
> 
> Here's how you do it on xterm:
> 
> writefln("\033]0;%s\007", title);
> 
> On Windows it is an api function:
> 
> http://msdn.microsoft.com/en-us/library/windows/desktop/ms686050%28v=vs.85%29.aspx
> 
> >>6. supporting cut/paste
> >
> >Don't know how to do this? Anybody a starting point?
> 
> I wouldn't worry about it up front, the system will give simple stuff for you and getting it right is a bit of a pain because you have to bring in other api calls (on both Windows and Linux; linux will need some X11 calls I believe.)
> 
> >>7. getting no-echo raw input
> >
> >Tried this but couldn't make it work yet. This is useful for passwords prompts, right?
> 
> 
> Here's a brief example for unix:
> 
> http://arsdnet.net/dcode/input.d
> 
> On Windows I'm almost certain it is actually easier but I haven't done that for a while and don't remember it right now.
> 
> 
> The reason my code there puts it in a struct is so it is automatically put back to the previous mode when you go out of scope.
> 
> 
> >>8. setting the size of the cursor
> >The size of the cursor? Why should I want to change its size?
> 
> There's a way to do this on  windows i'm pretty sure and it is hit and miss on linux - sometimes works, sometimes doesn't.
> 
> But a reason you might want it is to visually indicate the difference between insert and overwrite mode.
> 
> In a text editor for instance, a short cursor means insert. If you press the insert key on the keyboard, you switch to replace mode, and a taller cursor can show that.

I see.
Thanks for many pointers.

Jens
October 26, 2012
On Tuesday, 23 October 2012 at 22:47:40 UTC, Walter Bright wrote:
> On 10/22/2012 3:55 AM, Andrei Alexandrescu wrote:> On 10/22/12 9:47 AM, Jens Mueller wrote:
> >> This is probably interesting for Phobos. But I'm not the one
> to make a
> >> decision. The core Phobos developers should decide.
> >> Hopefully somebody is reading this.
> >
> > Off the top of my head something that is specific for only
> certain systems
> > (Unixen in this case) is decidedly of less Phobos interest.
> We could,
> > nevertheless, put such functionality in system-specific
> modules.
>
> A module that only sets the console color is a little too light to be a phobos entry.
>
> A more comprehensive module that included:
>
> 1. getting mouse input
> 2. getting size of the console
> 3. moving the cursor around
> 4. drawing boxes in the console window
> 5. setting the contents of the title bar
> 6. supporting cut/paste
> 7. getting no-echo raw input
> 8. setting the size of the cursor
>
> would be a definite candidate. I.e. a module that can support building a text mode screen app (like a text editor).

This would look like a full blown TUI-Toolkit and we should model
the API
after successfull GUI-Frameworks like Qt, i.e. provide a event
loop, use
a Signal/Slot mechanism etc.

That would be a real improvement over nCurses. What do you think?


October 26, 2012
On Friday, 26 October 2012 at 03:28:43 UTC, H. S. Teoh wrote:
> I don't think you can assume X11 calls will work. You may be running on GNU screen, or on a remote SSH connection.

Yeah. Vim does it though, if it can connect to a display, it does and enhances its copy/paste ability. If it can't, it carries on without.

There is a problem where sometimes the program dies with a BadWindow error if you move it in a screen from one to another....but it works most the time so they are doing something right.

> Regardless, I think on Linux this feature is not needed, since most terminals will give the user a way to cut and paste

It's still nice to be able to improve it but yeah i would agree it is a low priority since the built in is usually good enough.
October 26, 2012
On Friday, 26 October 2012 at 06:08:39 UTC, Robik wrote:
> Yeah, the problem is it does not work in all terminals.

Yeah, but neither will any other feature. But virtually every one used in practice nowadays supports it though.
October 26, 2012
On Fri, Oct 26, 2012 at 08:08:36AM +0200, Robik wrote:
> On Friday, 26 October 2012 at 01:35:43 UTC, Adam D. Ruppe wrote:
> >On Thursday, 25 October 2012 at 22:27:52 UTC, Jens Mueller wrote:
> >>>5. setting the contents of the title bar
> >>The title bar of what?
> >
> >Here's how you do it on xterm:
> >
> >writefln("\033]0;%s\007", title);
> >
> 
> Yeah, the problem is it does not work in all terminals.

The correct solution is to examine the TERM environment variable to find out what kind of terminal it is, and then use escape sequences specific to that terminal.


T

-- 
IBM = I'll Buy Microsoft!