You are not logged in.

#1 2010-07-14 11:49:54

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,389

Kingbash - menu driven auto-completion

One of the things i really missed from my glorious amigaos system was the kingcon terminal emulator.
It featured a simple and useful file browser when autocompletion was requested.
Now i managed to get something similar for bash too, here's what i did:

in .bashrc i added (removed the sourced script, thanks to Procyon ):

kingbash.gb() {
    OUTPUT=$(/usr/bin/kingbash.gambas "$READLINE_LINE" "$READLINE_POINT")
    READLINE_POINT=$(echo "$OUTPUT"|tail -n1)
    READLINE_LINE=$(echo "$OUTPUT"|head -n1)
}

if  [ "$DISPLAY"  != "" ]; then
        #bind -x '"\C-e": kingbash' #Use Ctrl-e
        bind -x '"\t": kingbash'        #Use Tab
fi

Next, comes /usr/bin/kingbash.gambas, a keyboard driven gambas 'script' which allows to browse the filesystem basing on the current commandline.

Here is a screencast: http://wpage.unina.it/aorefice/sharevari/kingbash.avi
The gambas source is here:  http://wpage.unina.it/aorefice/sharevar … ash.tar.gz (edit: updated)

I know gambas is an uncommon language, but unfortunately i don't know others.
It would be fine to use ncurses to make a text version too, so if anybody likes the idea...

Last edited by kokoko3k (2010-07-30 21:39:51)


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Online

#2 2010-07-14 12:16:38

benob
Member
Registered: 2008-11-11
Posts: 187

Re: Kingbash - menu driven auto-completion

It looks very interesting. An ncurses version would be nice for ssh/screen sessions. I could easily imagine fish-like completion for arguments...

Offline

#3 2010-07-14 19:26:46

cesura
Package Maintainer (PM)
From: Tallinn, Estonia
Registered: 2010-01-23
Posts: 1,867

Re: Kingbash - menu driven auto-completion

+1 for ncurses version. Nice work btw smile

Offline

#4 2010-07-14 19:42:24

dcc24
Member
Registered: 2009-10-31
Posts: 732

Re: Kingbash - menu driven auto-completion

+1 for ncurses version as well. This is a great idea, nice work smile


It is better to keep your mouth shut and be thought a fool than to open it and remove all doubt. (Mark Twain)

My AUR packages

Offline

#5 2010-07-14 23:21:00

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Kingbash - menu driven auto-completion

I had no idea bind -x existed. Instead of ncurses which makes a new screen, I am working on a non-intrusive pcfm-style adaption using python. I won't add the in-application directory navigation though.

Offline

#6 2010-07-14 23:21:01

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Kingbash - menu driven auto-completion

edit: duplicate (did I double click submit?)

Last edited by Procyon (2010-07-14 23:21:45)

Offline

#7 2010-07-15 14:33:15

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,389

Re: Kingbash - menu driven auto-completion

Unfortunately Gambas doesn't provide any ncurses binding and console application support is limited, so i'm stuck.
@Procyon, great work with pcfm!


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Online

#8 2010-07-15 19:28:53

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Kingbash - menu driven auto-completion

OK it's about done. Try it out tell me if anything isn't working as expected. (I actually only watched the video of your application, kokoko3k)

I was able to copy some things from pyfmii, I wrote a small tab completion for that too. And thanks a lot to this site, so ncurses isn't needed and it can look a bit like pcfm.

function kingbash.fn() { OUTPUT=$(kingbash.py); READLINE_POINT=$(echo "$OUTPUT"|tail -n1); READLINE_LINE=$(echo "$OUTPUT"|head -n1); }
bind -x '"\t":kingbash.fn'

kingbash.py:
version 1 outdated.

screenshot:
tNHh4MQ

So like I said in the screenshot it will auto-expand the smallest shared substring (like regular TAB) and show clearly what character it expects.

Keys: up/down, shift+up/down, pgup, pgdn, home, end: list movement
right: advances the cursor and quits
left: cursor back and quit
escape: quit
enter: fill in what the arrow points at
backspace: erase backwards and quit
control+u: clear line
anything else: add it to the line, and quit

The size is printed in the brackets, anything else provided by stat can be added.

Small bug: if for example you start off with "mplayer [fr" instead of "mplayer \[fr" it will expand to "mplayer \[Frostii", but the first line will look different (it will work as expected though).
I haven't added a scrollbar yet, so it's a bit hard to see if the list continues.

Feel free to build up from it how you want, sorry the source code is a mess, if you want me to add/change things to it let me know.

EDIT:
Fixed a bug with it not expanding ~ and variables. Just in paths though.
version 2 outdated

Last edited by Procyon (2010-07-16 10:58:20)

Offline

#9 2010-07-16 06:05:19

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,389

Re: Kingbash - menu driven auto-completion

It seems to works great!
Thank you very much.

if you want me to add/change things to it let me know.

