Browse Source

pkgsearch: --preview option and config file

config file stills needs documenting
DanielFGray 2 years ago
parent
commit
67f0a2793b
1 changed files with 51 additions and 19 deletions
  1. 51
    19
      pkgsearch

+ 51
- 19
pkgsearch View File

@@ -1,11 +1,14 @@
1 1
 #!/usr/bin/env bash
2 2
 
3 3
 declare -r esc=$'\033'
4
-declare -r c_reset="${esc}[0m"
5
-declare -r c_red="${esc}[31m"
6
-declare -r c_green="${esc}[32m"
7
-declare -r c_blue="${esc}[34m"
4
+declare -r c_red=$(tput setaf 1)
5
+declare -r c_blue=$(tput setaf 4)
6
+declare -r c_reset=$(tput sgr0)
7
+declare -a config_err
8
+declare -r config_dir="${XDG_CONFIG_DIR:-$HOME/.config}/pkgsearch"
9
+declare -r config_file="${config_dir}/conf"
8 10
 declare reverse
11
+declare preview_pos='right:hidden'
9 12
 
10 13
 usage() {
11 14
   LESS=-FEXR less <<HELP
@@ -13,6 +16,9 @@ pkgsearch [options] [query]
13 16
   lists and installs packages from your distro's repositories
14 17
 
15 18
   -r --reverse    reverse the orientation of the layout
19
+  -p --preview    options for the preview window
20
+                  is passed directly to fzf --preview-window
21
+                  defaults to 'right:hidden'
16 22
   -h --help       show this help
17 23
 
18 24
   without any arguments pkgsearch will list all available packages from your cache
@@ -30,7 +36,7 @@ die() {
30 36
 
31 37
 has() {
32 38
   local verbose=0
33
-  if [[ $1 == '-v' ]]; then
39
+  if [[ $1 = '-v' ]]; then
34 40
     verbose=1
35 41
     shift
36 42
   fi
@@ -64,10 +70,32 @@ select_from() {
64 70
 fzf() {
65 71
   command fzf -e +s --multi --cycle --ansi \
66 72
     ${reverse:+--reverse} \
73
+    --bind='Ctrl-X:toggle-preview' \
74
+    --preview-window="$preview_pos" \
67 75
     --no-hscroll --inline-info \
68 76
     --header='tab to select multiple packages, Ctrl-X for more info on a package' "$@"
69 77
 }
70 78
 
79
+parse_config_file() {
80
+  local line k v nr=0
81
+  config_err=()
82
+  while IFS= read -r line; do
83
+    (( ++nr ))
84
+    [[ -z "$line" || "$line" = '#'* ]] && continue
85
+    k="${line%% *}"
86
+    v="${line#* }"
87
+    if [[ -z "$v" ]]; then
88
+      config_err+=( "missing value for \"$k\" in config file on line $nr" )
89
+      continue
90
+    fi
91
+    case "$k" in
92
+      reverse) [[ "$v" = 'true' ]] && reverse="1" ;;
93
+      preview) preview_pos="$v" ;;
94
+      *) config_err+=( "unknown key \"$k\" in config file on line $nr" )
95
+    esac
96
+  done
97
+}
98
+
71 99
 install() {
72 100
   local pkgs count
73 101
   mapfile -t pkgs
@@ -79,9 +107,7 @@ install() {
79 107
 }
80 108
 
81 109
 debian() {
82
-  fzf --bind='Ctrl-X:toggle-preview' \
83
-      --preview='p={}; apt-cache show ${p%% *}' \
84
-      --preview-window='up:hidden' \
110
+  fzf --preview='p={}; apt-cache show ${p%% *}' \
85 111
       --query="$1" \
86 112
     < <(apt-cache search '' | sort |
87 113
       sed -u -r "s|^([^ ]+)|${c_green}\1${c_reset}|") |
@@ -93,9 +119,7 @@ arch() {
93 119
   local search packages
94 120
   search='pacman'
95 121
   [[ -n "$1" ]] && search=$(select_from 'pacaur' 'yaourt' 'packer' 'apacman' 'pacman')
96
-  packages=$(fzf --bind='Ctrl-X:toggle-preview' \
97
-      --preview="p=\$(cut -d' ' -f2 <<< {}); $search -Si \$p" \
98
-      --preview-window='up:hidden' \
122
+  packages=$(fzf --preview="p=\$(cut -d' ' -f2 <<< {}); $search -Si \$p" \
99 123
     < <( $search -Ss "$1" |
100 124
       gawk '{
101 125
         getline descr;
@@ -106,16 +130,14 @@ arch() {
106 130
         print repo, name, info, descr;
107 131
       }' blue="$c_blue" green="$c_green" reset="$c_reset"
108 132
     ) | cut -d' ' -f2)
109
-  [[ "$search" == "pacman" ]] && search="sudo pacman"
133
+  [[ "$search" = "pacman" ]] && search="sudo pacman"
110 134
   install "$search -S" <<< "$packages"
111 135
 }
112 136
 
113 137
 void() {
114
-  local package_list packagename='{sub(/-[^\-]*$/, "", $2); print $2}'
138
+  local package_list packagename='{ sub(/-[^\-]*$/, "", $2); print $2 }'
115 139
   package_list=$(xbps-query -Rs '' | sort)
116
-  fzf --bind='Ctrl-X:toggle-preview' \
117
-      --preview="p=\$(awk \"$packagename\" <<< {}); xbps-query -Rx \$p" \
118
-      --preview-window='up:hidden' \
140
+  fzf --preview="p=\$(awk \"$packagename\" <<< {}); xbps-query -Rx \$p" \
119 141
       --query="$1" <<< "$package_list" |
120 142
     awk "$packagename" |
121 143
     install 'xbps-install -S'
@@ -132,8 +154,18 @@ fedora() {
132 154
 
133 155
 while true; do
134 156
   case "$1" in
135
-    -r|--reverse)  reverse=1  ; shift ;;
136
-    -h|--help)     usage      ; exit  ;;
157
+    -r|--reverse)
158
+      reverse=1
159
+      shift
160
+      ;;
161
+    -h|--help)
162
+      usage
163
+      exit
164
+      ;;
165
+    -p|--preview)
166
+      preview_pos="$2"
167
+      shift 2
168
+      ;;
137 169
     *) break
138 170
   esac
139 171
 done
@@ -141,7 +173,7 @@ done
141 173
 has -v fzf gawk || die
142 174
 
143 175
 request="$*"
144
-osrelease=$(awk -F'=' '/^NAME/{gsub(/"/, "", $2); print tolower($2)}' /etc/os-release)
176
+osrelease=$(awk -F'=' '"NAME" == $1 { gsub("\"", "", $2); print tolower($2); }' /etc/os-release)
145 177
 case "${osrelease%% *}" in
146 178
   debian|ubuntu) debian "$request" ;;
147 179
   arch) arch "$request" ;;

Loading…
Cancel
Save