Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #77 from dylanaraps/special
Browse files Browse the repository at this point in the history
Handle special keys properly
  • Loading branch information
dylanaraps authored Jan 28, 2019
2 parents 21ed137 + f8c8ba2 commit 9dfd1be
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
25 changes: 6 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ This is the list of full keybindings along with their default values. You only n

# Go to child directory.
export FFF_KEY_CHILD1="l"
export FFF_KEY_CHILD2="C" # Right Arrow
export FFF_KEY_CHILD3="" # Enter / Return
export FFF_KEY_CHILD2=$'\e[C' # Right Arrow
export FFF_KEY_CHILD3="" # Enter / Return

# Go to parent directory.
export FFF_KEY_PARENT1="h"
export FFF_KEY_PARENT2="D" # Left Arrow
export FFF_KEY_PARENT2=$'\e[D' # Left Arrow
export FFF_KEY_PARENT3=$'\177' # Backspace
export FFF_KEY_PARENT4=$'\b' # Backspace (Older terminals)

Expand All @@ -210,11 +210,11 @@ export FFF_KEY_SHELL="s"

# Scroll down.
export FFF_KEY_SCROLL_DOWN1="j"
export FFF_KEY_SCROLL_DOWN2="B" # Down Arrow
export FFF_KEY_SCROLL_DOWN2=$'\e[B' # Down Arrow

# Scroll up.
export FFF_KEY_SCROLL_UP1="k"
export FFF_KEY_SCROLL_UP2="A" # Up Arrow
export FFF_KEY_SCROLL_UP2=$'\e[A' # Up Arrow

# Go to top and bottom.
export FFF_KEY_TO_TOP="g"
Expand Down Expand Up @@ -270,19 +270,6 @@ When rebinding a key in `fff` make sure you don't have two bindings with the sam

### How to figure out special keys.

You can verify the value of a keypress with the following command:

```sh
# Run this and press the desired key.
read -rn 1

# Left Arrow was pressed which is an escape sequence ('^[[D' or '\e[D']).
# 'read -n 1' uses the last character of escape sequences.
# Therefore the key-binding for the left arrow is 'D'.
read -rn 1
^[[D➜ [D
```
Below is a tiny script I've written which will tell you the exact value to use. It automates the deciphering of special key escape sequences to the exact value `fff` needs. Save this to a file and run it. Give it a key-press and it'll spit out the exact value needed.

```sh
Expand All @@ -298,7 +285,7 @@ key() {
# Escape Sequences.
$'\e')
read -rsn 2
printf '%s\n' "key: ${REPLY: -1}"
printf '%s %q\n' "key:" "${1}${REPLY}"
;;

# Return / Enter.
Expand Down
16 changes: 11 additions & 5 deletions fff
Original file line number Diff line number Diff line change
Expand Up @@ -579,12 +579,18 @@ cmd_line() {
}

key() {
case "$1" in
# Handle special key presses.
[[ $1 == $'\e' ]] && {
read "${read_flags[@]}" -rsn 2
local special_key="${1}${REPLY}"
}

case "${special_key:-$1}" in
# Open list item.
# 'C' is what bash sees when the right arrow is pressed ('\e[C').
# '' is what bash sees when the enter/return key is pressed.
"${FFF_KEY_CHILD1:=l}"|\
"${FFF_KEY_CHILD2:=C}"|\
"${FFF_KEY_CHILD2:=$'\e[C'}"|\
"${FFF_KEY_CHILD3:=""}")
open "${list[scroll]}"
;;
Expand All @@ -594,7 +600,7 @@ key() {
# '\177' and '\b' are what bash sometimes sees when the backspace
# key is pressed.
"${FFF_KEY_PARENT1:=h}"|\
"${FFF_KEY_PARENT2:=D}"|\
"${FFF_KEY_PARENT2:=$'\e[D'}"|\
"${FFF_KEY_PARENT3:=$'\177'}"|\
"${FFF_KEY_PARENT4:=$'\b'}")
# If a search was done, clear the results and open the current dir.
Expand All @@ -611,7 +617,7 @@ key() {
# Scroll down.
# 'B' is what bash sees when the down arrow is pressed ('\e[B').
"${FFF_KEY_SCROLL_DOWN1:=j}"|\
"${FFF_KEY_SCROLL_DOWN2:=B}")
"${FFF_KEY_SCROLL_DOWN2:=$'\e[B'}")
((scroll < list_total)) && {
((scroll++))
((y < max_items )) && ((y++))
Expand All @@ -626,7 +632,7 @@ key() {
# Scroll up.
# 'A' is what bash sees when the up arrow is pressed ('\e[A').
"${FFF_KEY_SCROLL_UP1:=k}"|\
"${FFF_KEY_SCROLL_UP2:=A}")
"${FFF_KEY_SCROLL_UP2:=$'\e[A'}")
# '\e[1L': Insert a line above the cursor.
# '\e[A': Move cursor up a line.
((scroll > 0)) && {
Expand Down
16 changes: 8 additions & 8 deletions fff.1
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,17 @@ For more information see:

# Go to child directory.
export FFF_KEY_CHILD1="l"
export FFF_KEY_CHILD2="C" # Right Arrow
export FFF_KEY_CHILD3="" # Enter / Return
export FFF_KEY_CHILD2=$'\e[C' # Right Arrow
export FFF_KEY_CHILD3="" # Enter / Return

# Go to parent directory.
export FFF_KEY_PARENT1="h"
export FFF_KEY_PARENT2="D" # Left Arrow
export FFF_KEY_PARENT3=$'\\177' # Backspace
export FFF_KEY_PARENT4=$'\\b' # Backspace (Older terminals)
export FFF_KEY_PARENT2=$'\e[D' # Left Arrow
export FFF_KEY_PARENT3=$'\177' # Backspace
export FFF_KEY_PARENT4=$'\b' # Backspace (Older terminals)

# Go to previous directory.
export FFF_KEY_PREVIOUS="\-"
export FFF_KEY_PREVIOUS="-"

# Search.
export FFF_KEY_SEARCH="/"
Expand All @@ -142,11 +142,11 @@ export FFF_KEY_SHELL="s"

# Scroll down.
export FFF_KEY_SCROLL_DOWN1="j"
export FFF_KEY_SCROLL_DOWN2="B" # Down Arrow
export FFF_KEY_SCROLL_DOWN2=$'\e[B' # Down Arrow

# Scroll up.
export FFF_KEY_SCROLL_UP1="k"
export FFF_KEY_SCROLL_UP2="A" # Up Arrow
export FFF_KEY_SCROLL_UP2=$'\e[A' # Up Arrow

# Go to top and bottom.
export FFF_KEY_TO_TOP="g"
Expand Down

0 comments on commit 9dfd1be

Please sign in to comment.