commit 7c87f80234bff181367148c52360b87df04489ed
parent 20e9303def4382693ee5ee0e781f7c3ce416a0c3
Author: Chris Down <chris@chrisdown.name>
Date: Sun, 6 Sep 2015 13:59:07 +0100
Add comments to clipmenu where it could be helpful for future devs
Diffstat:
1 file changed, 15 insertions(+), 6 deletions(-)
diff --git a/clipmenu b/clipmenu
@@ -2,6 +2,7 @@
shopt -s nullglob
+# We use this to make sure the cache files are sorted bytewise
LC_COLLATE=C
# Some people copy/paste huge swathes of text that could slow down dmenu
@@ -12,13 +13,19 @@ ordered_selections=()
files=("/tmp/clipmenu.$USER/"*)
+# We can't use `for ... in` here because we need to add files to
+# ordered_selections from last to first -- that is, newest to oldest. Incoming
+# clipboard entries have a ISO datetime prefixed to the front to aid in this.
for (( i=${#files[@]}-1; i>=0; i-- )); do
file=${files[$i]}
+ # We look for the first line matching regex /./ here because we want the
+ # first line that can provide reasonable context to the user. That is, if
+ # you have 5 leading lines of whitespace, displaying " (6 lines)" is much
+ # less useful than displaying "foo (6 lines)", where "foo" is the first
+ # line in the entry with actionable context.
first_line=$(sed -n '/./{p;q}' "$file" | cut -c1-"$line_length_limit")
-
- lines=$(wc -l "$file")
- lines=${lines%% *}
+ lines=$(wc -l < "$file")
if (( lines > 1 )); then
first_line+=" ($lines lines)"
@@ -28,9 +35,11 @@ for (( i=${#files[@]}-1; i>=0; i-- )); do
selections[$first_line]=$file
done
-chosen_line=$(
- printf '%s\n' "${ordered_selections[@]}" | dmenu -l 8 "$@"
-)
+# It's okay to hardcode `-l 8` here as a sensible default without checking
+# whether `-l` is also in "$@", because the way that dmenu works allows a later
+# argument to override an earlier one. That is, if the user passes in `-l`, our
+# one will be ignored.
+chosen_line=$(printf '%s\n' "${ordered_selections[@]}" | dmenu -l 8 "$@")
[[ $chosen_line ]] || exit 1