You are not logged in.

#51 2010-08-06 18:53:51

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

Re: Kingbash - menu driven auto-completion

Version 20: outdated http://aur.pastebin.com/xUkwJrth

For file mode: delete added, ^C kills the line

I added some help text. Maybe it's become a bit too much for pacman to do with post_install()

The ascii clean bind command for ^R is: bind -x '"\x12": kingbash.hs'

Last edited by Procyon (2010-08-08 13:20:31)

Offline

#52 2010-08-08 13:26:56

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

Re: Kingbash - menu driven auto-completion

version 21: outdated

F1 now prints a key table
In dmenu mode Escape restores the prompt to what it was originally, instead of clearing it entirely. (Tab completion just leaves it as it is)
In dmenu mode Tab works like Insert
dmenu mode now also works case insensitively
I added a bar at the bottom. Let me know if you don't like it.
The AUR package's post.install now only prints "run kingbash without arguments for help" instead of actually doing that.

Last edited by Procyon (2010-08-08 15:11:46)

Offline

#53 2010-08-08 14:11:08

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 1,959
Website

Re: Kingbash - menu driven auto-completion

I don't get how the dmenu works.

 echo -e 'a\nb' | KingBash -r

this prints the help.  I was expecting a way to choose 'a' or 'b'

function kingbash.hs() {
 history -a
 echo -n "KingBash> $READLINE_LINE"
 OUTPUT=`KingBash -r <(tac ~/.bash_history)`
 READLINE_POINT=`echo "$OUTPUT" | tail -n 1`
 READLINE_LINE=`echo "$OUTPUT" | head -n -1`
 echo -ne "\r\e[2K"; }
bind -x '"%": kingbash.hs'

and after sourcing the above code, ^R just uses the normal bash history search function.

BTW: time to get your stuff into github, so we can benefit from version control, easier packaging, etc.


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#54 2010-08-08 14:35:57

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

Re: Kingbash - menu driven auto-completion

For CLI dmenu, you also need to do export READLINE_POINT=0. I forgot to add that to the help text.
EDIT: actually, stdin will be gone, so it needs extra code that reopens /dev/tty

In the function.hs you quoted, I didn't yet know the right ascii code for ^R and used % (and explained it below). In the post after it I had found out it was \x12. From the help text:

function kingbash.hs() {
  echo -n "KingBash> $READLINE_LINE" #Where "KingBash> " looks best if it resembles your PS1, at least in length.
  history -a
  OUTPUT=`KingBash -r <(tac ~/.bash_history)`
  READLINE_POINT=`echo "$OUTPUT" | tail -n 1`
  READLINE_LINE=`echo "$OUTPUT" | head -n -1`
  echo -ne "\r\e[2K"; }
bind -x '"\x12":kingbash.hs'

I don't really like using extra tools like github. I know there is a problem with DOS-mode newlines, the AUR package fixes it (and using github shouldn't make it easier for AUR users) What I can do from now on is present new versions like:
Version 21: wget -O- http://aur.pastebin.com/download.php?i=BKEaUAtm | sed 's/\r$//' > KingBash; chmod +x KingBash

Last edited by Procyon (2010-08-08 14:48:02)

Offline

#55 2010-08-08 15:07:39

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

Re: Kingbash - menu driven auto-completion

Version 22: outdated http://aur.pastebin.com/Rrfg00a3
Quick Install: wget -O- http://aur.pastebin.com/download.php?i=Rrfg00a3 | sed 's/\r$//' > KingBash; chmod +x KingBash

If stdin is a pipe, go to dmenu mode, and read the info. (and reopen stdin for user input)
For CLI dmenu, this eliminates the need for "-r <(cat)" (and makes it work)

Last edited by Procyon (2010-08-08 17:28:26)

Offline

#56 2010-08-08 16:16:09

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 1,959
Website

Re: Kingbash - menu driven auto-completion

