Skip to content

Commit

Permalink
Minor improvements for CLI parameter handling, debug message handling…
Browse files Browse the repository at this point in the history
… and only find monitor-config records when we're placing windows based on the display resolution not a CLI parameter.
  • Loading branch information
maxwax committed Apr 4, 2023
1 parent 8c965f7 commit 2b98036
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 44 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# CHANGELOG


### v1.4

* Replace if/then blocks for debug statements with debug_text function to provide the same functionality. The code is simpler and cleaner this way.

* Default DEBUG to false

* Add CLI handling for command line parameters -d and -h

* If we are using the resolution to find a monitor-config rule and the monitor-config rule to then find window placement rules, call find_resolution and find_monitor_config. Otherwise, don't call either of these as we don't need them and can just use window placement rules.

### v.1.3.1

Error out if we can't find a monitor-config rule based on H x V or just H resolutions. Fixes a case where the script just hands when a monitor-config is not found.
Expand Down
98 changes: 54 additions & 44 deletions position
Original file line number Diff line number Diff line change
Expand Up @@ -39,61 +39,50 @@ function usage {
available_display_configs
}

function debug_text {
if [[ ! -z $DEBUG ]]
then
echo "$*"
fi
}

function get_resolution {

RESOLUTION=$(xdpyinfo | grep dimensions | awk '{print $2}')

# Remove vertical resolution 'x1200'
HORIZONTAL_RESOLUTION=${RESOLUTION%x*}

if [[ ! -z $DEBUG ]]
then
echo "Resolution: $RESOLUTION, Horizontal Resolution $HORIZONTAL_RESOLUTION"
fi
debug_text "Resolution: $RESOLUTION, Horizontal Resolution $HORIZONTAL_RESOLUTION"
}

function scan_for_monitor_config {
# $1 is an exact horizontal x vertical resolution or horizontal x 9999 string

if [[ ! -z $DEBUG ]]
then
echo "scan_for_monitor_config Looking for |${1}|"
fi
debug_text "scan_for_monitor_config Looking for |${1}|"

DISPLAY_LINE_COUNT=$(grep '^monitor-config:' $CONFIG_FILE | egrep -v "^#" | grep ${1}| wc -l)

if [[ $DISPLAY_LINE_COUNT -gt 0 ]]
then
DISPLAY_LINE=$(grep '^monitor-config:' $CONFIG_FILE | egrep -v "^#" | grep ${1})
if [[ ! -z $DEBUG ]]
then
echo "Monitor config is |$DISPLAY_LINE|"
fi
debug_text "Monitor config is |$DISPLAY_LINE|"

POSITION_RULE=$(echo $DISPLAY_LINE | awk '{print $2}')
if [[ ! -z $DEBUG ]]
then
echo "Position rule is |$POSITION_RULE|"
fi
debug_text "Position rule is |$POSITION_RULE|"
fi
}

# Using display resolution, try to find a monitor-config line for the resolution
function find_monitor_config {
POSITION_RULE=""
# Try getting exact horizontal x vertical resolution config
if [[ ! -z $DEBUG ]]
then
echo "Looking for monitor-config match horizontal x vertical resolution |$RESOLUTION|"
fi
debug_text "Looking for monitor-config match horizontal x vertical resolution |$RESOLUTION|"

scan_for_monitor_config $RESOLUTION
if [[ $POSITION_RULE == "" ]]
then
if [[ ! -z $DEBUG ]]
then
echo "Looking for monitor-config horizontal match only: |${HORIZONTAL_RESOLUTION}x9999|"
fi
debug_text "Looking for monitor-config horizontal match only: |${HORIZONTAL_RESOLUTION}x9999|"

# Now try getting horizontal x 9999 resolution config
scan_for_monitor_config "${HORIZONTAL_RESOLUTION}x9999"
Expand Down Expand Up @@ -137,29 +126,42 @@ function position_windows {
# Main code
#

DBEUG=false

# Required
CONFIG_FILE=~/.position.conf

# Global variable
RESOLUTION=""
DISPLAY_LINE=""

# Only used to catch if user wants help, offer this before any real work
PARAM=${1:-"_null"}

if [[ $PARAM == "help" || $PARAM == "usage" || $PARAM == "-h" || $PARAM == "--help" || $PARAM == "-help" ]]
then
usage
exit 0
else
if [[ $PARAM != "_null" ]]
then
echo "Using special config |$PARAM|"
else
echo "Using default resolution based window placement."
PARAM=""
fi
fi
# defualt to null
PARAM="_null"
#PARAM=${1:-"_null"}

while [ "$1" != "" ]
do
case $1 in
-d | --debug )
DEBUG=true
debug_text "Debugging mode enabled"
;;

"help" | "-h" | "--help" | "usage" )
usage
exit 0
;;

# Any parameter not recognized as a command can be treated
# as the user requesting a specific position rule-group
*)
PARAM=$1
break
;;

esac
shift
done

# We require a config file, so check for that and bail out if not available
if [[ ! -r $CONFIG_FILE ]]
Expand All @@ -184,18 +186,26 @@ then
exit 1
fi

if [[ ! -z $PARAM ]]
if [[ $PARAM != "_null" ]]
then
echo "Using rule-group |$PARAM| as requested on command line"
else
echo "No rule-group specified on the CLI, will select one based on resolution"
fi

if [[ $PARAM != "_null" ]]
then
# User provided a parameter, so we'll apply any rules with the parameter name
POSITION_RULE="$PARAM"
else
# User did not provide a parameter,
# Identify the monitor configuration via the whole desktop resolution
get_resolution
fi

# Attempt to find a monitor-config rule in the config file for this resolution
find_monitor_config
# Attempt to find a monitor-config rule in the config file for this resolution
# This is only required if we are finding rules by resolution not parameter
find_monitor_config
fi

# Do we have rules to position windows for this display configuration?
check_for_window_rules
Expand Down

0 comments on commit 2b98036

Please sign in to comment.