It would be nice to add the ability to browse the filesystem too, i think it could be done just by testing if the selected item is a dir when hitting -say- arrow right, and run almost all the code again using the last selection.
arrow left could simply go to the parent dir.
-
I found a little 'bug' too:
if i write:
dd -if=[TAB], or something like
cat ./file.txt| gr[TAB]
it will not complete.

-edit-


vi \[tab]
causes an error: IndexError: string index out of range

cat /usr/lo[tab]
correctly completes to "local", but the cursor stays in place  (after the "o").

ls /et[tab]
completes to "ls /etc ", it would be more convenient to complete to "ls /etc/"

Last edited by kokoko3k (2010-07-16 07:32:13)


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Online

#10 2010-07-16 11:53:01

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Kingbash - menu driven auto-completion

version 3: outdated

Now it will know that a command is expected after & ; |, and a file after = (did I forget any?)

I fixed the "vim \" error.

The other two were fixed in the last edit of my previous post. I'll make it a bit clearer.

Like I said above, I don't know if directory navigation is useful, because you can always just do <ENTER><TAB> and continue there. I can give it a try though.

Last edited by Procyon (2010-07-16 12:45:54)

Offline

#11 2010-07-16 12:40:24

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,389

Re: Kingbash - menu driven auto-completion

Those issues were all fixed, I'm starting to prefer this text version over the graphical one, thanks.

But now we've another one:

#fin[TAB]
Traceback (most recent call last):
  File "/root/scripts/kingbash.py", line 235, in <module>
    main()
  File "/root/scripts/kingbash.py", line 32, in main
    if ( point <= space or prework[-1] in ("|",";","&") or prework[-2:] in ("| ", "; ", "& ") ) and prework[-1] != "=" and prework[-2:] != "= ": #duplicate line below
IndexError: string index out of range

Last edited by kokoko3k (2010-07-16 12:40:47)


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Online

#12 2010-07-16 12:59:10

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Kingbash - menu driven auto-completion

version 4: outdated

Yeah it only worked after ;|&. It's fixed now. The code is a mess though.

I'll try to add the scrollbar from pcfm (it has a really nice one) and the directory browsing with left/right (the keys currently (like regular TAB) aren't that useful anyway). And maybe using insert for marking items so multiple are returned.

Something else that can be added is to auto-run the program again if you type a key. I don't think this would get in the way of how you expect regular TAB to work. For instance if you have cat file01.txt, you get the tab completion of file0, you type 1, and then immediately hit TAB again.

Last edited by Procyon (2010-07-16 18:49:46)

Offline

#13 2010-07-16 19:00:48

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Kingbash - menu driven auto-completion

version 5: outdated

No scrollbar yet. I added
left: go up a dir, and rerun (it only affects the path, so it will preserve the filename and try to find it in the parent directory)
right: like enter, but rerun if it's a directory
insert: it will add the current item to the line, and it will then add the path+file you already had
So if you have:
"cat /etc/rc.<TAB>" you can press <INS> on rc.conf and it will then show:
"cat /etc/rc.conf /etc/rc." with the program reran

I also made it rerun the program when you type a single key. This may create some unexpected/bad behavior, but I'm not sure. If so it should be a configurable option.

Last edited by Procyon (2010-07-16 20:30:08)

Offline

#14 2010-07-16 20:31:27

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Kingbash - menu driven auto-completion

version 6: outdated

Scrollbar! Only a small off-by-one from the pcfm code.

tNHliZg

EDIT
version 7: outdated
Some small fixes: path expansion (cd ~/\(brackets\)/file.<TAB>), not erasing \ as the last item, and adding the xterm keycode for backspace.

I'm not sure whether to keep the auto-tab after typing a single character... What do you think about this?

Last edited by Procyon (2010-07-18 13:06:52)

Offline

#15 2010-07-18 14:28:04

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Kingbash - menu driven auto-completion

version 8: outdated

Small fixes again: completing commands that use a path, make backspace rerun the program, alt+backspace erases a whole word (delimited by quotes, space, slash), shell escapes missing after insert, don't rerun after space and equal sign.

I can't seem to get rid of the "blink". Maybe if it was pure bash.

Last edited by Procyon (2010-07-21 17:15:58)

Offline

#16 2010-07-18 21:58:58

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,389

Re: Kingbash - menu driven auto-completion

Procyon wrote:

I'm not sure whether to keep the auto-tab after typing a single character... What do you think about this?

I'm just back from the weekend, so i will see your work (that seems exciting!) tomorrow.
Maybe i didn't understood, but, just in case i'm right, I think that there is no drawback in leaving the choice to the user.
I'd find very comfortable to type "/" and walk through the entire filesystem.

Last edited by kokoko3k (2010-07-18 21:59:30)


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Online

#17 2010-07-19 11:19:29

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,389

Re: Kingbash - menu driven auto-completion

