changeset 3:a5f727748b9a

subr: initial creation
author David Demelier <markand@malikania.fr>
date Tue, 07 Nov 2017 10:40:57 +0100
parents 28f19009cbc2
children af59db417857
files dmenu.subr
diffstat 1 files changed, 85 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dmenu.subr	Tue Nov 07 10:40:57 2017 +0100
@@ -0,0 +1,85 @@
+#!/bin/sh
+#
+# dmenu.subr -- common operations for dmenutools
+#
+# Copyright (c) 2016-2017 David Demelier <markand@malikania.fr>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+# Directory for configuration.
+if [ -z "${XDG_CONFIG_HOME}" ]; then
+    XDG_CONFIG_HOME=${HOME}/.config
+fi
+
+# Open user optional configuration.
+if [ -r ${XDG_CONFIG_HOME}/dmenutools.conf ]; then
+    . ${XDG_CONFIG_HOME}/dmenutools.conf
+fi
+
+#
+# dmt_fatal
+# -------------------------------------------------------------------
+#
+# Log a fatal error through fatal_cmd helper.
+# 
+dmt_fatal()
+{
+    ${fatal_cmd} "$1"
+    exit 1
+}
+
+#
+# dmt_run
+# -------------------------------------------------------------------
+#
+# Run dmenu with parameters specified from both configuration and arguments.
+#
+dmt_run()
+{
+    dmenu $*
+}
+
+#
+# dmt_boolean
+# -------------------------------------------------------------------
+# 
+# Check if a variable is a boolean value.
+#
+dmt_boolean()
+{
+    v=$(echo $1 | tr '[:upper:]' '[:lower:]')
+
+    if [ $v = "1" ] || [ $v = "yes" ] || [ $v = "on" ] || [ $v = "true" ]; then
+        return 0
+    fi
+
+    return 1
+}
+
+# Determine a fatal helper.
+if [ -z "${fatal_cmd}" ]; then
+    if command -v notify-send > /dev/null 2>&1; then
+        fatal_cmd="notify-send"
+    elif command -v xmessage > /dev/null 2>&1; then
+        fatal_cmd="xmessage"
+    else
+        fatal_cmd="echo"
+    fi
+fi
+
+if ! command -v dmenu > /dev/null 2>&1; then
+    dmt_fatal "dmenu is missing"
+fi
+
+# vim: syntax=sh: