summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Wührer <def@gmx.at>2022-11-03 19:23:18 +0100
committerDavid Wührer <def@gmx.at>2022-11-03 19:23:18 +0100
commitbe555f29f47f81993c12bf76065a0cb6bb0fee96 (patch)
treeeb5991d8b0309609746fe612068faeadf0c52e0b
parente60829f7b9c61dc870173a8dd8f2f85c17517ec4 (diff)
Add a few old things.
-rwxr-xr-xall6
-rwxr-xr-xany6
-rwxr-xr-xclone12
-rwxr-xr-xpf38
-rwxr-xr-xspt55
-rwxr-xr-xsum_up4
-rwxr-xr-xxmlbrowser39
-rwxr-xr-xyamlbrowser57
8 files changed, 217 insertions, 0 deletions
diff --git a/all b/all
new file mode 100755
index 0000000..dc33367
--- /dev/null
+++ b/all
@@ -0,0 +1,6 @@
+#!/bin/bash
+
+for i in $(seq 1 $#)
+do
+ [[ ${!i} ]] || exit 1
+done
diff --git a/any b/any
new file mode 100755
index 0000000..9fd9234
--- /dev/null
+++ b/any
@@ -0,0 +1,6 @@
+#!/bin/bash
+
+for i in $(seq 1 $#)
+do
+ [[ ${!i} ]] && exit 0
+done
diff --git a/clone b/clone
new file mode 100755
index 0000000..bbf920f
--- /dev/null
+++ b/clone
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+set -eu
+
+for arg in "$@"
+do
+ dir=${arg#*:}
+ dir="$HOME"/src/${dir%/*}
+ mkdir -pv "$dir"
+ cd "$dir"
+ git clone --verbose --progress "$arg"
+done
diff --git a/pf b/pf
new file mode 100755
index 0000000..95c8e92
--- /dev/null
+++ b/pf
@@ -0,0 +1,38 @@
+#!/bin/bash
+
+set -ue
+
+get_parent () {
+ ps hoppid p$1
+}
+
+get_children () {
+ ps hopid --ppid "$1"
+}
+
+get_ancestors () {
+ case $1 in
+ 1|'') :
+ ;; *) get_ancestors $(get_parent $1)
+ esac
+ echo "$1"
+}
+
+get_descendents () {
+ for c in $(get_children "$1")
+ do echo "$c"
+ get_descendents "$c"
+ done
+}
+
+declare -i p
+for p in "$@"
+do
+ [[ $p -ne 0 ]] && {
+ get_ancestors "$p"
+ get_descendents "$p"
+ } | {
+ readarray -t pids
+ ps -Hf "${pids[@]}" | sed "/^[^ ]* *$p /s/.*/&/"
+ }
+done
diff --git a/spt b/spt
new file mode 100755
index 0000000..e237d93
--- /dev/null
+++ b/spt
@@ -0,0 +1,55 @@
+#!/bin/bash
+
+###
+# Single pass tabulate.
+#
+# Turn grid-aligned tables into tab-separated values
+# using the first line as a template.
+# Pre-existing tabs are preserved,
+# which is probably not good.
+##
+# TODO:
+# - Optionally require more than one space for column separation.
+
+shopt -s extglob
+
+spitline(){
+ declare -i prevtab=0
+ for tabstop in ${tabstops[@]}
+ do
+ # Strip trailing spaces.
+ field=${line:$prevtab:$((tabstop-prevtab))}
+ field=${field%%*( )}
+ printf '%s\t' "$field"
+ prevtab=$tabstop
+ done
+ printf '%s\n' "${line:$prevtab}"
+}
+
+##
+# First line:
+##
+# The tab stops are where stretches of empty space end.
+IFS=$'\n'
+read -r firstline
+declare -i skip=1
+for ((i=0;i<${#firstline};i++))
+do
+ case ${firstline:$i:1} in
+ \ )
+ skip=0
+ ;; *)
+ [[ $skip -eq 0 ]] && tabstops+=($i)
+ skip=1
+ esac
+done
+#echo >&2 ${tabstops[@]}
+line=$firstline
+spitline
+
+##
+# All other lines:
+declare -i prevtab
+while read -r line
+do spitline
+done
diff --git a/sum_up b/sum_up
new file mode 100755
index 0000000..1d035d7
--- /dev/null
+++ b/sum_up
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+#sed 's/\t/ + /g'| bc -q
+sed 's/\t/ + /g'| python3 -qqIi 2> /dev/null
diff --git a/xmlbrowser b/xmlbrowser
new file mode 100755
index 0000000..731b466
--- /dev/null
+++ b/xmlbrowser
@@ -0,0 +1,39 @@
+#!/usr/bin/python3
+
+import sys
+import xml.etree.ElementTree as ET
+
+from qtpy import QtGui, QtWidgets, QtCore
+from qtpy.QtWidgets import QApplication, QTreeWidget, QTreeWidgetItem, QLabel
+
+def _make_item(element) -> QTreeWidgetItem:
+ r = QTreeWidgetItem([element.tag, None, element.text])
+ r.addChildren([QTreeWidgetItem([None, name, value]) for name, value in element.attrib.items()])
+ r.addChildren([_make_item(child) for child in element])
+ return r
+
+def main():
+ app = QApplication(sys.argv)
+ # TODO: Check for suitable arguments first.
+ # Parameter may be a Qt option.
+ if len(sys.argv) == 1:
+ print("Usage:", sys.argv[0], "[file …]", file=sys.stderr)
+ return
+
+ items = []
+ for arg in sys.argv[1:]:
+ with open(arg) as xmlfile:
+ xmltree = ET.parse(xmlfile)
+ fileItem = QTreeWidgetItem([arg])
+ items.append(fileItem)
+ fileItem.addChildren([_make_item(xmltree.getroot())])
+
+ tree = QTreeWidget()
+ tree.setColumnCount(2)
+ tree.setHeaderLabels(["Element", "attribute", "text"])
+ tree.insertTopLevelItems(0, items)
+ tree.show()
+ sys.exit(app.exec_()) # Main loop.
+
+if __name__ == '__main__':
+ main()
diff --git a/yamlbrowser b/yamlbrowser
new file mode 100755
index 0000000..464f6c6
--- /dev/null
+++ b/yamlbrowser
@@ -0,0 +1,57 @@
+#!/usr/bin/env python3
+
+import sys
+import yaml
+from qtpy import QtGui, QtWidgets, QtCore
+from qtpy.QtWidgets import QApplication, QTreeWidget, QTreeWidgetItem, QLabel
+
+def _make_children (parent:QTreeWidgetItem, d) -> QTreeWidgetItem:
+ """
+ If the item is a container item, i.e. list or dict,
+ its contents are added as children to the parent.
+
+ If the item is not a container item,
+ it becomes the value to its parent.
+ """
+ def transform_l (p:QTreeWidgetItem, d:list) -> QTreeWidgetItem:
+ p.addChildren (_make_children (QTreeWidgetItem (), x) for x in d)
+ return p
+ def transform_d (p:QTreeWidgetItem, d:dict) -> QTreeWidgetItem:
+ p.addChildren (_make_children (QTreeWidgetItem ([key]), value) for key, value in d.items ())
+ return p
+ return {
+ list: lambda p, d : transform_l (p, d)
+ , dict: lambda p, d : transform_d (p, d)
+ , str: lambda p, d : QTreeWidgetItem ([p.text (0), d])
+ , int: lambda p, d : QTreeWidgetItem ([p.text (0), str(d)])
+ , float: lambda p, d : QTreeWidgetItem ([p.text (0), str(d)])
+ , bool: lambda p, d : QTreeWidgetItem ([p.text (0), str(d)])
+ }[type (d)] (parent, d)
+
+def main ():
+ app = QApplication (sys.argv)
+ # TODO: Check for suitable arguments first.
+ # Parameter may be a Qt option.
+ if len (sys.argv) == 1:
+ print ("Usage:", sys.argv[0], "[file …]", file=sys.stderr)
+ return
+ try:
+ items = []
+ for arg in sys.argv[1:]:
+ with open (arg) as file:
+ data = list (yaml.safe_load_all (file))
+ items.append (_make_children (QTreeWidgetItem ([arg]), data[0] if len (data) == 1 else data))
+
+ tree = QTreeWidget ()
+ tree.setColumnCount (2)
+ tree.setHeaderLabels (["Key", "Value"])
+ tree.insertTopLevelItems(0, items)
+ tree.show ()
+ except Exception as x:
+ print (x, file=sys.stderr)
+ label = QtWidgets.QLabel (str(x))
+ label.show () # Opens window.
+ sys.exit (app.exec_()) # Main loop. Display the label.
+
+if __name__=='__main__':
+ main ()