- if i type mplayer /[TAB] and i browse the filesystem with the right key (say until /mnt/movies/anime/), as soon as i hit the left arrow, it returns to the root ("/")  instead of the parent dir (/mnt/movies) is it expected?
- Links pointing to directories should be browsable.

-edit-
It would be nice to highlight the current item or add another arrow to the right, sometimes it is difficult to identify it (with long paths).

Thanks!

Last edited by kokoko3k (2010-07-19 17:29:00)


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Online

#18 2010-07-19 20:35:03

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Kingbash - menu driven auto-completion

I will look at 1 & 2 tomorrow. (1 should be a small bug (I have seen it work right before) and I have done 2 in pyfmii already, so I can just copy-paste it)

About 3, I thought about this, but there is already a white background for that which has already been typed / guessed. Typically -- mc-style -- is a blue background for the current item.
In pcfm I did an arrow on the right, it helps a bit, but you still lose it when tabbing in and it's near the bottom.
Maybe a blue background around the white background? Or use underline for the already-completed.

By the way, do you also get a blink? It's visible when there are only 1 or 0 items in the complete list. It seems to happen any time python is called, even with just sys.exit(0).

Offline

#19 2010-07-20 09:44:28

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,389

Re: Kingbash - menu driven auto-completion

If by blink you mean that the current cimmand line disappears just after TAB, yes.
I don't think you can do something about it, it happens even whith the graphical version.


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Online

#20 2010-07-21 17:22:50

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Kingbash - menu driven auto-completion

version 9: outdated

This addresses the issues above.

The selection bar doesn't go to the end of the line, I opened a new topic that could fix both that and the issue of really long file names: https://bbs.archlinux.org/viewtopic.php?id=101446

About the blink, it's hard to rewrite everything in bash, it could be possible to just run the last argument through `ls` to determine how many completions are possible, and to only call python (with the list) on 2 or more.

EDIT
version 10: outdated

There was a crashing bug with multiple pages.

EDIT2
version 11: outdated

Thanks to falconindy in the linked thread, the selection bar now reaches to the end, and long filenames don't mess up the display. If there is a newline in the filename, it will show as (nl)

I made a small fix to the scrollbar. It used to disappear on long lists sometimes.

And I changed the color of the selection bar to cyan, as blue is a really dark color with the default color scheme.

EDIT3
I forgot to add, a pure bash script doesn't fix the blink either. It seems to be caused by any long script.

Last edited by Procyon (2010-07-22 09:31:04)

Offline

#21 2010-07-22 07:29:31

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,389

Re: Kingbash - menu driven auto-completion

It's getting better every day.
A little usability issue:
Browsing to an empty folder (ls /home/koko/[tab] -> /home/koko/mymusic/rock/emptyfolder) causes the script to return that folder.
Maybe it should allow the user to go back or press enter.


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Online

#22 2010-07-22 10:02:29

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Kingbash - menu driven auto-completion

version 12: (outdated) http://aur.pastebin.com/XNKJa2wx

Now it will send a parameter to the next rerun process, so you can continue browsing when there are only 0 or 1 completions if you use left and right arrow.

I also added this behavior in:
"cd /<TAB>"
"h"
And it completes and continues browsing in /home/

Last edited by Procyon (2010-07-23 22:43:12)

Offline

#23 2010-07-22 10:57:44

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,389

Re: Kingbash - menu driven auto-completion

wonderful!


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Online

#24 2010-07-23 23:35:03

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Kingbash - menu driven auto-completion

version 13: outdated

I added a few features:
First is recommendations. They show up in red at the top.
It looks at the command, and matches the filename to a regex. I was able to make these matches as broad as I wanted because many commands can go to filetypes, unlike with a filemanager where you have to pick one command. If anyone is missing commands/types, let me know.

Then I added expansion of * and ?. I didn't make it check for escapes, which shouldn't make a big difference, and because it wants to show escaped lines it will change into \* or \? on the KingBash> line (and when you press Escape unfortunately).

So for example:
mirage *.gif<TAB>
and you can choose which gif

In addition you can press * in the menu to paste everything that is recommended to the command line. If everything is not recommended, everything will be pasted. The program will NOT rerun after that unlike Insert, not sure if that is wanted.

While I was sorting the list by recommendations, I thought I'd sort it by directories too.

Version 14: outdated

Small fixes in path expansion, page up, wrong usage, "./" regression. Now it tries to use a third of the screen with fcntl, or 23 lines if that fails.

Last edited by Procyon (2010-07-26 16:23:28)

Offline

#25 2010-07-26 07:19:32

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,389

Re: Kingbash - menu driven auto-completion

Ok, now i'm getting this:

#./mup<tab> (executable is mupen64plus)
Traceback (most recent call last):
  File "/root/scripts/kingbash.py", line 444, in <module>
    main()
  File "/root/scripts/kingbash.py", line 102, in main
    if point > space and isrecommended(prework.split()[0], suggestion, dirappend=="/"):
IndexError: list index out of range

Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Online

Board footer

Powered by FluxBB