BREAKING: Add ability to limit recent emoji shown (#1)

Instead of `-P` being a flag for showing history it has become an option
whose argument is the number of history entries to show, `-P 4`.

To mimic the old behavior use `-P 0`.

Added some test coverage for history functionality.

Fixes #1.
This commit is contained in:
Marty Oehme 2023-04-30 18:16:26 +02:00
parent 6b39e5e05a
commit 0aaca26b0e
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A
4 changed files with 99 additions and 21 deletions

View file

@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- Pass through return code 1 from selection tool - Pass through return code 1 from selection tool
- (!) Number of displayed recent emoji can be set with `-P` option. This replaces previous `-P`
history flag toggle. Use number to set amount of recent entries to display, `-P3`. To
completely hide history use `-P0`
### Changed ### Changed

View file

@ -118,10 +118,10 @@ By default, bemoji will sort the list it displays by your most frequently and mo
To disable this behavior, execute bemoji like the following: To disable this behavior, execute bemoji like the following:
```bash ```bash
bemoji -P bemoji -P 0
``` ```
This will stop bemoji from re-ordering your emoji lists before displaying them. This will stop bemoji from adding recently used emoji before displaying the list.
You can also stop bemoji from adding any emoji to your history in the first place: You can also stop bemoji from adding any emoji to your history in the first place:
@ -133,20 +133,30 @@ This will not add any of the emoji you pick to your recent emojis.
Put both together to completely ignore the recent emoji feature of the program: Put both together to completely ignore the recent emoji feature of the program:
```bash ```bash
bemoji -Pp bemoji -p -P0
``` ```
Like this, you'll be hiding any recent personal emoji and no one will know that you always type 👄🍆💦. Like this, you'll be hiding any recent personal emoji and no one will know that you always type 👄🍆💦.
To limit the number of your recently used emoji that are shown without hiding them completely simply increase the number to however many you wish to display.
For example, to display only the top 4 recently used emoji:
```bash
bemoji -P 4
```
The recent list will also contain emoji that are *not* usually on your lists, The recent list will also contain emoji that are *not* usually on your lists,
so kept in single-use lists for example. so kept in single-use lists for example.
If you don't wish those to show up, make use of these options. If you don't wish those to show up, make use of these options.
### Setting custom directories ### Setting custom directories and editing history
By default bemoji stores your recent history in `$XDG_STATE_HOME/bemoji-history.txt`, By default bemoji stores your recent history in `$XDG_STATE_HOME/bemoji-history.txt`,
so most often in `~/.local/state/bemoji-history.txt` so most often in `~/.local/state/bemoji-history.txt`
You can edit this file in any text editor to change your recent history,
removing, adding or changing the emoji appearing there.
You can overwrite the directories bemoji uses for its emoji lists and history files with the following two environment variables: You can overwrite the directories bemoji uses for its emoji lists and history files with the following two environment variables:
``` ```
@ -248,16 +258,16 @@ What follows is a list of all environment variables bemoji understands,
with their default settings with their default settings
```bash ```bash
BEMOJI_DB_LOCATION=$XDG_DATA_HOME/bemoji # where the emoji lists reside BEMOJI_DB_LOCATION="$XDG_DATA_HOME/bemoji" # where the emoji lists reside
BEMOJI_HISTORY_LOCATION=$XDG_STATE_HOME # where the state file resides BEMOJI_HISTORY_LOCATION="$XDG_STATE_HOME" # where the state file resides
BEMOJI_CUSTOM_LIST="" # the custom emoji list to display BEMOJI_CUSTOM_LIST="" # the custom emoji list to display
BEMOJI_DOWNLOAD_LIST="" # the default emoji lists to download to database BEMOJI_DOWNLOAD_LIST="" # the default emoji lists to download to database
BEMOJI_DEFAULT_COMMAND=<clip-tool> # which command to invoke by default BEMOJI_DEFAULT_COMMAND="" # which command to invoke by default
BEMOJI_PICKER_CMD=bemenu # which picker tool to use BEMOJI_PICKER_CMD="bemenu" # which picker tool to use
BEMOJI_CLIP_CMD=wl-copy # which clipboard tool to use BEMOJI_CLIP_CMD="wl-copy" # which clipboard tool to use
BEMOJI_TYPE_CMD=wtype # which typing tool to use (ydotool will NOT work) BEMOJI_TYPE_CMD="wtype" # which typing tool to use (ydotool will NOT work)
BEMOJI_PRIVATE_MODE=false # whether to save new entries BEMOJI_PRIVATE_MODE=false # whether to save new entries
BEMOJI_IGNORE_RECENT=false # whether to display recent entries BEMOJI_LIMIT_RECENT="" # whether to display recent entries
BEMOJI_ECHO_NEWLINE=true # whether to end the output with a newline character BEMOJI_ECHO_NEWLINE=true # whether to end the output with a newline character
``` ```

28
bemoji
View file

@ -18,7 +18,7 @@ bm_echo_newline=${BEMOJI_ECHO_NEWLINE:-true}
# Do not save choices # Do not save choices
bm_private_mode=${BEMOJI_PRIVATE_MODE:-false} bm_private_mode=${BEMOJI_PRIVATE_MODE:-false}
# Do not sort results # Do not sort results
bm_ignore_recent=${BEMOJI_IGNORE_RECENT:-false} bm_limit_recent="$BEMOJI_LIMIT_RECENT"
# Report usage # Report usage
usage() { usage() {
@ -35,7 +35,7 @@ usage() {
echo " Other options:" echo " Other options:"
echo " -n Do not print a newline after the picked emoji." echo " -n Do not print a newline after the picked emoji."
echo " -p Do not save picked emoji to recent history." echo " -p Do not save picked emoji to recent history."
echo " -P Do not order emoji by recently used." echo " -P <number> Limit number of recent emoji to display."
echo " -D <choice> Choose from default lists to download." echo " -D <choice> Choose from default lists to download."
echo " Valid choices: all|none|emoji|math (multiple choices possible)." echo " Valid choices: all|none|emoji|math (multiple choices possible)."
echo " -f <filepath> Use a custom emoji database. Can be a url which will be retrieved." echo " -f <filepath> Use a custom emoji database. Can be a url which will be retrieved."
@ -51,7 +51,7 @@ version() {
} }
# Get Options # Get Options
while getopts ":f:D:tcenpPhv" o; do while getopts ":f:D:tcenpP:hv" o; do
case "${o}" in case "${o}" in
f) BEMOJI_CUSTOM_LIST="${OPTARG}" ;; f) BEMOJI_CUSTOM_LIST="${OPTARG}" ;;
t) bm_cmds+=(_typer) ;; t) bm_cmds+=(_typer) ;;
@ -60,7 +60,7 @@ while getopts ":f:D:tcenpPhv" o; do
n) bm_echo_newline=false ;; n) bm_echo_newline=false ;;
D) BEMOJI_DOWNLOAD_LIST="${OPTARG}" ;; D) BEMOJI_DOWNLOAD_LIST="${OPTARG}" ;;
p) bm_private_mode=true ;; p) bm_private_mode=true ;;
P) bm_ignore_recent=true ;; P) bm_limit_recent="${OPTARG}" ;;
h) usage 0 ;; h) usage 0 ;;
v) version ;; v) version ;;
*) usage 1 ;; *) usage 1 ;;
@ -117,25 +117,33 @@ gather_emojis() {
result=$(cat "$bm_db_location"/*.txt) result=$(cat "$bm_db_location"/*.txt)
fi fi
if [ "$bm_ignore_recent" = true ]; then if [ -n "$bm_limit_recent" ] && [ "$bm_limit_recent" -eq 0 ]; then
printf "%s" "$result" printf "%s" "$result"
else return
printf "%s\n%s" "$(get_most_recent)" "$result" | cat -n - | sort -uk2 | sort -n | cut -f2-
fi fi
printf "%s\n%s" "$(get_most_recent "$bm_limit_recent")" "$result" | cat -n - | sort -uk2 | sort -n | cut -f2-
} }
get_most_recent() { get_most_recent() {
limit=${1}
recent_file="$bm_history_file" recent_file="$bm_history_file"
if [ ! -f "$recent_file" ]; then if [ ! -f "$recent_file" ]; then
touch "$recent_file" touch "$recent_file"
fi fi
# TODO improve this messy line # TODO improve this messy line
sed -e '/^$/d' "$recent_file" | local result
result=$(sed -e '/^$/d' "$recent_file" |
sort | sort |
uniq -c | uniq -c |
sort -rn | sort -k1rn |
sed -e 's/^\s*//' | sed -e 's/^\s*//' |
cut -d' ' -f2- cut -d' ' -f2-)
if [ -z "$limit" ]; then
echo "$result"
else
echo "$result" | head -n "$limit"
fi
} }
add_to_recent() { add_to_recent() {

57
test/bemoji_history.bats Normal file
View file

@ -0,0 +1,57 @@
#!/usr/bin/env bash
setup_file() {
# make bemoji executable from anywhere relative to current testfile
TEST_DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )"
PATH="$TEST_DIR/..:$PATH"
}
setup() {
load 'test_helper/bats-support/load'
load 'test_helper/bats-assert/load'
# mock out interactive picker for static emoji return
export BEMOJI_PICKER_CMD="echo ❤️"
# set up small default set of test emoji for each test
export BEMOJI_DB_LOCATION="$BATS_TEST_TMPDIR/database"
export BEMOJI_HISTORY_LOCATION="$BATS_TEST_TMPDIR/history"
mkdir -p "$BEMOJI_DB_LOCATION" "$BEMOJI_HISTORY_LOCATION"
cat "$BATS_TEST_DIRNAME/resources/test_emoji.txt" > "$BEMOJI_DB_LOCATION/emoji.txt"
}
@test "sorts by frecency" {
echo -e "there\nhello\nhello" > "$BEMOJI_HISTORY_LOCATION/bemoji-history.txt"
echo -e "database" > "$BEMOJI_DB_LOCATION/emoji.txt"
BEMOJI_CLIP_CMD="cat -" BEMOJI_PICKER_CMD="cat -" run bemoji 3>&-
assert_output "hellotheredatabase"
}
@test "history limiting uses sorted results" {
echo -e "zany\nmy\nisee\nonomatopeia" > "$BEMOJI_HISTORY_LOCATION/bemoji-history.txt"
echo -e "database" > "$BEMOJI_DB_LOCATION/emoji.txt"
BEMOJI_CLIP_CMD="cat -" BEMOJI_PICKER_CMD="cat -" run bemoji -P 1 3>&-
assert_output "iseedatabase"
}
@test "history limiting takes frecency into account" {
echo -e "there\nfriend\nhello\nhello" > "$BEMOJI_HISTORY_LOCATION/bemoji-history.txt"
echo -e "database" > "$BEMOJI_DB_LOCATION/emoji.txt"
BEMOJI_CLIP_CMD="cat -" BEMOJI_PICKER_CMD="cat -" run bemoji -P 1 3>&-
assert_output "hellodatabase"
}
@test "-P 0 disables history" {
echo -e "there\nfriend\nhello\nhello" > "$BEMOJI_HISTORY_LOCATION/bemoji-history.txt"
echo -e "database" > "$BEMOJI_DB_LOCATION/emoji.txt"
BEMOJI_CLIP_CMD="cat -" BEMOJI_PICKER_CMD="cat -" run bemoji -P 0 3>&-
assert_output "database"
}
@test "BEMOJI_LIMIT_RECENT=0 disables history" {
echo -e "there\nfriend\nhello\nhello" > "$BEMOJI_HISTORY_LOCATION/bemoji-history.txt"
echo -e "database" > "$BEMOJI_DB_LOCATION/emoji.txt"
BEMOJI_LIMIT_RECENT=0 BEMOJI_CLIP_CMD="cat -" BEMOJI_PICKER_CMD="cat -" run bemoji 3>&-
assert_output "database"
}