Great.  I love the xmms-like history searching.
Also reading from stdin works fine now.  I don't need the 'export READLINE_POINT=0'
2 more things:
- can you make the scriptname lowercase?
- do you *need* to output the character position in dmenu mode? i don't see the point. it just gets in the way afaict.


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#57 2010-08-08 16:57:45

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

Re: Kingbash - menu driven auto-completion

Thanks, it's getting there, right? I actually have to use this because I patched libx11 without keyboard grab because VirtualBox started misbehaving, the flipside of which is that dmenu doesn't work at all. For the past few months I was using pcfm which also has a stdin mode, but it's slower and since I stopped updating it I never bothered fixing some bugs.

Since there is now a good reason to use the program on the command line, it makes sense to make the name lowercase. Since it would break people's current functions, I'll add a symlink or a post.upgrade warning. Which would be better?

I will add a special variable if stdin was read, and use it to suppress printing the cursor position.

Offline

#58 2010-08-08 17:11:13

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 1,959
Website

Re: Kingbash - menu driven auto-completion

post.upgrade

btw try ^R and then type *, kingbash just seems to abort. (even when i have entries with '*' in them)


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#59 2010-08-08 17:40:26

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

Re: Kingbash - menu driven auto-completion

Version 23: (outdated) http://aur.pastebin.com/254BQMwG
Quick install: wget -O- http://aur.pastebin.com/download.php?i=254BQMwG | sed 's/\r$//' > kingbash; chmod +x kingbash

The asterisk is a special key for tab completion. In the other mode I made it quit by mistake, it will now get added to the line instead.
This version also doesn't print the cursor position in dmenu mode (if stdin is a pipe)

Last edited by Procyon (2010-08-27 20:34:16)

Offline

#60 2010-08-27 19:36:23

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 1,959
Website

Re: Kingbash - menu driven auto-completion

i'm loving kingbash for history searching,
one thing that bothers me though: i don't want duplicate lines in it's suggestions.

I tried filtering at the input level like so:

