Move executables into local/bin

and fix known bustedness
This commit is contained in:
2020-03-26 09:46:11 -04:00
parent caea8cab11
commit a2cc7a788a
11 changed files with 15 additions and 9 deletions

View File

@@ -0,0 +1,49 @@
#!/usr/bin/env python3
import codecs
import glob
import os
import os.path
import sys
def main():
screenlayout_dir = os.path.expanduser('~/.screenlayout')
if not os.path.isdir(screenlayout_dir):
os.makedirs(screenlayout_dir)
profiles = [
os.path.basename(f)
for f in glob.glob(os.path.join(screenlayout_dir, '*.sh'))
]
if len(profiles) == 0:
return 0
cur_profile = profiles[0]
cur_profile_path = os.path.join(screenlayout_dir, '.current')
if os.path.isfile(cur_profile_path):
with codecs.open(cur_profile_path, encoding='utf-8') as infile:
cur_profile = infile.read().strip()
if cur_profile not in profiles:
print('ERROR:{}: current profile {} not found'.format(progname,
cur_profile), file=sys.stderr)
cur_profile = profiles[0]
cur_idx = profiles.index(cur_profile)
next_idx = cur_idx + 1
if next_idx >= (len(profiles) - 1):
next_idx = abs(next_idx - len(profiles))
next_profile = profiles[next_idx]
with codecs.open(cur_profile_path, 'w', encoding='utf-8') as outfile:
outfile.write(next_profile)
os.execl(os.path.join(screenlayout_dir, next_profile), '--')
return 0
if __name__ == '__main__':
sys.exit(main())

3
local/bin/i3status-update-do Executable file
View File

@@ -0,0 +1,3 @@
#!/usr/bin/env bash
"$@"
exec kill -USR1 $(pidof i3status)

137
local/bin/i3wrapper.py Executable file
View File

@@ -0,0 +1,137 @@
#!/usr/bin/env python
import json
import os
import subprocess
import sys
_BITS = []
def _bit(idx):
def wrapper(func):
_BITS.append([idx, func.__name__.replace('_', ''), func])
return func
return wrapper
@_bit(6)
def _mem():
with open('/proc/meminfo') as meminfo_fp:
attrs = {}
for line in meminfo_fp.readlines():
parts = line.split(':', 2)
attrs[parts[0].strip()] = parts[1].strip()
avail = int(attrs['MemAvailable'].split()[0])
total = int(attrs['MemTotal'].split()[0])
used = int(((total - avail) / total) * 100)
color = None
if used > 75:
color = '#ff0000'
if used < 25:
color = '#00ff00'
return 'M:{}%'.format(used), color
@_bit(0)
def _backlight(dev_dir='/sys/class/backlight/intel_backlight'):
with open('{}/brightness'.format(dev_dir)) as br_fp:
with open('{}/max_brightness'.format(dev_dir)) as mb_fp:
value = int(br_fp.read().strip())
max_value = int(mb_fp.read().strip())
pct = int(float(float(value) / float(max_value)) * 100)
color = None
if pct >= 75:
color = '#ffaa00'
if pct <= 25:
color = '#5599ff'
return '☼:{}%'.format(pct), color
THINKFAN_CONF = '/etc/thinkfan.conf'
ACPI_IBM_FAN = '/proc/acpi/ibm/fan'
@_bit(7)
def _hwtemp(conf=THINKFAN_CONF, fan=ACPI_IBM_FAN):
if not os.path.exists(conf):
return None, None
temps = []
with open('/etc/thinkfan.conf') as tfc_fp:
for line in tfc_fp.readlines():
if not line.startswith('hwmon '):
continue
try:
with open(line.split(' ')[1].strip()) as temp_fp:
temps.append(float(temp_fp.read()))
except (OSError, IOError) as exc:
sys.stderr.write(str(exc) + '\n')
avg_temp = float(sum(temps)) / len(temps) / 1000.0
color = None
fan_level = 'unset'
try:
with open(fan) as fan_fp:
for line in fan_fp.readlines():
if not line.startswith('level:\t\t'):
continue
fan_level = int(line.replace('level:\t\t', ''))
except (OSError, IOError) as exc:
sys.stderr.write(str(exc) + '\n')
if avg_temp > 75:
color = '#ff0000'
if avg_temp >= 50:
color = '#ffaa00'
if avg_temp <= 25:
color = '#5599ff'
return 'T:{:.1f}°C L:{}'.format(avg_temp, fan_level), color
def _print_line(message):
sys.stdout.write(message + '\n')
sys.stdout.flush()
def _read_line():
try:
line = sys.stdin.readline().strip()
if not line:
sys.exit(3)
return line
except KeyboardInterrupt:
sys.exit()
def main(bits=_BITS):
_print_line(_read_line())
_print_line(_read_line())
while True:
line, prefix = _read_line(), ''
if line.startswith(','):
line, prefix = line[1:], ','
loaded = json.loads(line)
for idx, name, func in bits:
try:
value, color = func()
if value is None:
continue
record = dict(full_text=str(value), name=name)
if color is not None:
record.update(dict(color=color))
loaded.insert(idx, record)
except Exception as exc:
sys.stderr.write(str(exc) + '\n')
sys.stderr.flush()
_print_line(prefix+json.dumps(loaded))
if __name__ == '__main__':
main()

