File: sxmo_common.sh

package info (click to toggle)
sxmo-utils 1.14.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 6,016 kB
  • sloc: sh: 9,166; ansic: 117; makefile: 68
file content (77 lines) | stat: -rwxr-xr-x 1,891 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2022 Sxmo Contributors

# This script is meant to be sourced by various sxmo scripts
# and defines some common settings

# This script ensures all sxmo scripts are using the busybox version of
# certain coreutils rather than any other version that may be installed on the
# user's computer

#aliases aren't expanded in bash
# shellcheck disable=SC2039,SC3044
command -v shopt > /dev/null && shopt -s expand_aliases

alias superctl="systemctl --user"

alias dmenu="sxmo_dmenu.sh"
alias bemenu="sxmo_dmenu.sh"
alias jq="gojq" # better performances
alias rfkill="/usr/sbin/rfkill"

if ! command -v sxmobar > /dev/null; then
	sxmobar() {
		sxmo_status.sh "$@"
	}
fi

confirm_menu() {
	printf "No\nYes\n" | \
		dmenu "$@" | \
		grep -q "Yes"
}

sxmo_log() {
	printf "%s %s: %s\n" "$(date +%H:%M:%S)" "${0##*/}" "$*" >> "${XDG_STATE_HOME:-$HOME}"/sxmo.log 2>&1
}

sxmo_debug() {
	if [ -n "$SXMO_DEBUG" ]; then
		printf "%s %s DEBUG: %s\n" "$(date +%H:%M:%S)" "${0##*/}" "$*" >> "${XDG_STATE_HOME:-$HOME}"/sxmo.log 2>&1
	fi
}

# Outputs the paths of the specified file/dir path in XDG_DATA_DIRS.
# Will output $2 instances (default 1, 0 for no limit)
# Will separate instances with $3 (default " ")
#
# xdg_data_path icons/open.ico
# -> "/usr/local/share/icons/open.ico"
#
# xdg_data_path sxmo/appcfg
# -> "/usr/share/sxmo/appcfg"
#
# xdg_data_path icons 0 "|"
# -> "/usr/local/share/icons|/usr/share/icons"
xdg_data_path() {
	filepath=$1
	instance_count=${2:-1}
	sep=${3:-" "}

	IFS=':'
	instance=0
	for dir in ${XDG_DATA_DIRS:-/usr/local/share:/usr/share}
	do
		if [ -e "$dir/$filepath" ]; then
			if [ "$instance" -ge "$instance_count" ] && [ "$instance_count" != "0" ]; then
				break
			fi
			if [ $instance -gt 0 ]; then
				printf '%s' "${sep}"
			fi
			printf '%s' "${dir}/${filepath}"
			instance=$((instance+1))
		fi
	done
}