Improve command line

This commit is contained in:
Rico Tiongson 2020-05-02 00:22:13 +08:00
parent a485cb133e
commit c399f3d72b
5 changed files with 229 additions and 85 deletions

View File

@ -1,3 +1,6 @@
v1.2.0 (2020-04-20)
* Simplified source files
v1.1.0 (2020-04-18)
* Add mouse hold gestures
* Add experimental `hold3` configuration

View File

@ -75,7 +75,6 @@ Comfortable, seamless, and fast 3-finger (and 4-finger) touchpad swipe gestures
```
comfortable-swipe start
comfortable-swipe stop
comfortable-swipe restart
comfortable-swipe status
```
@ -95,35 +94,95 @@ Comfortable, seamless, and fast 3-finger (and 4-finger) touchpad swipe gestures
1. Configurations
```bash
# configuration value
comfortable-swipe config list
comfortable-swipe config get <KEY>
comfortable-swipe config set <KEY> [=] <VALUE>
# list all configurations
comfortable-swipe config list [<pattern>]
# remove configuration value
comfortable-swipe config delete <KEY>
```
1. Configuration extras
Get config:
```bash
# show all possible configuration keys
comfortable-swipe config keys
# show path to config
comfortable-swipe config path
# open config file with editor
comfortable-swipe config edit
$> comfortable-swipe config get left3
ctrl+alt+Right
```
1. (Advanced) Run program buffer
Set config (whitespace ignored):
```bash
$> comfortable-swipe config set up4 super+Up
up4 = super+Up
$> comfortable-swipe config set left3 = super+Right
left3 = super+Right
$> comfortable-swipe config set right3 = super + Left + alt
right3 = super+Left+alt
```
Delete config:
```bash
$> comfortable-swipe config delete mouse3
```
List all cofigurations
```bash
$> comfortable-swipe config list
threshold = 1.0
left4 = ctrl+super+shift+Right
right3 = ctrl+super+Left
right4 = ctrl+super+shift+Left
up3 = ctrl+F12
up4 = super+d
down3 = ctrl+F12
down4 = super+d
mouse4 = button1
left3 = ctrl+super+Right
```
Show all possible keys:
```bash
$> comfortable-swipe config keys
left3
left4
right3
right4
up3
up4
down3
down4
threshold
mouse3
mouse4
```
Show path to config file:
```bash
$> comfortable-swipe config path
/home/hikari9/.config/comfortable-swipe.conf
```
1. (Advanced) Invoke program buffer with `comfortable-swipe buffer`
```bash
./my-custom-runner | comfortable-swipe buffer
cat <<"EOF" | comfortable-swipe buffer
-event7 GESTURE_SWIPE_BEGIN +0.45s 3
event7 GESTURE_SWIPE_UPDATE +0.45s 3 8.81/-2.34 (32.48/-8.62 unaccelerated)
event7 GESTURE_SWIPE_UPDATE +0.46s 3 15.54/-3.88 (41.99/-10.50 unaccelerated)
event7 GESTURE_SWIPE_UPDATE +0.46s 3 18.09/-4.16 (48.88/-11.25 unaccelerated)
event7 GESTURE_SWIPE_UPDATE +0.47s 3 19.67/-3.61 (53.15/-9.75 unaccelerated)
event7 GESTURE_SWIPE_UPDATE +0.48s 3 19.79/-3.19 (53.48/-8.62 unaccelerated)
event7 GESTURE_SWIPE_UPDATE +0.49s 3 17.72/-2.36 (47.90/-6.37 unaccelerated)
event7 GESTURE_SWIPE_UPDATE +0.49s 3 15.66/-2.36 (42.32/-6.37 unaccelerated)
event7 GESTURE_SWIPE_UPDATE +0.50s 3 13.35/-2.08 (36.09/-5.62 unaccelerated)
event7 GESTURE_SWIPE_UPDATE +0.51s 3 10.80/-1.94 (29.20/-5.25 unaccelerated)
event7 GESTURE_SWIPE_UPDATE +0.52s 3 8.38/-1.53 (22.64/-4.12 unaccelerated)
event7 GESTURE_SWIPE_UPDATE +0.53s 3 6.43/-1.25 (17.39/-3.37 unaccelerated)
event7 GESTURE_SWIPE_UPDATE +0.53s 3 4.86/-0.83 (13.12/-2.25 unaccelerated)
event7 GESTURE_SWIPE_UPDATE +0.54s 3 3.76/-0.55 (10.17/-1.50 unaccelerated)
event7 GESTURE_SWIPE_UPDATE +0.55s 3 2.67/-0.28 ( 7.22/-0.75 unaccelerated)
event7 GESTURE_SWIPE_UPDATE +0.56s 3 1.70/ 0.28 ( 4.59/ 0.75 unaccelerated)
event7 GESTURE_SWIPE_UPDATE +0.56s 3 1.09/ 0.14 ( 2.95/ 0.37 unaccelerated)
event7 GESTURE_SWIPE_END +0.59s 3
EOF
```
## Configuring Swipe Gestures