function kingbash.hs() {
  (...)
  OUTPUT=$(KingBash -r <(tac ~/.bash_history | uniq))

this of course only uniq's the input, not the suggestions when a filter is applied.
so next i tried to modify grep_list, by checking if the previous line differs, but that makes the list of suggestions empty tongue

def grep_list(patterns, lines):
        good_list=[]
        for line in lines:
            (...)
                if found:
                        if good_list and good_list[ len(good_list) -1 ] != line:
                                good_list.append(line)
        return good_list

so, i'm leaving this to you tongue


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#61 2010-08-27 20:10:39

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

Re: Kingbash - menu driven auto-completion

Version 24: outdated http://aur.pastebin.com/ePSQdz5d
Quick install: wget -O- http://aur.pastebin.com/download.php?i=ePSQdz5d | sed 's/\r$//' > kingbash; chmod +x kingbash

New:
Dieter@be's uniq'd history suggestion (just 1 more step).

In filename completion I made Tab clear the filename but leave the path (this is sometimes useful when in a directory with files that start the same or you already completed half a name, but you want a new file after all).

I added a check for "i" in $- as per the suggestion of jlaunay on the AUR page as he was having trouble with scp - this is only a change in the "usage advice" string for the binds in your bashrc

Extra key codes for Home and End so they work in VTE.

Extra key code for Tab as a literal Tab beside the key code. I had some issues with this in dmenu mode.

In a previous version I also fixed a key code for Delete.

Last edited by Procyon (2010-08-29 15:59:06)

Offline

#62 2010-08-27 20:33:49

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 1,959
Website

Re: Kingbash - menu driven auto-completion

oh of course, the bug in my grep_line change is so stupid.  well at least i almost got it smile thanks buddy


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#63 2010-08-28 00:32:58

jlaunay
Member
From: France
Registered: 2010-08-15
Posts: 11
Website

Re: Kingbash - menu driven auto-completion

Procyon wrote:

I added a check for "i" in $- as per the suggestion of jlaunay on the AUR page as he was having trouble with scp - this is only a change in the "usage advice" string for the binds in your bashrc

I was having trouble not only with scp but especially with any interactive shells, I stumbled upon this issue while debugging a rsync based backup system.
Thanks for this helpful tool.

Offline

#64 2010-08-29 12:33:56

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 1,959
Website

Re: Kingbash - menu driven auto-completion

Some more suggestions:

* maybe in usage_help_text(), you could explain the commandline argument(s), ie -r
because i'm not sure what exactly -r does and when/why it's needed.

* replace all occurences of ~/.bash_history with $HISTFILE

* when using <..> | kingbash, you can use tab to "load" the entry and optionally modify it.  that's great. just like dmenu.
However, when using kingbash.hs you always "have" to confirm (and potentially edit) the line, before executing it. (because when the function exists, the readline vars are set by the shell,
so it looks ready for executing, but you still have to confirm with enter.  I would prefer to execute directly, if that's somehow possible (maybe by writing an "enter" character?) if i want to modify the entry first, i can always use tab. this would allow us to save 1 chartacter of typing, and would make it more similar to how dmenu_run works.

* don't use "/usr/bin/kingbash" in usage function (in fact, nowhere). it's up to the user to put kingbash in his $PATH, just refer to 'kingbash'

finally, what is pcfm? some in the beginning of this thread this was inspired by it.


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#65 2010-08-29 13:39:15

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

Re: Kingbash - menu driven auto-completion

1. I added some helpful text about invocation

2. $HISTFILE is now used

3. Yes it is possible but not without external tools. I added a check to see if the old READLINE_LINE is different from the new READLINE_LINE, and if so to use xdotool to press Return. I commented it out by default.
This works even if you press Escape because Escape restores the prompt to what it was before, in dmenu/history mode at least.

4. argv[0] is used so it should be ok.

5. I wrote pcfm at the beginning of this year. It's written in bash and uses the same kind of "ncurses with only escape codes". It uses $PROMPT_COMMAND and a session file because it quits when a file is selected (or you start typing) and it then leaves the rest over to bash by writing the filename and an appropriate command to the prompt using either screen, xdotool or xsel in combination with xdotool.

I'm using pcfm less and less now that I have kingbash. I feel really comfortable with it though, but the fact that it had to paste strings is a big problem: if you quickly type "cd", it has to quit after the c and paste it to the prompt, but the d would already be there. And pasting lots of text also only worked up to a limit.

Version 25: outdated http://aur.pastebin.com/85ezNy6p
There are only usage advice changes: http://aur.pastebin.com/diff.php?i=85ezNy6p
The AUR package can wait till version 26.

Last edited by Procyon (2010-08-29 15:58:59)

Offline

#66 2010-08-29 14:16:30

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 1,959
Website

Re: Kingbash - menu driven auto-completion

1) i noticed after downloading the file, I had to run:

sed -i 's/\r//' kingbash

to avoid :

$  kingbash
: No such file or directory
$ vim kingbash # change /usr/bin/env python to /usr/bin/python
$  kingbash
bash: /usr/bin/kingbash: /usr/bin/python^M: bad interpreter: No such file or directory

I really recommend putting this on github. makes it easier to package, and makes it easier for me to contribute.
if you need help with git/github, contact me and I'll help you out.
example of shell scripts installed through AUR/github: http://aur.archlinux.org/packages.php?ID=30071

2) notice the /usr/bin/kingbash

$ kingbash | grep OUTPUT
  OUTPUT=`/usr/bin/kingbash "$(compgen -ab -A function)"`
  READLINE_POINT=`echo "$OUTPUT" | tail -n 1`
  READLINE_LINE=`echo "$OUTPUT" | head -n -1`
  OUTPUT=`/usr/bin/kingbash -r <(tac $HISTFILE)`
  READLINE_POINT=`echo "$OUTPUT" | tail -n 1`
  READLINE_LINE=`echo "$OUTPUT" | head -n -1`
$ which kingbash
/usr/bin/kingbash

