Trying to write a full-blown terminal emulator on golang

2,605 views
Skip to first unread message

Sherjil Ozair

unread,
May 27, 2012, 9:46:33 PM5/27/12
to golan...@googlegroups.com
Hi,

I'm trying to write a full terminal using golang using os/exec.

I can handle all the stuff which is line-by-line, i.e. which follows a strict read-eval-print loop.

I'm stuck on trying to make it work with stuff like, vi, nano that essentially use some sort of curses library.

Is there any support on golang that can effectively handle such calls?

For example, it could help if I redirect the stdin and stdout of vi to some variables in my program, which could be readers and writers, and feed data on the writer and take data from the writer.

Help would be much appreciated.

Thanks,
Sherjil Ozair

Kevin Ballard

unread,
May 28, 2012, 1:44:52 AM5/28/12
to Sherjil Ozair, golan...@googlegroups.com
That is a very complicated subject. But in general, you need to emulate a tty device, not just handle stdin/stdout.

-Kevin

unread,
May 28, 2012, 2:48:31 AM5/28/12
to golan...@googlegroups.com
I would start by looking on the Internet for examples of terminal emulation in C, and by examining the source code of terminal emulators such as Konsole or vte.

Sherjil Ozair

unread,
May 28, 2012, 7:11:01 AM5/28/12
to Uriel, golan...@googlegroups.com
What do you think about this idea?

Whenever a user presses any key on the browser terminal, I send that
keystroke to the server, and golang sends it to the stdin of the
program. Now, I record the change that made to the output buffer. I
transmit the diff back to the browser, and it alters display.

Where would I be stuck if I chose this simple naive approach?

I have seen nsf's termbox-go project, which has an option which
returns the complete output buffer array of cells(attribute and
character), so I think getting the output buffer should not be a
problem.

Why do you guys feel this is gonna be very tough?

On Mon, May 28, 2012 at 10:59 AM, Uriel <ur...@berlinblue.org> wrote:
> Is not that simple, for an attempt to make a simple terminal emulator
> see: http://st.suckless.org/
>
> But as far as i know nobody has succeeded, writing a terminal emulator
> is not as hard as writing a web browser, but is closer than you might
> think.

Rémy Oudompheng

unread,
May 28, 2012, 8:03:30 AM5/28/12
to Sherjil Ozair, Uriel, golan...@googlegroups.com
On 2012/5/28 Sherjil Ozair <sherji...@gmail.com>:
> What do you think about this idea?
>
> Whenever a user presses any key on the browser terminal, I send that
> keystroke to the server, and golang sends it to the stdin of the
> program. Now, I record the change that made to the output buffer. I
> transmit the diff back to the browser, and it alters display.
>
> Where would I be stuck if I chose this simple naive approach?
>
> I have seen nsf's termbox-go project, which has an option which
> returns the complete output buffer array of cells(attribute and
> character), so I think getting the output buffer should not be a
> problem.
>
> Why do you guys feel this is gonna be very tough?

You can also study how ajaxterm is made:
http://antony.lesuisse.org/software/ajaxterm/

Rémy.

André Moraes

unread,
May 28, 2012, 9:03:09 AM5/28/12
to Sherjil Ozair, golan...@googlegroups.com
On Mon, May 28, 2012 at 8:11 AM, Sherjil Ozair <sherji...@gmail.com> wrote:
> What do you think about this idea?
>
> Whenever a user presses any key on the browser terminal, I send that
> keystroke to the server, and golang sends it to the stdin of the
> program. Now, I record the change that made to the output buffer. I
> transmit the diff back to the browser, and it alters display.

shell-in-a-box does that.

https://code.google.com/p/shellinabox/

Most of your code will be javascript and not Go.



--
André Moraes
http://amoraes.info

Sherjil Ozair

unread,
May 28, 2012, 1:28:41 PM5/28/12
to André Moraes, golan...@googlegroups.com
Thanks for the answers, shell-in-a-box and ajaxterm were interesting
finds for me. Ajaxterm is in particularly interesting since its in
python, a similar language to golang.

But guys, the issue remains. I want to build this in go, so I need to
know about the goland specific things. For example, I want to use some
golang low-level function using which I could build a buffer array,
non-intrusively. And write to stdin, without modifying the behaviour
of the program in any other way.

André Moraes

unread,
May 28, 2012, 3:21:59 PM5/28/12
to Sherjil Ozair, golan...@googlegroups.com
>
> But guys, the issue remains. I want to build this in go, so I need to
> know about the goland specific things. For example, I want to use some
> golang low-level function using which I could build a buffer array,
> non-intrusively. And write to stdin, without modifying the behaviour
> of the program in any other way.
>

The first place you need to look is:
http://golang.org/pkg/os/exec/
http://golang.org/pkg/os
http://golang.org/pkg/bytes

Sherjil Ozair

unread,
May 31, 2012, 10:31:48 AM5/31/12
to André Moraes, golan...@googlegroups.com
Yes, indeed. 
And what about  https://github.com/kr/pty ?
Reply all
Reply to author
Forward
0 new messages