paulmars / realiaser

Realiaser is a game which helps you memorize your shell aliases.
MIT License
103 stars 1 forks source link

Prompt function does not allow for history format #1

Open jalanb opened 11 years ago

jalanb commented 11 years ago

On my machine the given method to extract the last command does not work:

$ echo $0
-bash
$ bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11)
Copyright (C) 2007 Free Software Foundation, Inc.
$ history -1 | cut -d ' ' -f 3-20 | realiaser
-bash: history: -1: invalid option
history: usage: history [-c] [-d offset] [n] or history -awrn [filename] or history -ps arg [arg...]
1

Even if I were to work around that, the cut command is too simplistic, because it does not consider $HISTTIMEFORMAT:

$ echo $HISTTIMEFORMAT
%h/%d - %H:%M:%S
$ history | tail -n 2 | head -n 1 | cut -d ' ' -f 3-20
 Dec/03 - 23:47:13echo $HISTTIMEFORMAT

OK, let's turn that off

$ unset HISTTIMEFORMAT

Now I wonder about the cut command starting at field 3:

$ history | tail -n 2 | head -n 1 | cut -d ' ' -f 3-20
   981  unset HISTTIMEFORMAT

Those extra spaces before the command number (981) do seem to make the third field less than helpful. I need the 5th instead:

$ cd ..
$ history | tail -n 2 | head -n 1 | cut -d ' ' -f 5-20
cd ..

And that leaves the whole function as

function last_command() {
  local old_format=$HISTTIMEFORMAT
  unset HISTTIMEFORMAT
  echo $(history | head -n 2 | tail -n 1 | cut -d ' ' -f 5-20 | realiaser)
  export HISTTIMEFORMAT=$old_format
}

Which works for me, but YMMV as you may be on Linux or Solaris or ... some other variety of Unix

paulmars commented 11 years ago

Ah yes. I'm using ZSH and the method of getting history is different. I should note that in the Readme.

Nice work.

kid-icarus commented 11 years ago

So I'm running ZSH in iTerm2, and by default last_command() wasn't working as expected. I used the follwing:

function last_command() {
  echo $(history | tail -n 1 | cut -d ' ' -f 2-20 | realiaser)
}
paulmars commented 11 years ago

Here is the expected output.

~ ls -l 1440 [ABRIDGED] ~ history | tail -n 1 ll (1390) 37247 ls -l ~

paulmars commented 11 years ago

~ history | tail -n 1 | cut -d ' ' -f 2-20 ll (1342) ls -l

paulmars commented 11 years ago

I've found that part of the configuration to be very system dependent. I'm using ZSH / OSX. Newest version afaik.