From a92a01fe479e3b069e23297cb3b541eb273deb58 Mon Sep 17 00:00:00 2001 From: Amin Bandali Date: Fri, 12 Apr 2019 23:39:24 -0400 Subject: [PATCH 1/1] scripts: pull current scripts out of rc.org & clean up battery-percentage-time: pull out of rc.org scripts my-i3status: pull out of rc.org rofi-light: pull out of rc.org rofi-remmina: pull out of rc.org s: pull out of rc.org sway-ws-util: pull out of rc.org toggle-presentation-mode: pull out of rc.org toggle-tablet: pull out of rc.org volume-info: pull out of rc.org zathura-sync: pull out of rc.org remove Scripts --- .local/bin/battery-percentage-time | 22 +++ .local/bin/my-i3status | 70 +++++++ .local/bin/rofi-light | 5 + .local/bin/rofi-remmina | 31 +++ .local/bin/s | 7 + .local/bin/sway-ws-util | 31 +++ .local/bin/toggle-presentation-mode | 3 + .local/bin/toggle-tablet | 32 ++++ .local/bin/volume-info | 9 + .local/bin/zathura-sync | 8 + rc.org | 287 ---------------------------- 11 files changed, 218 insertions(+), 287 deletions(-) create mode 100755 .local/bin/battery-percentage-time create mode 100755 .local/bin/my-i3status create mode 100755 .local/bin/rofi-light create mode 100755 .local/bin/rofi-remmina create mode 100755 .local/bin/s create mode 100755 .local/bin/sway-ws-util create mode 100755 .local/bin/toggle-presentation-mode create mode 100755 .local/bin/toggle-tablet create mode 100755 .local/bin/volume-info create mode 100755 .local/bin/zathura-sync diff --git a/.local/bin/battery-percentage-time b/.local/bin/battery-percentage-time new file mode 100755 index 0000000..b0122a8 --- /dev/null +++ b/.local/bin/battery-percentage-time @@ -0,0 +1,22 @@ +#!/bin/sh + +dbus_send() { + label=$1 + dbus-send --print-reply=literal --system \ + --dest=org.freedesktop.UPower \ + /org/freedesktop/UPower/devices/battery_BAT0 \ + org.freedesktop.DBus.Properties.Get \ + string:org.freedesktop.UPower.Device \ + string:"${label}" | awk '{print $3}' +} + +perc=$(dbus_send 'Percentage') +state=$(dbus_send 'State') + +if [ "$state" -eq 2 ]; then # Discharging + secs=$(dbus_send 'TimeToEmpty') +elif [ "$state" -eq 1 ]; then # Charging + secs=$(dbus_send 'TimeToFull') +fi + +printf '%s%%%%%2dh%02dm \n' "$perc" $((secs / 3600)) $((secs % 3600 / 60)) diff --git a/.local/bin/my-i3status b/.local/bin/my-i3status new file mode 100755 index 0000000..2831c15 --- /dev/null +++ b/.local/bin/my-i3status @@ -0,0 +1,70 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +# This script is a simple wrapper which prefixes each i3status line with custom +# information. It is based on: +# https://github.com/i3/i3status/blob/master/contrib/wrapper.py +# +# In ~/.i3status.conf, add the following line: +# output_format = "i3bar" +# in the 'general' section. +# Then, in ~/.config/i3/config or ~/.config/sway/config add: +# status_command i3status | my-i3status.py +# in the 'bar' section. Make sure my-i3status.py is in $PATH. +# +# © 2012 Valentin Haenel +# © 2018 Amin Bandali +# +# This program is free software. It comes without any warranty, to the extent +# permitted by applicable law. You can redistribute it and/or modify it under +# the terms of the Do What The Fuck You Want To Public License (WTFPL), Version +# 2, as published by Sam Hocevar. See http://sam.zoy.org/wtfpl/COPYING for more +# details. + +import sys +import json +import os + +def get_nosleep(): + """ Return true if ~/.nosleep exists. """ + return os.path.isfile(os.path.expanduser("~/.nosleep")) + +def print_line(message): + """ Non-buffered printing to stdout. """ + sys.stdout.write(message + '\n') + sys.stdout.flush() + +def read_line(): + """ Interrupted respecting reader for stdin. """ + # try reading a line, removing any extra whitespace + try: + line = sys.stdin.readline().strip() + # i3status sends EOF, or an empty line + if not line: + sys.exit(3) + return line + # exit on ctrl-c + except KeyboardInterrupt: + sys.exit() + +if __name__ == '__main__': + # Skip the first line which contains the version header. + print_line(read_line()) + + # The second line contains the start of the infinite array. + print_line(read_line()) + + while True: + line, prefix = read_line(), '' + # ignore comma at start of lines + if line.startswith(','): + line, prefix = line[1:], ',' + + if get_nosleep(): + j = json.loads(line) + # insert information into the start of the json, but could be anywhere + j.insert(0, {'full_text' : '•', 'name' : 'nosleep'}) + # and echo back new encoded json + print_line(prefix+json.dumps(j)) + else: + print_line(prefix+line) diff --git a/.local/bin/rofi-light b/.local/bin/rofi-light new file mode 100755 index 0000000..f2a46af --- /dev/null +++ b/.local/bin/rofi-light @@ -0,0 +1,5 @@ +#!/bin/sh + +cur=$(light -G) +val=$(rofi -dmenu -mesg "light $cur" -p "light -S " -l 0 -width 12) +[ -n "$val" ] && light -S "$val" diff --git a/.local/bin/rofi-remmina b/.local/bin/rofi-remmina new file mode 100755 index 0000000..153a5a6 --- /dev/null +++ b/.local/bin/rofi-remmina @@ -0,0 +1,31 @@ +#!/usr/bin/env python2 + +import ConfigParser +import os +from subprocess import Popen, PIPE + +remmina_dir = os.path.expanduser("~/.local/share/remmina") + +fdict = dict() + +for f in os.listdir(remmina_dir): + fp = os.path.join(remmina_dir, f) + c = ConfigParser.ConfigParser() + c.read(fp) + n = c.get('remmina', 'name') + fdict[n] = fp + +lines = max(min(15, len(fdict)), 1); +width = len(max(fdict.keys(), key=len)) +rofi = Popen(["rofi", "-i", "-dmenu", \ + "-l", str(lines), "-width", str(width), \ + "-p", "connection"], stdout=PIPE, stdin=PIPE) +selected = rofi.communicate("\n" \ + .join(fdict.keys()) \ + .encode("utf-8"))[0] \ + .decode("utf-8") \ + .strip() +rofi.wait() + +r = Popen(["remmina", "-c", fdict[selected]]) +r.wait() diff --git a/.local/bin/s b/.local/bin/s new file mode 100755 index 0000000..fec27ed --- /dev/null +++ b/.local/bin/s @@ -0,0 +1,7 @@ +#!/bin/bash + +export _JAVA_AWT_WM_NONREPARENTING=1 +export _JAVA_OPTIONS='-Dawt.useSystemAAFontSettings=on -Dswing.aatext=true' +light -Nr 2 +source $HOME/.zprofile +sway diff --git a/.local/bin/sway-ws-util b/.local/bin/sway-ws-util new file mode 100755 index 0000000..e03bf24 --- /dev/null +++ b/.local/bin/sway-ws-util @@ -0,0 +1,31 @@ +#!/bin/sh + +curr_ws=$(swaymsg -t get_workspaces | jq -r '.[] | select(.focused==true).name') +[ "$curr_ws" -eq 1 ] && prev_ws=10 || prev_ws=$((curr_ws-1)) +[ "$curr_ws" -eq 10 ] && next_ws=1 || next_ws=$((curr_ws+1)) +dest_ws=-1 +op=-1 + +if [ "$1" = "switch" ] || [ "$1" = "move" ]; then + op="$1" + if [ "$2" = "prev" ]; then + dest_ws="$prev_ws" + elif [ "$2" = "next" ]; then + dest_ws="$next_ws" + else + echo "Usage: $0 $1 {prev|next} [follow]" + exit 1 + fi +else + echo "Usage: $0 {switch|move} {prev|next} [follow]" + exit 1 +fi + +if [ "$op" = "switch" ]; then + sway workspace "$dest_ws" +elif [ "$op" = "move" ]; then + sway move container to workspace "$dest_ws" + if [ "$3" = "follow" ]; then + sway workspace "$dest_ws" + fi +fi diff --git a/.local/bin/toggle-presentation-mode b/.local/bin/toggle-presentation-mode new file mode 100755 index 0000000..400084f --- /dev/null +++ b/.local/bin/toggle-presentation-mode @@ -0,0 +1,3 @@ +#!/bin/sh + +xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/presentation-mode -T diff --git a/.local/bin/toggle-tablet b/.local/bin/toggle-tablet new file mode 100755 index 0000000..819af42 --- /dev/null +++ b/.local/bin/toggle-tablet @@ -0,0 +1,32 @@ +#!/bin/sh + +# This script toggles between a 'normal' mode and a 'tablet' mode, doing +# a few things: + +# - rotates the screen using =xrandr=, so that rotating the physical +# display of my X220t would have the laptop's battery on the right +# hand side, +# - enables touch screen, +# - properly rotates the stylus pen and touch screen pointers, and +# - toggles between RGB and Vertical BGR sub-pixel order. + +case $(xfconf-query -c pointers -p /Wacom_ISDv4_E6_Pen_stylus/Properties/Wacom_Rotation) in + 0) # Screen is not rotated, we should rotate it right (90°) + xrandr -o 3 + xfconf-query -c pointers -p /Wacom_ISDv4_E6_Pen_stylus/Properties/Wacom_Rotation -s 1 + xfconf-query -c pointers -p /Wacom_ISDv4_E6_Finger_touch/Properties/Device_Enabled -s 1 + xfconf-query -c pointers -p /Wacom_ISDv4_E6_Finger_touch/Properties/Wacom_Rotation -s 1 + xfconf-query -c xsettings -p /Xft/RGBA -s vbgr + ;; + 1) # Currently top is rotated right, we should set it normal (0°) + xrandr -o 0 + xfconf-query -c pointers -p /Wacom_ISDv4_E6_Pen_stylus/Properties/Wacom_Rotation -s 0 + xfconf-query -c pointers -p /Wacom_ISDv4_E6_Finger_touch/Properties/Wacom_Rotation -s 0 + xfconf-query -c pointers -p /Wacom_ISDv4_E6_Finger_touch/Properties/Device_Enabled -s 0 + xfconf-query -c xsettings -p /Xft/RGBA -s rgb + ;; + *) + echo "Unknown result from 'xfconf-query -c pointers -p /Wacom_ISDv4_E6_Pen_stylus/Properties/Wacom_Rotation'" >&2 + exit 1 + ;; +esac diff --git a/.local/bin/volume-info b/.local/bin/volume-info new file mode 100755 index 0000000..d87137e --- /dev/null +++ b/.local/bin/volume-info @@ -0,0 +1,9 @@ +#!/bin/sh + +cur_vol=$(pamixer --get-volume) + +if [ $(pamixer --get-mute) = true ]; then + printf 'mt \n' "$cur_vol" +else + printf '%02d \n' "$cur_vol" +fi diff --git a/.local/bin/zathura-sync b/.local/bin/zathura-sync new file mode 100755 index 0000000..4384ffe --- /dev/null +++ b/.local/bin/zathura-sync @@ -0,0 +1,8 @@ +#!/bin/sh + +pos="$1" +pdffile="$2" +zathura --synctex-forward "$pos" "$pdffile" || \ + ( + zathura -x "emacsclient --eval '(progn (switch-to-buffer (file-name-nondirectory \"%{input}\")) (goto-line %{line}))'" "$pdffile" & + sleep 1; zathura --synctex-forward "$pos" "$pdffile" ) diff --git a/rc.org b/rc.org index e73ff10..fc6fc24 100644 --- a/rc.org +++ b/rc.org @@ -2570,290 +2570,3 @@ echo -e "\033[5 q" #+end_src -* Scripts - -This section contains various useful scripts and the ones used by the -programs above. For instance, =toggle-tablet= for switching to and -from tablet mode on my X220T, =toggle-presentation-mode= for toggling -Xfce's presentation mode which keeps the screen awake, and -=rofi-light= a small utility that uses [[https://github.com/DaveDavenport/rofi][Rofi]] to ask and [[https://github.com/haikarainen/light][light]] to set an -exact brightness value. - -** battery-percentage-time -:PROPERTIES: -:header-args+: :tangle ~/.local/bin/battery-percentage-time :shebang "#!/bin/sh" -:END: - -#+begin_src sh :tangle no -dbus_send() { - label=$1 - dbus-send --print-reply=literal --system \ - --dest=org.freedesktop.UPower \ - /org/freedesktop/UPower/devices/battery_BAT0 \ - org.freedesktop.DBus.Properties.Get \ - string:org.freedesktop.UPower.Device \ - string:"${label}" | awk '{print $3}' -} - -perc=$(dbus_send 'Percentage') -state=$(dbus_send 'State') - -if [ "$state" -eq 2 ]; then # Discharging - secs=$(dbus_send 'TimeToEmpty') -elif [ "$state" -eq 1 ]; then # Charging - secs=$(dbus_send 'TimeToFull') -fi - -printf '%s%%%%%2dh%02dm \n' "$perc" $((secs / 3600)) $((secs % 3600 / 60)) -#+end_src - -** my-i3status.py -:PROPERTIES: -:header-args+: :tangle ~/.local/bin/my-i3status.py :shebang "#!/usr/bin/env python2" -:END: - -#+begin_src python :comments none -# -*- coding: utf-8 -*- - -# This script is a simple wrapper which prefixes each i3status line with custom -# information. It is based on: -# https://github.com/i3/i3status/blob/master/contrib/wrapper.py -# -# In ~/.i3status.conf, add the following line: -# output_format = "i3bar" -# in the 'general' section. -# Then, in ~/.config/i3/config or ~/.config/sway/config add: -# status_command i3status | my-i3status.py -# in the 'bar' section. Make sure my-i3status.py is in $PATH. -# -# © 2012 Valentin Haenel -# © 2018 Amin Bandali -# -# This program is free software. It comes without any warranty, to the extent -# permitted by applicable law. You can redistribute it and/or modify it under -# the terms of the Do What The Fuck You Want To Public License (WTFPL), Version -# 2, as published by Sam Hocevar. See http://sam.zoy.org/wtfpl/COPYING for more -# details. - -import sys -import json -import os - -def get_nosleep(): - """ Return true if ~/.nosleep exists. """ - return os.path.isfile(os.path.expanduser("~/.nosleep")) - -def print_line(message): - """ Non-buffered printing to stdout. """ - sys.stdout.write(message + '\n') - sys.stdout.flush() - -def read_line(): - """ Interrupted respecting reader for stdin. """ - # try reading a line, removing any extra whitespace - try: - line = sys.stdin.readline().strip() - # i3status sends EOF, or an empty line - if not line: - sys.exit(3) - return line - # exit on ctrl-c - except KeyboardInterrupt: - sys.exit() - -if __name__ == '__main__': - # Skip the first line which contains the version header. - print_line(read_line()) - - # The second line contains the start of the infinite array. - print_line(read_line()) - - while True: - line, prefix = read_line(), '' - # ignore comma at start of lines - if line.startswith(','): - line, prefix = line[1:], ',' - - if get_nosleep(): - j = json.loads(line) - # insert information into the start of the json, but could be anywhere - j.insert(0, {'full_text' : '•', 'name' : 'nosleep'}) - # and echo back new encoded json - print_line(prefix+json.dumps(j)) - else: - print_line(prefix+line) -#+end_src - -** rofi-light -:PROPERTIES: -:header-args+: :tangle ~/.local/bin/rofi-light :shebang "#!/bin/bash" -:END: - -#+begin_src bash -cur=$(light -G) -val=$(rofi -dmenu -mesg "light $cur" -p "light -S " -l 0 -width 12) -[ -n "$val" ] && light -S $val -#+end_src - -** rofi-remmina.py -:PROPERTIES: -:header-args+: :tangle ~/.local/bin/rofi-remmina.py :shebang "#!/usr/bin/env python2" -:END: - -#+begin_src python -import ConfigParser -import os -from subprocess import Popen, PIPE - -remmina_dir = os.path.expanduser("~/.local/share/remmina") - -fdict = dict() - -for f in os.listdir(remmina_dir): - fp = os.path.join(remmina_dir, f) - c = ConfigParser.ConfigParser() - c.read(fp) - n = c.get('remmina', 'name') - fdict[n] = fp - -lines = max(min(15, len(fdict)), 1); -width = len(max(fdict.keys(), key=len)) -rofi = Popen(["rofi", "-i", "-dmenu", \ - "-l", str(lines), "-width", str(width), \ - "-p", "connection"], stdout=PIPE, stdin=PIPE) -selected = rofi.communicate("\n" \ - .join(fdict.keys()) \ - .encode("utf-8"))[0] \ - .decode("utf-8") \ - .strip() -rofi.wait() - -r = Popen(["remmina", "-c", fdict[selected]]) -r.wait() -#+end_src - -** s (run sway) -:PROPERTIES: -:header-args+: :tangle ~/.local/bin/s :shebang "#!/bin/bash" -:END: - -#+begin_src bash -export _JAVA_AWT_WM_NONREPARENTING=1 -export _JAVA_OPTIONS='-Dawt.useSystemAAFontSettings=on -Dswing.aatext=true' -light -Nr 2 -source $HOME/.zprofile -sway -#+end_src - -** sway-ws-util -:PROPERTIES: -:header-args+: :tangle ~/.local/bin/sway-ws-util :shebang "#!/bin/bash" -:END: - -#+begin_src bash -curr_ws=$(swaymsg -t get_workspaces | jq -r '.[] | select(.focused==true).name') -[[ $curr_ws -eq 1 ]] && prev_ws=10 || prev_ws=$((curr_ws-1)) -[[ $curr_ws -eq 10 ]] && next_ws=1 || next_ws=$((curr_ws+1)) -dest_ws=-1 -op=-1 - -if [ "$1" = "switch" ] || [ "$1" = "move" ]; then - op="$1" - if [ "$2" = "prev" ]; then - dest_ws="$prev_ws" - elif [ "$2" = "next" ]; then - dest_ws="$next_ws" - else - echo "Usage: $0 $1 {prev|next} [follow]" - exit 1 - fi -else - echo "Usage: $0 {switch|move} {prev|next} [follow]" - exit 1 -fi - -if [ "$op" = "switch" ]; then - sway workspace "$dest_ws" -elif [ "$op" = "move" ]; then - sway move container to workspace "$dest_ws" - if [ "$3" = "follow" ]; then - sway workspace "$dest_ws" - fi -fi -#+end_src - -** toggle-presentation-mode -:PROPERTIES: -:header-args+: :tangle ~/.local/bin/toggle-presentation-mode :shebang "#!/bin/bash" -:END: - -#+begin_src bash :tangle no -xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/presentation-mode -T -#+end_src - -** toggle-tablet -:PROPERTIES: -:header-args+: :tangle ~/.local/bin/toggle-tablet :shebang "#!/bin/bash" -:END: - -This script toggles between a 'normal' mode and a 'tablet' mode, doing -a few things: - -- rotates the screen using =xrandr=, so that rotating the physical - display of my X220t would have the laptop's battery on the right - hand side, -- enables touch screen, -- properly rotates the stylus pen and touch screen pointers, and -- toggles between RGB and Vertical BGR sub-pixel order. - -#+begin_src bash :tangle no -case $(xfconf-query -c pointers -p /Wacom_ISDv4_E6_Pen_stylus/Properties/Wacom_Rotation) in - 0) # Screen is not rotated, we should rotate it right (90°) - xrandr -o 3 - xfconf-query -c pointers -p /Wacom_ISDv4_E6_Pen_stylus/Properties/Wacom_Rotation -s 1 - xfconf-query -c pointers -p /Wacom_ISDv4_E6_Finger_touch/Properties/Device_Enabled -s 1 - xfconf-query -c pointers -p /Wacom_ISDv4_E6_Finger_touch/Properties/Wacom_Rotation -s 1 - xfconf-query -c xsettings -p /Xft/RGBA -s vbgr - ;; - 1) # Currently top is rotated right, we should set it normal (0°) - xrandr -o 0 - xfconf-query -c pointers -p /Wacom_ISDv4_E6_Pen_stylus/Properties/Wacom_Rotation -s 0 - xfconf-query -c pointers -p /Wacom_ISDv4_E6_Finger_touch/Properties/Wacom_Rotation -s 0 - xfconf-query -c pointers -p /Wacom_ISDv4_E6_Finger_touch/Properties/Device_Enabled -s 0 - xfconf-query -c xsettings -p /Xft/RGBA -s rgb - ;; - *) - echo "Unknown result from 'xfconf-query -c pointers -p /Wacom_ISDv4_E6_Pen_stylus/Properties/Wacom_Rotation'" >&2 - exit 1 - ;; -esac -#+end_src - -** volume-info -:PROPERTIES: -:header-args+: :tangle ~/.local/bin/volume-info :shebang "#!/bin/sh" -:END: - -#+begin_src sh :tangle no -cur_vol=$(pamixer --get-volume) - -if [ $(pamixer --get-mute) = true ]; then - printf 'mt \n' "$cur_vol" -else - printf '%02d \n' "$cur_vol" -fi -#+end_src - -** zathura-sync.sh -:PROPERTIES: -:header-args+: :tangle ~/.local/bin/zathura-sync.sh :shebang "#!/bin/sh" -:END: - -#+begin_src sh -pos="$1" -pdffile="$2" -zathura --synctex-forward "$pos" "$pdffile" || \ - ( - zathura -x "emacsclient --eval '(progn (switch-to-buffer (file-name-nondirectory \"%{input}\")) (goto-line %{line}))'" "$pdffile" & - sleep 1; zathura --synctex-forward "$pos" "$pdffile" ) -#+end_src -- 2.20.1