View File

@ -11,9 +11,13 @@ KEYS="left3|left4|right3|right4|up3|up4|down3|down4|threshold|mouse3|mouse4"
DEPRECATED="hold3|hold4"
#########
# USAGE #
#########
function usage {
cat <<EOF
Usage: $BASENAME [--help|--version] [start|stop|restart|config|autostart|buffer|debug|status] [<args>]
Usage: $BASENAME [--help|--version] [start|stop|config|autostart|buffer|debug|status] [<args>]
Configuration:
@ -33,25 +37,23 @@ Global Options:
-v, --version print the program version
Commands:
start
starts 3/4-finger gesture service
start [--attach]
starts 3/4-finger gesture service in a detached process
flag '--attach' to attach the output in the console
stop
stops 3/4-finger gesture service
restart
restarts 3/4-finger gesture service
edit
edits the configuration file with gedit
autostart <[on|off|toggle|status|path]>
autostart [on|off|toggle|status|path]
toggle to automatically run on startup automatically run on startup (toggleable)
buffer
parses output of libinput debug-events
config <[get|set|delete|list|properties|path]> [...]
config [get|set|delete|list|properties|path] [...]
shows the location of the config file
debug
@ -65,17 +67,67 @@ EOF
}
####################
# COMMAND OPTIONS #
####################
for i in "$@"; do
case $i in
-h | --help) # eagerly show help
help
exit 0
;;
-v | --version) # eagerly print version
echo "comfortable-swipe $VERSION"
exit 0
;;
--attach)
if [[ "$1" != start ]]; then
echo "Unknown option: $i" >&2
exit 1
fi
;;
*)
# unknown option
if [[ "$i" == -* ]]; then
echo "Unknown option: $i" >&2
exit 1
fi
;;
esac
done
#########
# START #
#########
# start comfortable-swipe
# internally pipes debug text to the buffer
function start {
stop > /dev/null 2>&1
if [[ "$1" == "--attach" ]]; then
# attach the buffered output
debug | buffer
else
# detach buffered output
nohup "$BASENAME" debug </dev/null 2>&1 | "$BASENAME" buffer >/dev/null 2>&1 &
echo "Comfortable swipe is RUNNING in the background"
fi
}
########
# STOP #
########
# stop running comfortable-swipe commands (except self)
function stop {
function stop_prop {
pgrep -f "${1:?}" | fgrep -v $$ | xargs kill --
pgrep -f "${1:?}" | fgrep -v $$ | xargs -I{} kill -- "{}"
}
stop_prop "$BASENAME"
stop_prop "$(which comfortable-swipe)"
@ -83,26 +135,29 @@ function stop {
}
# restart comfortable swipe
function restart {
stop > /dev/null 2>&1
# restart the server in the background
nohup "$BASENAME" start 0<&- &>/dev/null &
}
#########
# DEBUG #
#########
# show debug text
# internally just calls libinput debug-events
function debug {
if command -v libinput-debug-events > /dev/null 2>&1; then
local DEBUGEVENTS="libinput-debug-events"
# compat for old Ubuntu
local COMMAND="libinput-debug-events"
else
local DEBUGEVENTS="libinput debug-events"
local COMMAND="libinput debug-events"
fi
stdbuf -oL -e0 $DEBUGEVENTS 2> >(fgrep -v 'double tracking')
stdbuf -oL -e0 $COMMAND 2> >(fgrep -v 'double tracking')
}
##########
# BUFFER #
##########
# parse input from a buffer
# internally calls comfortable-swipe-main.cpp, which is
# installed as: /usr/local/bin/ comfortable-swipe-buffer
@ -111,6 +166,11 @@ function buffer {
}
##########
# STATUS #
##########
# verbosely show comfortable-swipe status
function status {
# TODO: show configuration status as well
@ -122,22 +182,30 @@ function status {
fi
}
##########
# CONFIG #
##########
# get location of configuration file
function config {
local CONFIG="$HOME/.config/comfortable-swipe.conf"
############
# show usage
function usage {
echo "Usage: $BASENAME config [get|set|delete|list|keys|edit|path]..."
echo "Usage: $BASENAME config [get|set|delete|list|keys|edit|path] ..."
}
#####################
# show path to config
function path {
echo "$CONFIG"
}
# eit configuration with gedit
function edit {
gedit "$CONFIG"
}
# get all keys from config, without comments
# and show only last among duplicates
##############################################
# list config, show only last among duplicates
function list {
# dispatch subcommands
if [[ $# -eq 0 ]]; then
@ -159,16 +227,18 @@ function config {
list | egrep "($pattern)[^=]* ="
fi
}
############################################
# get a list of all valid keys, one per line
function keys {
echo "$KEYS" | sed 's/|/\n/g'
}
##################################
# Get a specific key in the config
# Usage: comfortable-swipe config get <PROP>
function get {
# helper function to print usage
function usage {
echo "Usage: $BASENAME config get <PROP>"
echo "Usage: $BASENAME config get <PROPERTY>"
echo -n "Props: "
echo "$KEYS" | sed 's/|/, /g'
}
@ -180,20 +250,22 @@ function config {
local KEY="$1"
# check if key is valid
if ! [[ "$KEY" =~ ^($KEYS|$DEPRECATED)$ ]]; then
echo "'$KEY' is an invalid property" >&2
echo >&2
# echo "'$KEY' is an invalid property" >&2
# echo >&2
usage >&2
echo >&2
echo "If you want to filter properties, you can instead try:" >&2
echo "If you want to list down properties, you can instead try:" >&2
echo >&2
echo " $BASENAME config list $@" >&2
echo " $BASENAME config list" >&2
echo >&2
exit 1
fi
# get key from config file
list | fgrep -m1 "$KEY =" | sed -E "s/^$KEY = //"
}
# Deletes a key in the config
############################
# delete a key in the config
function delete {
# helper function to print usage
function usage {
@ -217,17 +289,23 @@ function config {
local RESULT="$(egrep -v "^\\s*($(echo "$DELETE" | awk '{print $1}' | paste -s -d '|'))\\s*=" "$CONFIG")"
echo "$RESULT" > "$CONFIG"
# restart comfortable-swipe
restart
start
fi
}
# Set a specific key in the config
# Usage: comfortable-swipe config get <key>
##################################
# set a specific key in the config
function set {
# helper function to print usage
function usage {
echo "Usage: $BASENAME config set <PROPERTY> [=] <VALUE>"
echo -n "Valid properties: "
echo "$KEYS" | sed 's/|/, /g'
echo
echo "Example:"
echo
echo " $BASENAME config set left3 = ctrl+super+Right"
echo
}
# no next argument: show help
if [[ $# -eq 0 ]]; then
@ -294,12 +372,18 @@ function config {
fi
# show newly set value
echo "$KEY = $(get "$KEY")"
restart
start
}
######################
# dispatch subcommands
if [[ $# -eq 0 ]]; then
# no options; just show usage
usage
echo "Try:"
echo
echo " $BASENAME config list"
echo
elif declare -f "$1" >/dev/null 2>&1; then
# invoke subcommand function, passing arguments through
# TODO: unset all nonlocal functions
@ -311,17 +395,23 @@ function config {
fi
}
#############
# AUTOSTART #
#############
# enable or disable autostart
# you can also set manually by running: gnome-session-properties
function autostart {
# path to autostart files
local AUTOSTART="$HOME/.config/autostart/comfortable-swipe.desktop"
local ENABLED="X-GNOME-Autostart-enabled"
##########################
# show autostart file path
function path {
echo "$AUTOSTART"
}
##################################################
# echo autostart status: ON, OFF, MISSING, INVALID
function status {
if [[ ! -f "$AUTOSTART" ]]; then
@ -334,20 +424,28 @@ function autostart {
echo "INVALID"
fi
}
##################
# enable autostart
function on {
sed -i "s/$ENABLED=false/$ENABLED=true/" "$AUTOSTART"
echo "Autostart switched on"
}
###################
# disable autostart
function off {
sed -i "s/$ENABLED=true/$ENABLED=false/" "$AUTOSTART"
echo "Autostart switched off"
}
#####################################
# toggle to opposite autostart status
function toggle {
[[ $(status) = ON ]] && off || on
}
######################
# dispatch subcommands
if [[ $# -eq 0 ]]; then
# default behavior is to toggle
@ -364,27 +462,10 @@ function autostart {
}
####################
# COMMAND OPTIONS #
####################
for i in "$@"; do
case $i in
-h | --help) # eagerly show help
help
exit 0
;;
-v | --version) # eagerly print version
echo "comfortable-swipe $VERSION"
exit 0
;;
*) # unknown option
if [[ "$i" == -* ]]; then
echo "Unknown option: $i" >&2
exit 1
fi
;;
esac
done
############
# DISPATCH #
############
# dispatch subcommands
if [[ $# -eq 0 ]]; then
@ -396,8 +477,8 @@ elif declare -f "$1" >/dev/null 2>&1; then
"$@" # same as "$1" "$2" "$3" ... for full argument list
else
function abort {
echo "Error: $1 not recognized" >&2
echo >&2
# echo "Error: $1 not recognized" >&2
# echo >&2
usage >&2
echo -n "Valid props: " >&2
echo "$KEYS" | sed 's/|/, /g' >&2
@ -408,6 +489,6 @@ else
"$BASENAME" config get "$1" || abort
else
# multiple arguments, use shorthand set
config set "$@" 2> /dev/null || abort
"$BASENAME" config set "$@" 2> /dev/null || abort
fi
fi

View File

@ -60,7 +60,6 @@ int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.flush();
// get configuration file
map<string, string> config;
if (ini_parse(COMFORTABLE_SWIPE_CONFIG, parse_config, &config) < 0) {

View File

@ -175,5 +175,7 @@ cat <<EOF
Successfully installed comfortable-swipe $VERSION
Autostart switched $("$TARGET" autostart status)
Try running: comfortable-swipe start
EOF
# start the program
"$TARGET" start