37
local/bin/latlon Executable file
View File

@@ -0,0 +1,37 @@
#!/usr/bin/env bash
set -o errexit
set -o pipefail
main() {
if [[ "${DEBUG}" ]]; then
set -o xtrace
fi
_get_lat_lon
}
_get_lat_lon() {
: "${IP_LOOKUP_URL:=https://ifconfig.co/ip}"
: "${GEOIP_LOOKUP_HOST:=http://api.geoiplookup.net}"
local ipaddr
ipaddr="$(curl -fsSL "${IP_LOOKUP_URL}")"
local geoip_xml
geoip_xml="$(curl -fsSL "${GEOIP_LOOKUP_HOST}?query=${ipaddr}")"
_extract_lat_long "${geoip_xml}"
}
_extract_lat_long() {
python <<EOPYTHON
from __future__ import print_function
import sys
from xml.dom.minidom import parseString as parse_xml_string
dom = parse_xml_string("""${1}""")
lat = dom.getElementsByTagName('latitude')[0].firstChild.data
lon = dom.getElementsByTagName('longitude')[0].firstChild.data
print('{}:{}'.format(lat, lon))
EOPYTHON
}
main "${@}"

9
local/bin/redshift-gtk-wrapper Executable file
View File

@@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -o errexit
main() {
export REDSHIFT_EXE=redshift-gtk
exec ~/.local/bin/redshift-wrapper "${@}"
}
main "${@}"

12
local/bin/redshift-wrapper Executable file
View File

@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -o errexit
set -o pipefail
main() {
: "${FALLBACK_COORDS:=40.4325:-79.863}"
local coords
coords="$(~/.local/bin/latlon 2>/dev/null || echo "${FALLBACK_COORDS}")"
exec "${REDSHIFT_EXE:-redshift}" -l "${coords}" -v
}
main "${@}"

52
local/bin/thinkfan-confgen Executable file
View File

@@ -0,0 +1,52 @@
#!/usr/bin/env bash
set -o errexit
set -o pipefail
main() {
if [[ -f "${1}" ]]; then
exec 1>"${1}"
shift
fi
if [[ -f /etc/default/thinkfan-confgen ]]; then
source /etc/default/thinkfan-confgen
fi
: "${THINKFAN_LOWER_BOUND:=30}"
: "${THINKFAN_STEP:=4}"
printf '# thinkfan-confgen created %s\n' "$(date -u)"
printf '# THINKFAN_LOWER_BOUND=%s\n' "${THINKFAN_LOWER_BOUND}"
printf '# THINKFAN_STEP=%s\n\n' "${THINKFAN_STEP}"
find /sys -type f -name 'temp*_input' | while read -r line; do
if [[ "${line}" =~ thinkpad_hwmon ]]; then
continue
fi
printf 'hwmon %s\n' "${line}"
done
printf '\ntp_fan /proc/acpi/ibm/fan\n\n'
local halfstep="$((THINKFAN_STEP / 2))"
local cur="${THINKFAN_LOWER_BOUND}"
for level in 0 1 2 3 4 5 6 7; do
if [[ "${level}" == 0 ]]; then
printf '(0, 0, %s)\n' "${cur}"
continue
fi
if [[ "${level}" == 7 ]]; then
printf '(7, %s, 32767)\n' "$((cur - halfstep))"
continue
fi
printf '(%s, %s, %s)\n' \
"${level}" "$((cur - halfstep))" "$((cur + THINKFAN_STEP))"
cur="$((cur + THINKFAN_STEP))"
done
}
main "${@}"