krunner bash plugin

krunner plugin for display our personal lists generated by our bash scripts, can also run a command

Examples:
pacman -Qs, run pac:* link: open pamac page

get last forum news, run new:, link open forum page

df: link: open dolpin

ps: link load ksysguard and copy PID

docker: start or stop systemd service

err: display last journald errors

possibilities are endless ...


How to
Create a bash file ~/.config/krunnershell.sh
for each action, create one or 2 bash functions, for exemple match_ls(){} run_ls(){} for filter ls: in krunner

in match_() function
if link action is different from title, we return 2 values with "||" as separator ("http://manjaro||page title||[hint]"). First column is send to run_
() fonction

File example

# ~/.config/krunnershell.sh
#
# kill "/usr/lib/qt/plugins/krunner_shell.py" after add/remove action (or reboot)
#

match_ls(){
  # return list
  [ -z "$1" ] && exit 0
  [[ ${#1} < 2 ]] && exit 0
  match="${1}*"
  #ls -dHp -1 $match --color=never # | awk '{print $(NF)}'
  while read -r line; do
    echo "${line}||${line:1}" # only for test diff data<->title
  done < <(ls -dHp -1 $match --color=never)
}
run_ls (){
  xdg-open "$1"
  exit 0
  if [[ -d "$1" ]]; then
    dolphin "$1"
  else
    kate "$1"
  fi
}

match_pac(){
  pacman -Qq | grep "$1"
}
run_pac(){
  qdbus org.kde.klipper /klipper org.kde.klipper.klipper.setClipboardContents $1
  qdbus org.kde.plasmashell /org/kde/osdService showText 'manjaro'  "$1"
  pamac-manager --details=$1
}

match_df() {
  while read -r line; do
    echo "${line##* }||${line}"
  done < <(df -x tmpfs| awk '/^\// {print $1"\t"$5" "$(NF)}')
}
run_df(){
  dolphin $1
}

match_err(){
  while read -r m j h m u mes; do
    echo "${j} ${h:0:5} ${u%%\[*} ${mes}"
  done < <(SYSTEMD_COLORS=0 journalctl -b0 -p3 -r -n12 --no-tail --no-pager)
}

match_ps(){
  while read -r u p t c; do
    echo "$p||$u $p $c $t"
  done < <(ps -aux -t|grep $1|grep -v '"'|grep -v "grep"|awk '{print $1" "$2" "$8" "$(NF)}'|head -n10)
}
run_ps(){
  qdbus org.kde.klipper /klipper org.kde.klipper.klipper.setClipboardContents $1  #save PID
  qdbus org.kde.plasmashell /org/kde/osdService showText 'manjaro'  "$1"
  ksysguard    # can paste PID in search bar
}

match_docker(){
  [[ $(systemctl show docker --no-pager | grep -c "ActiveState=active") == 1 ]] && echo "stop" || echo "start"
}
run_docker(){
  pkexec systemctl "$1" docker
}

match_temp(){
  sensors | grep high --color=never
}

match_new(){
python - "$0"<<'EOF'
import os, sys, json, urllib.request
with urllib.request.urlopen(f"https://forum.manjaro.org/c/announcements.json") as f_url:
    req = f_url.read()
topics = json.loads(req)['topic_list']['topics']
for topic in [t for t in topics if not t['title'].startswith('About') and not t['closed']]:
    print(f"https://forum.manjaro.org/t/{topic['slug']}/{topic['id']}/||{topic['fancy_title']}")
EOF
}
run_new(){
  xdg-open "$1"
}

Installation
not in aur

wget https://raw.githubusercontent.com/papajoker/krunner-shell/master/resources/PKGBUILD
makepkg -sfci

After install run kquitapp5 krunner if not reboot
empty bash script installed: we write it according to our needs
Exists action about: for list actions available


Tips
krunner is small by default, I change width

2 Likes

new action/function examples:

man: :green_heart: search in man and display page in web browser

match_man(){
  while read -r name id sep txt; do
    echo -e "${name}||${name}:\t${txt}"
  done < <(man -k "$1")
}
run_man(){
  qdbus org.kde.plasmashell /org/kde/osdService showText 'help' "$1"
  man -Thtml "$1" >"/tmp/krunner-man.html" && xdg-open "/tmp/krunner-man.html"
}

radio: play web radio with clementine or vlc

match_radio(){
  tmp="/tmp/krunner-radios.lst"
  echo "" > "$tmp"
  echo "http://ais-sa2-dal01-1.cdnstream.com:80/1989_64.aac||Rock mix||rock" >> "$tmp"
  echo "http://node1.mingusradio.com:7646/rock||Mingus radio||rock" >> "$tmp"
  echo "http://stream.punkrockers-radio.de:8000/mp3||PunkRockers||punk"  >> "$tmp"
  echo "http://94.23.26.22:8090/live.mp3||Punk fm||punk"  >> "$tmp"
  grep -i "$1" "$tmp" --color=never   # can filter list by name,genre or not
}
run_radio(){
  if pacman -Qq clementine 2>/dev/null; then
    qdbus org.mpris.MediaPlayer2.clementine /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.OpenUri "$1" ||clementine "$1"
    return 0
  fi
  if pacman -Qq vlc 2>/dev/null; then
    qdbus org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.OpenUri "$1" || vlc "$1"
    return 0
  fi
}
1 Like

Forum kindly sponsored by