3) the xdotool type trick is good enough for me.  thanks.  However, after hitting tab I tried using ^A (readlinel/emacs binding to go to beginning of line). nothing really happened, other then adding a literal '^A' at the end of the string (but you don't see this in kingbash).  if you try to execute this, the command will have a '^A' at the end.  Likewise, if you search in your history afterwards with kingbash, you don't see the '^A' in the specific history entry, but it's there, and will mess things up if you want to execute it again.


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#67 2010-08-29 15:11:45

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

Re: Kingbash - menu driven auto-completion

1. Yes, the carriage returns are a problem. It's a bug in pastebin.com. The PKGBUILD and the Quick Install lines I have been giving also have the sed command you mentioned.

I have used github in the past, but I'm sticking with this because it's just one single text file. I could switch to pocoo too, because it doesn't seem to have the same bug.

2. It's possible that you run something like ./kingbash and argv[0] will be "./kingbash" which isn't appropriate for bashrc functions, so I'll change it to just the filename part of argv[0].

3. I did not add a lot of readline bindings. If a string is not recognized it is pasted as is with only \x1b replaced by "ESC". In this case ^A or \x01 is added to the line (at the position of the cursor).

I will add a binding for ^A and ^E.
^U and Alt+Backspace are already in.
I could also add Shift+Left and Right for moving the cursor around words.
That's about it for the most common ones, right?

Offline

#68 2010-08-29 15:24:06

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 1,959
Website

Re: Kingbash - menu driven auto-completion

Procyon wrote:

1. Yes, the carriage returns are a problem. It's a bug in pastebin.com. The PKGBUILD and the Quick Install lines I have been giving also have the sed command you mentioned.

I have used github in the past, but I'm sticking with this because it's just one single text file. I could switch to pocoo too, because it doesn't seem to have the same bug.

2. It's possible that you run something like ./kingbash and argv[0] will be "./kingbash" which isn't appropriate for bashrc functions, so I'll change it to just the filename part of argv[0].

3. I did not add a lot of readline bindings. If a string is not recognized it is pasted as is with only \x1b replaced by "ESC". In this case ^A or \x01 is added to the line (at the position of the cursor).

I will add a binding for ^A and ^E.
^U and Alt+Backspace are already in.
I could also add Shift+Left and Right for moving the cursor around words.
That's about it for the most common ones, right?

i think so, I always forget the shortcuts other then ^A and ^E, so ... smile
Btw, there is a python module for readline.  I think the module is shipped with python by default.  That's probably worth looking into.

The advantage of github (and version control in general) it's much easier for others to understand all changes and to contribute.
And it can give your tool more visibility, and hence, more contributions/input from others.  Not many people follow this thread (esp not people using other distro's)

If that didn't convince you: you can always use github as a plain replacement for your current pastebin approach.  It doesn't put \r's in downloads .

keep up the good work.


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#69 2010-08-29 16:25:02

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

Re: Kingbash - menu driven auto-completion

Version 26: http://aur.pastebin.com/pBrWRG4F
Quick Install: wget -O- http://aur.pastebin.com/download.php?i=pBrWRG4F | sed 's/\r$//' > kingbash; chmod +x kingbash

Shift Right/Left and Alt Right/Left - move around words (space delimited)
Control A E - move to start/end of line
Usage advice uses only the filename of the name you used to invoke the program instead of the path too

Thanks. If I ever clean up the source I will add it to github and freshmeat or something like that.

For the next version, I'll look into completion of bash variables.

Offline

#70 2010-10-02 19:04:45

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 1,959
Website

Re: Kingbash - menu driven auto-completion

a little extension on the earlier introduced prevention of having multiple times the same line after each other.
i changed the condition in grep_line to become this:

if found and (len(good_list) == 0 or line not in good_list):

this way there are not only no subsequent duplicate lines, there are no duplicate lines in the output at all.
I find it more useful like this, although it makes kingbash a bit slower


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#71 2010-10-03 00:26:16

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

Re: Kingbash - menu driven auto-completion

You can change the calling function too for this. For instance

... < sort -u ~/.bash_history

Or preserving the reverse order where the most recent item comes first:

... < cat -n ~/.bash_history | sort -u -k 2 | sort -nr | cut -f2-


By the way, I've noticed that a directory full of unicode/CJK files in urxvt (and only urxvt) can cause an infinite loop. I'll try to get to the bottom of it.

Offline

#72 2010-10-03 06:33:12

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 1,959
Website

Re: Kingbash - menu driven auto-completion

Procyon wrote:

You can change the calling function too for this. For instance

... < sort -u ~/.bash_history

Or preserving the reverse order where the most recent item comes first:

... < cat -n ~/.bash_history | sort -u -k 2 | sort -nr | cut -f2-

right. I'm using the second variant now.  It suprises me this is actually faster then my python patch.


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#73 2010-10-05 20:19:43

shpelda
Member
Registered: 2008-08-07
Posts: 59

Re: Kingbash - menu driven auto-completion

This is amazingly close to my needs. Thanks.
..
And first bug..
when pressing BACKSPACE at before anything else, "../" is added without checking whether parent exists.
testcase(using kingbash.fn bound to tab):

cd /
cd <tab><hold backspace now>

And second one:
During completion, mistype ";" , try to delete and kingbash dies with exception.
for example cd /tmp/X11/;     <--------- /tmp/X11 filled in by kingbash, ";" typed. Now press backspace, and you are here:

Traceback (most recent call last):
  File "/tmp/kingbash", line 773, in <module>
    main()
  File "/tmp/kingbash", line 264, in main
    if point > first_space and len(prework.split()) > 0 and isrecommended(re.sub(".*[^\\\\][;&|]", "", prework).split()[0], suggestion, dirappend=="/"):
IndexError: list index out of range

Last edited by shpelda (2010-10-05 21:59:06)

Offline

#74 2010-10-06 18:42:12

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

Re: Kingbash - menu driven auto-completion

The infinite loop bug I mentioned earlier is not from an infinite loop, but probably something in urxvt that can't handle the no-line-wrap mode and long utf8 lines. The same command works fine if the terminal window is wider and even if the first command in the python script is print it won't happen.

EDIT:
I confirmed it in a simple way, could someone test it out? I will make a new thread if it's not easily reproducible.
If it freezes the terminal you will have to kill -9 it. Don't run it in client/daemon urxvt because it will freeze the entire daemon.

Without wordwrap should work:
for i in $(seq 100); do echo -n あ; done; echo
With wordwrap will freeze:
echo -e '\e[?7l'
for i in $(seq 100); do echo -n あ; done; echo

@Dieter@be: The fastest would be for kingbash to only try to generate up to what is visible. Or display and continue in the background (complex). I will look into some speed improvements because lag can really get on your nerves.

@shpelda: The ".." is hard coded for speed. I will make it follow the rules in the next release. I don't know what you mean with the test case though.

The second one is a bit weird. It looks like /tmp/X11/ didn't even exist. Usually when you try to complete on a non-existent file it won't do anything, but it won't make that check again later in the program... backspace makes the program stay inside the loop so you can get bugs like that. Anyway I'll add the necessary checks.

Last edited by Procyon (2010-10-06 22:31:00)

Offline

#75 2010-10-06 21:40:58

shpelda
Member
Registered: 2008-08-07
Posts: 59

Re: Kingbash - menu driven auto-completion

/tmp/X11 existed at the time being. Sorry for cryptic example. /etc/X11 would be better i guess.
That IndexError  - I had modified kingbash.
Though I don;t understand why relatively simple extension causes this.

-- elif c == "ESC[D":#LEFT
++ elif c in ("ESC[D","\x08"):#LEFT

Just made ctrl-l behave like right arrow. Works fine. But then this IndexError starts occuring. Any idea why?

Last edited by shpelda (2010-10-06 21:43:07)

Offline

Board footer

Powered by FluxBB