Files
@ 41ee6a32070a
Branch filter:
Location: majic-scripts/utils/cheatsheet_viewer.sh - annotation
41ee6a32070a
15.7 KiB
text/x-sh
[cheatsheet_viewer.sh] Added support for automatically activating overlay mode based on current window title.
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 | 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e f3669338ab80 88ff60100e1b ac4dfe8bc978 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 88ff60100e1b 88ff60100e1b 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 41ee6a32070a 41ee6a32070a f3669338ab80 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 626f4a46ba00 626f4a46ba00 626f4a46ba00 ac4dfe8bc978 42a53c04297e 42a53c04297e 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 5ebeba51e2ff 88ff60100e1b 88ff60100e1b 88ff60100e1b 41ee6a32070a 41ee6a32070a 41ee6a32070a 88ff60100e1b 88ff60100e1b 41ee6a32070a 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 41ee6a32070a 5ebeba51e2ff 41ee6a32070a 5ebeba51e2ff 5ebeba51e2ff 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 5ebeba51e2ff 88ff60100e1b 88ff60100e1b 88ff60100e1b 5ebeba51e2ff 41ee6a32070a 41ee6a32070a 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 88ff60100e1b 88ff60100e1b 88ff60100e1b ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 5ebeba51e2ff 5ebeba51e2ff ac4dfe8bc978 ac4dfe8bc978 626f4a46ba00 41ee6a32070a ac4dfe8bc978 41ee6a32070a ac4dfe8bc978 ac4dfe8bc978 41ee6a32070a ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a 41ee6a32070a ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 626f4a46ba00 626f4a46ba00 626f4a46ba00 626f4a46ba00 626f4a46ba00 626f4a46ba00 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 41ee6a32070a 5ebeba51e2ff 41ee6a32070a 5ebeba51e2ff 5ebeba51e2ff ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 626f4a46ba00 626f4a46ba00 626f4a46ba00 626f4a46ba00 626f4a46ba00 626f4a46ba00 626f4a46ba00 626f4a46ba00 626f4a46ba00 626f4a46ba00 626f4a46ba00 626f4a46ba00 5ebeba51e2ff 41ee6a32070a 41ee6a32070a 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e ac4dfe8bc978 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 5ebeba51e2ff 5ebeba51e2ff 5ebeba51e2ff 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 5ebeba51e2ff 42a53c04297e 5ebeba51e2ff 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 f3669338ab80 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 88ff60100e1b 5ebeba51e2ff 88ff60100e1b ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 ac4dfe8bc978 5ebeba51e2ff ac4dfe8bc978 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e 42a53c04297e | #!/bin/bash
#
# cheatsheet_viewer.sh
#
# Copyright (C) 2025, Branko Majic <branko@majic.rs>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Treat unset variables as errors.
set -u
PROGRAM="cheatsheet_viewer.sh"
VERSION="0.0.1"
function usage() {
cat <<EOF
$PROGRAM $VERSION, simple utility for viewing image cheatsheets
Usage:
$PROGRAM [OPTIONS] list
$PROGRAM [OPTIONS] info PROFILE
$PROGRAM [OPTIONS] display [PROFILE]
$PROGRAM [OPTIONS] toggle [PROFILE]
EOF
}
function short_help() {
cat <<EOF
$(usage)
For more details see $PROGRAM -h.
EOF
}
function long_help() {
cat <<EOF
$(usage)
$PROGRAM is a simple utility for viewing image cheatsheets with
support for multiple profiles.
Cheatsheets are shown using the pqiv image viewer.
All configuration is stored under user's own home directory at:
- ~/.config/cheatsheet_viewer/
Individual profiles are stored as sub-directories, with each
sub-directory representing an individual profile that can be
activated. Base directory for profiles is:
- ~/.config/cheatsheet_viewer/profiles/
Each profile definition consists out of a number of configuration
files:
- ~/.config/cheatsheet_viewer/profiles/PROFILE/cheatsheets (list of
cheatsheet files, one per line, blank lines are ignored, lines
starting with '#' are ignored)
- ~/.config/cheatsheet_viewer/profiles/PROFILE/overlay (list of
window titles for which to automatically apply the overlay mode)
Multiple commands are provided for working with the profiles and
displaying the cheatsheets.
list
Lists the available profiles.
info PROFILE
Arguments:
PROFILE (profile name)
Shows detailed information about user-specified profile, such as
list of defined cheatsheets etc.
display [PROFILE]
Arguments:
PROFILE (profile name)
Displays cheatsheets from the specified profile (default profile
name is 'default').
toggle [PROFILE]
Arguments:
PROFILE (profile name)
Toggles display of cheatsheets from the specified profile (default
profile name is 'default'). Unlike the display command, the toggle
command is more intelligent and ensures that only one instance of
cheatsheet-specific pqiv is present. Thex toggle command also keeps
track of which cheatsheet was last selected, and will update the
image viewer to point to it when opened.
$PROGRAM accepts the following options:
-o
Display cheatsheets in "overlay" mode, keeping the current
window in focus, and making the cheatsheet window
always-on-top. Useful when playing games and similar. The
cheatsheet window can still be focused using keyboard and
mouse-clicks.
-q
Quiet mode.
-d
Enable debug mode.
-v
Show script version and licensing information.
-h
Show full help.
Please report bugs and send feature requests to <branko@majic.rs>.
EOF
}
function version() {
cat <<EOF
$PROGRAM, version $VERSION
+-----------------------------------------------------------------------+
| Copyright (C) 2025, Branko Majic <branko@majic.rs> |
| |
| This program is free software: you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation, either version 3 of the License, or |
| (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with this program. If not, see <http://www.gnu.org/licenses/>. |
+-----------------------------------------------------------------------+
EOF
}
# Commands
# ========
#
# Lists available profiles.
#
# Arguments:
#
# $1 (config_dir)
# Base directory under which all configuration files are stored.
#
# Returns:
# 0 on success, 1 otherwise.
#
function command_list() {
local config_dir="$1"
local profiles_dir="$config_dir/profiles"
shopt -s nullglob
local profiles=("$profiles_dir"/*)
shopt -u nullglob
if (( ${#profiles[@]} == 0 )); then
echo "No profiles defined under $profiles_dir"
return 0
fi
echo "Available profiles:"
echo
for profile in "${profiles[@]}"; do
echo " - $(basename "$profile")"
done
}
#
# Displays profile information.
#
# Arguments:
#
# $1 (config_dir)
# Base directory under which all configuration files are stored.
#
# $2 (profile)
# Profile name.
#
# Returns:
# 0 on success, 1 otherwise.
#
function command_info() {
local config_dir="$1"
local profile="$2"
local profile_dir="$config_dir/profiles/$profile"
local cheatsheets_file="$profile_dir/cheatsheets"
local cheatsheets=()
if [[ ! -d $profile_dir ]]; then
error "No such profile found under $profile_dir"
return 1
fi
if [[ -e $cheatsheets_file ]]; then
readarray -t cheatsheets < <(grep --invert-match --extended-regexp "(^[[:blank:]]*#|^[[:blank:]]*$)" "$cheatsheets_file")
fi
echo "Profile $profile"
echo "========${profile//?/=}"
echo
echo "Base directory: $profile_dir"
echo "Cheatsheets list: $cheatsheets_file"
echo
if (( ${#cheatsheets[@]} == 0 )); then
echo "No configured cheatsheets."
else
echo "Configured cheatsheets:"
echo
for cheatsheet in "${cheatsheets[@]}"; do
echo " - $cheatsheet"
done
fi
}
#
# Displays configured cheatsheets from a profile.
#
# Arguments:
#
# $1 (config_dir)
# Base directory under which all configuration files are stored.
#
# $2 (profile)
# Profile name.
#
# $3 (overlay_mode)
# Specify if cheatsheets should be shown in overlay mode.
#
# Returns:
# 0 on success, 1 otherwise.
#
function command_display() {
local config_dir="$1"
local profile="$2"
local overlay_mode="$3"
local profile_dir="$config_dir/profiles/$profile"
local cheatsheets_file="$profile_dir/cheatsheets"
local overlay_limits_file="$profile_dir/overlay"
local caller_window_id caller_window_title window_title
local cheatsheets=()
local -A overlay_limits
if [[ ! -d $profile_dir ]]; then
error "No such profile found under $profile_dir"
return 1
fi
if [[ -e $cheatsheets_file ]]; then
readarray -t cheatsheets < <(grep --invert-match --extended-regexp "(^[[:blank:]]*#|^[[:blank:]]*$)" "$cheatsheets_file")
fi
if (( overlay_mode )); then
overlay_limits[_ALL_]=1
elif [[ -e $overlay_limits_file ]]; then
while read -r window_title; do
if [[ -n $window_title ]]; then
overlay_limits[$window_title]=1
fi
done < <(grep --invert-match --extended-regexp "(^[[:blank:]]*#|^[[:blank:]]*$)" "$overlay_limits_file")
else
# Means that cheatsheets are not shown as overlays. Assignment
# introduced to avoid unbound variable errors in checks.
overlay_limits[_DISABLED_]=1
fi
if (( ${#cheatsheets[@]} == 0 )); then
warning "No cheatsheets are defined under $cheatsheets_file"
return 0
fi
if (( ! overlay_limits[_DISABLED_] )); then
caller_window_id=$(xprop -root | grep '^_NET_ACTIVE_WINDOW(WINDOW)' | sed -e 's/.*window id # //')
caller_window_title=$(xprop -id "$caller_window_id" _NET_WM_NAME | sed -Ee 's/^[^=]+ = "//;s/"$//')
fi
local pqiv_options=(
"--scale-mode-screen-fraction=1.0"
"--zoom-level=1.0"
"--transparent-background"
"--hide-info-box"
"--window-title=cheatsheet-viewer-$profile"
)
pqiv "${pqiv_options[@]}" "${cheatsheets[@]}" &
# The ${caller_window_title-_} is used to avoid invalid array subscript.
if (( overlay_limits[_ALL_] || overlay_limits[${caller_window_title-_}] )); then
local counter=0
while (( counter < 10 )) && ! wmctrl -l | grep -q "cheatsheet-viewer-$profile"; do
sleep 0.05
(( counter+=1 ))
done
wmctrl -F -r "cheatsheet-viewer-$profile" -b add,above
wmctrl -i -a "$caller_window_id"
fi
}
#
# Toggles display of configured cheatsheets from a profile.
#
# Arguments:
#
# $1 (config_dir)
# Base directory under which all configuration files are stored.
#
# $2 (profile)
# Profile name.
#
# $3 (overlay_mode)
# Specify if cheatsheets should be shown in overlay mode.
#
# Returns:
# 0 on success, 1 otherwise.
#
function command_toggle() {
local config_dir="$1"
local profile="$2"
local overlay_mode="$3"
local profile_dir="$config_dir/profiles/$profile"
local cheatsheets_file="$profile_dir/cheatsheets"
local last_cheatsheet_file="$profile_dir/last"
local overlay_limits_file="$profile_dir/overlay"
local current_process_id current_profile last_cheatsheet caller_window_id caller_window_title window_title
local cheatsheets=()
local -A overlay_limits
if [[ ! -d $profile_dir ]]; then
error "No such profile found under $profile_dir"
return 1
fi
if [[ -e $cheatsheets_file ]]; then
readarray -t cheatsheets < <(grep --invert-match --extended-regexp "(^[[:blank:]]*#|^[[:blank:]]*$)" "$cheatsheets_file")
fi
if (( overlay_mode )); then
overlay_limits[_ALL_]=1
elif [[ -e $overlay_limits_file ]]; then
while read -r window_title; do
if [[ -n $window_title ]]; then
overlay_limits[$window_title]=1
fi
done < <(grep --invert-match --extended-regexp "(^[[:blank:]]*#|^[[:blank:]]*$)" "$overlay_limits_file")
else
# Means that cheatsheets are not shown as overlays. Assignment
# introduced to avoid unbound variable errors in checks.
overlay_limits[_DISABLED_]=1
fi
if (( ${#cheatsheets[@]} == 0 )); then
warning "No cheatsheets are defined under $cheatsheets_file"
return 0
fi
if [[ -e $last_cheatsheet_file ]]; then
last_cheatsheet=$(< "$last_cheatsheet_file")
else
last_cheatsheet=""
fi
# Get information about existing running cheatsheet viewer pqiv instance (if any).
current_process_id=$(wmctrl -lp | grep 'cheatsheet-viewer-' | awk '{print $3}')
current_profile=$(wmctrl -lp | grep 'cheatsheet-viewer-' | awk '{print $5}' | sed -e 's/cheatsheet-viewer-//')
# Kill off the existing running process.
if [[ -n $current_process_id ]]; then
kill "$current_process_id"
fi
# If requested profile is not the same as current one, we want to
# show cheatsheets from requested profile.
if [[ $current_profile != "$profile" ]]; then
if (( ! overlay_limits[_DISABLED_] )); then
caller_window_id=$(xprop -root | grep '^_NET_ACTIVE_WINDOW(WINDOW)' | sed -e 's/.*window id # //')
caller_window_title=$(xprop -id "$caller_window_id" _NET_WM_NAME | sed -Ee 's/^[^=]+ = "//;s/"$//')
fi
local pqiv_options=(
"--scale-mode-screen-fraction=1.0"
"--zoom-level=1.0"
"--transparent-background"
"--hide-info-box"
"--window-title=cheatsheet-viewer-$profile"
)
if [[ -n $last_cheatsheet && -f $last_cheatsheet ]]; then
pqiv_options+=("--action=goto_file_byname($last_cheatsheet); set_status_output(1)")
else
pqiv_options+=("--action=set_status_output(1)")
fi
(
while read -r line; do
eval "$line"
echo "$CURRENT_FILE_NAME" > "$profile_dir/last"
done < <(pqiv "${pqiv_options[@]}" "${cheatsheets[@]}" | grep --line-buffered '^CURRENT_FILE_NAME=')
) &
# The ${caller_window_title-_} is used to avoid invalid array subscript.
if (( overlay_limits[_ALL_] || overlay_limits[${caller_window_title-_}] )); then
local counter=0
while (( counter < 10 )) && ! wmctrl -l | grep -q "cheatsheet-viewer-$profile"; do
sleep 0.05
(( counter+=1 ))
done
wmctrl -F -r "cheatsheet-viewer-$profile" -b add,above
wmctrl -i -a "$caller_window_id"
fi
fi
}
# Set-up colours for message printing if we're not piping and terminal is
# capable of outputting the colors.
_COLOR_TERMINAL=$(tput colors 2>&1)
if [[ -t 1 ]] && (( _COLOR_TERMINAL > 0 )); then
_TEXT_BOLD=$(tput bold)
_TEXT_WHITE=$(tput setaf 7)
_TEXT_BLUE=$(tput setaf 6)
_TEXT_GREEN=$(tput setaf 2)
_TEXT_YELLOW=$(tput setaf 3)
_TEXT_RED=$(tput setaf 1)
_TEXT_RESET=$(tput sgr0)
else
_TEXT_BOLD=""
_TEXT_WHITE=""
_TEXT_BLUE=""
_TEXT_GREEN=""
_TEXT_YELLOW=""
_TEXT_RED=""
_TEXT_RESET=""
fi
# Set-up functions for printing coloured messages.
function debug() {
if [[ $DEBUG != 0 ]]; then
echo "${_TEXT_BOLD}${_TEXT_BLUE}[DEBUG]${_TEXT_RESET}" "$@"
fi
}
function info() {
if [[ $QUIET == 0 ]]; then
echo "${_TEXT_BOLD}${_TEXT_WHITE}[INFO] ${_TEXT_RESET}" "$@"
fi
}
function success() {
if [[ $QUIET == 0 ]]; then
echo "${_TEXT_BOLD}${_TEXT_GREEN}[OK] ${_TEXT_RESET}" "$@"
fi
}
function warning() {
echo "${_TEXT_BOLD}${_TEXT_YELLOW}[WARN] ${_TEXT_RESET}" "$@" >&2
}
function error() {
echo "${_TEXT_BOLD}${_TEXT_RED}[ERROR]${_TEXT_RESET}" "$@" >&2
}
# Define error codes.
SUCCESS=0
ERROR_ARGUMENTS=1
# Disable debug and quiet modes by default.
DEBUG=0
QUIET=0
# Default paths, directories etc.
CONFIG_DIR="$HOME/.config/cheatsheet_viewer"
# Default option values.
OVERLAY_MODE=0
# If no arguments were given, just show usage help.
if [[ -z ${1-} ]]; then
short_help
exit "$SUCCESS"
fi
# Parse the arguments
while getopts "oqdvh" opt; do
case "$opt" in
o) OVERLAY_MODE=1;;
q) QUIET=1;;
d) DEBUG=1;;
v) version
exit "$SUCCESS";;
h) long_help
exit "$SUCCESS";;
*) short_help
exit "$ERROR_ARGUMENTS";;
esac
done
i=$OPTIND
shift $(( i-1 ))
# Quiet and debug are mutually exclusive.
if [[ $QUIET != 0 && $DEBUG != 0 ]]; then
error "Quiet and debug options are mutually exclusive."
exit "$ERROR_ARGUMENTS"
fi
COMMAND="${1-}"
shift
if [[ $COMMAND == list ]]; then
command_list "$CONFIG_DIR"
elif [[ $COMMAND == info ]]; then
PROFILE="${1-}"
shift
if [[ -z $PROFILE ]]; then
error "Profile name must be specified."
exit "$ERROR_ARGUMENTS"
fi
command_info "$CONFIG_DIR" "$PROFILE"
elif [[ $COMMAND == display ]]; then
PROFILE="${1-default}"
shift
command_display "$CONFIG_DIR" "$PROFILE" "$OVERLAY_MODE"
elif [[ $COMMAND == toggle ]]; then
PROFILE="${1-default}"
shift
command_toggle "$CONFIG_DIR" "$PROFILE" "$OVERLAY_MODE"
else
error "Unsupported command: $COMMAND"
exit "$ERROR_ARGUMENTS"
fi
|