You are not logged in.

#3301 2020-01-05 21:23:42

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Post your handy self made command line utilities

In the spirit of reinventing the wheel, and doing it badly... I dumped all AUR helpers last year and the one piece of functionality I was missing was searching for packages (other than using surfraw, which is fine). So I use this. Let the witchburning begin!

#!/usr/bin/env bash
# Search for packages in repos and AUR

package=$1
url='https://aur.archlinux.org/rpc/?v=5&type=search&by=name&arg='
aurfile='/tmp/aursearch'

pacman -Ss "^${package}$"

status=$?
if (( "$status" != 0 )); then
  printf "%s\n" "Not in repos, searching AUR..."

  trap "rm '${aurfile}'" EXIT INT TERM

  curl -s "${url}$1" -o "$aurfile"
  awk '{gsub(/"/,"");  printf "• %s\n", $0}' \
      <(jshon -e results -a -e Name < "$aurfile")
fi

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3302 2020-01-05 21:28:01

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: Post your handy self made command line utilities

jasonwryan wrote:

I dumped all AUR helpers last year and the one piece of functionality I was missing was searching for packages (other than using surfraw, which is fine). So I use this. Let the witchburning begin!

Okay, let me start.

"Hey, not only do you use AUR helpers, you're the author of one!"


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#3303 2020-01-05 22:17:17

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Post your handy self made command line utilities

The only way AUR helpers could be compared to wheels would be to compare them to wheels on a boat.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3304 2020-01-06 06:03:39

waitnsea
Member
From: France
Registered: 2013-02-10
Posts: 59

Re: Post your handy self made command line utilities

Trilby wrote:

The only way AUR helpers could be compared to wheels would be to compare them to wheels on a boat.

Sure, caterpillars work better, and here
    it's not an advertisement, it's pure ecological delirium !

Last edited by waitnsea (2020-01-06 09:43:25)

Offline

#3305 2020-01-08 12:22:04

WorMzy
Administrator
From: Scotland
Registered: 2010-06-16
Posts: 12,411
Website

Re: Post your handy self made command line utilities

Trilby wrote:

The only way AUR helpers could be compared to wheels would be to compare them to wheels on a boat.

https://en.wikipedia.org/wiki/Paddle_st … dle_wheels ?


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

#3306 2020-01-08 12:26:31

schard
Forum Moderator
From: Hannover
Registered: 2016-05-06
Posts: 2,112
Website

Re: Post your handy self made command line utilities

WorMzy wrote:
Trilby wrote:

The only way AUR helpers could be compared to wheels would be to compare them to wheels on a boat.

https://en.wikipedia.org/wiki/Paddle_st … dle_wheels ?

It's funny, because AUR helpers actually (mostly) are outdated technology with low efficency.


Inofficial first vice president of the Rust Evangelism Strike Force

Offline

#3307 2020-01-08 13:31:12

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Post your handy self made command line utilities

And most of them are also steaming piles of ship!


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3308 2020-01-09 10:36:09

Awebb
Member
Registered: 2010-05-06
Posts: 6,610

Re: Post your handy self made command line utilities

I find AUR helpers immensely useful. You can search the AUR, show infos about packages and even be informed about updates with a few quick commands... Then you feed that information to your own scripts and call it a day.

The buildorder command of auracle is really nice.

Offline

#3309 2020-01-15 01:01:43

Alad
Wiki Admin/IRC Op
From: Bagelstan
Registered: 2014-05-04
Posts: 2,418
Website

Re: Post your handy self made command line utilities

From what I've seen, people outright claiming there is no use for AUR helpers have a very limited amount of AUR packages. When you have* more than say, 100 AUR packages, some automation is in order.

To get back to the topic at hand, jq might be worth looking into for the script in #3301.

* "have" can both mean "use" and "maintain". I've certainly seen people maintain more than 300 AUR packages. An AUR helper can just as well assist with maintanance tasks.

Last edited by Alad (2020-01-15 01:04:20)


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby

Offline

#3310 2020-01-15 02:18:13

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Post your handy self made command line utilities

Alad wrote:

To get back to the topic at hand, jq might be worth looking into for the script in #3301.

How so? I have always used jshon because it was community contribution. What advantages are there to jq that would make it a worthwhile change?


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3311 2020-01-15 02:31:59

Alad
Wiki Admin/IRC Op
From: Bagelstan
Registered: 2014-05-04
Posts: 2,418
Website

Re: Post your handy self made command line utilities

Mostly that it would reduce your script to 1-2 lines? E.g there's no need to remove quotes, because of the -r flag.

Last edited by Alad (2020-01-15 02:32:16)


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby

Offline

#3312 2020-01-23 20:02:41

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Post your handy self made command line utilities

@jasonwryan, I have a few questions about your script #3301
Why do you use curly braces in the trap?
With pacman you search for exact results, why didn't you set it for the AUR search also, any specific reason?
You set variable 'package=$1', the curl command uses '$1', why? Thanks.

Offline

#3313 2020-01-23 20:31:15

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Post your handy self made command line utilities

That was an earlier iteration. The current version also searches AUR.

The curly braces are for legibility, and the variable package was setting up for the larger script (I pasted it here for feedback before going further in case I screwed something obvious up) smile


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3314 2020-01-23 21:47:40

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Post your handy self made command line utilities

Yes clear. I have already used the one from #3301 works nice and it gave me a clear picture how to use the AUR API wink
I have created a script with the help of yours to update all my AUR packages trough the API, if I think it's ready to publish I will.
I will have a look at the new one you published tomorrow, thanks.

Offline

#3315 2020-01-24 10:38:12

Alad
Wiki Admin/IRC Op
From: Bagelstan
Registered: 2014-05-04
Posts: 2,418
Website

Re: Post your handy self made command line utilities

I have created a script with the help of yours to update all my AUR packages trough the API, if I think it's ready to publish I will.

No! More AUR helpers! See what you've done, triangle thingy! wink

Last edited by Alad (2020-01-24 10:39:29)


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby

Offline

#3316 2020-01-25 17:54:53

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Post your handy self made command line utilities

Nice script @jasonwryan, the one on 'shiv'. Though I personally like the pacman search more because it can give you colored output which gives a better separation in my script and shows if the package is already installed, but I think that's personal;)
I adapted the jshon check also.
Here is a first draft based on #3301

#!/bin/bash
# shellcheck disable=2162
#set -x
# AUR helper that leaves you in control

#exclude file, don't search for this package, one package(name only) per line
exclude="$HOME"/bin/exclude/aur-update-excl
buildir="$HOME"/build

#Don't touch the URL's or variables below only if you must
url1='https://aur.archlinux.org/rpc/?v=5&type=search&by=name&arg=' #search api
url2='https://aur.archlinux.org/rpc/?v=5&type=info&arg[]='     #info api
url3='https://aur.archlinux.org/cgit/aur.git/snapshot/'      #get package

package=$2
aurfile='/tmp/aurfile'
result='/tmp/result'
trap 'rm -f "$aurfile" "$result"' EXIT INT TERM

type jshon > /dev/null 2>&1 || {
  printf "%s\n" "No jshon, bailing...";
  exit 1;
}

_reposearch() {
  pacman -Ss "^${package}$"

  status=$?
  if [ "$status" != 0 ]; then
    echo "• Not in repos, searching AUR..."
    printf "\n" && _aursearch
  elif [ "$status" = 0 ]; then
    printf "\n" && read -p "• Search AUR too? Y or N : " aur
    printf "\n" && [[ "$aur" =~ ([Yy]) ]] && _aursearch || exit
  fi
}

_aursearch() {
    curl -s "${url1}$package" -o "$aurfile"
    mapfile -t aur < <(jshon -e results -a -e Name -u < "$aurfile")

    [ ${#aur[@]} -eq 0 ] && printf "• Also not in AUR" && unset aur && exit
    printf "%s\n" "• Exit or download one of the AUR result/s"

    select name in "${aur[@]}" exit; do
      case $name in
          exit) unset aur && exit;;
             *) printf "%s\n" "$name" > "$result"; printf "\n"; break;;
      esac
    done
    [ -n "$result" ] && unset aur && _get || exit
}

#_checkupd() {
#  aurprog=/tmp/aurprog
#  aurinfo=/tmp/aurinfo
#  aurver=/tmp/aurver
#  localver=/tmp/localver
#  trap 'rm "$aurinfo" "$aurprog" "$aurver" "$localver"' EXIT INT TERM

#  if [ -e "$exclude" ]; then
#    pacman -Qm  > "$aurprog"
#    while read -r excl; do
#     sed -i "/$excl/d" "$aurprog"
#    done < "$exclude"
#  else
#    pacman -Qm > "$aurprog"
#  fi

#  while read -r prog version; do
#    echo "$version" > "$localver"

#    curl -s "${url2}$prog" -o "$aurinfo"
#    jshon -e results -a -e Version -u < "$aurinfo" > "$aurver"

#    [ "$( diff "$localver" "$aurver" )" != '' ] &&
#     printf "%s\n" "• Update for $prog New version is $(<"$aurver")" && _get
#  done < "$aurprog"
#}

_get() {
  [ -e "$result" ] && prog="$(<"$result")"
  [ "$1" = -g ] && prog="$package"
  [ -d "$buildir/$prog" ] &&
   mv "$buildir/$prog" "$buildir/${prog}.bak"
  curl -sL "${url3}${prog}.tar.gz" -o "$buildir/${prog}.tar.gz"

  tar zxvf "$buildir/${prog}.tar.gz" -C "$buildir" && rm "$buildir/${prog}.tar.gz"

  printf "%s\n" "• Downloaded $prog & extracted to $buildir/$prog"
}

_pass() {
  [ "$1" = -r ] && _reposearch
  [ "$1" = -a ] && _aursearch
  [ "$1" = -g ] && _get "$@"
#  [ "$1" = -u ] && _checkupd
  [ "$1" = "" ]
  [ -z "$1" ] && printf "%s\\n" "Options: [ -r <package-name> #search package in official repo or/and in AUR ]
         [ -a <package-name> #search if package excist in AUR ]
         [ -u #check for AUR updates ]
         [ -g <package-name> #get AUR package ]"
}
_pass "$@"

exit 0

edit: the exclude file only works for the update part, not the search..;)
made small clean changes, no need for awk at all, 'u - unstring' is enough for this script

edit2: for now I have disabled the check for updates, because not all git packages update their PKGBUILD
There are packages which are good like 'profile-sync-daemon' but a few are not like 'archiso-git'
It would be nice if pkgver was updated to the correct version;) then for scripts like this one you don't have to invent the wheel...

Last edited by qinohe (2020-01-27 17:08:09)

Offline

#3317 2020-02-13 13:27:56

neol09
Member
Registered: 2020-02-13
Posts: 13

Re: Post your handy self made command line utilities

Switching between two languages is straightforward
But i wanted to switch between 3 languages and display the status

#!/bin/bash
languages=(us il ru)
timeout=1s
file="/tmp/${USER}_LANG"

[ "$file" ] && [ ! -f "$file" ] && touch "$file" && chmod 600 "$file"

while IFS=, read -ra arr; do
  current="${arr[0]:=${languages[0]}}" last="${arr[1]:=${languages[1]}}"
done < <( setxkbmap -query | sed -En '/layout/{s/.* //p}' )

setLang() {
  setxkbmap "$1,$last"
  [ "$file" ] && echo "[${1}]:${last}" > "$file"
  "$0" rest &
}

if [ "$1" = rest ]; then
  sleep "$timeout"
  exit 1
fi

if pgrep -fl "${0##*/} rest" || [ "$last" = "$current" ]; then
  for L in "${languages[@]}"; do (( a++ ))
    if [ "$L" = "$current" ]; then
      setLang "${languages[$((a % ${#languages[@]}))]}"
      break
    fi
  done
else
  buff=$last last=$current
  setLang "$buff"
fi

an example for i3status

read_file lang {
path = /tmp/noel09_LANG
format = "%title: %content   "
}

and a tmux example

set -g status-right "#(cat /tmp/noel09_LANG)"

I enjoy bash, i want get better. roast me!

[edit] updated quoting,
Credit for the implementation goes to gradgrind
Haven't found other ways of getting the active keyboard layout.

[edit2] Is it over-complicated yet?
[edit3] down to ~35 lines from ~80

Last edited by neol09 (2020-02-17 04:51:23)

Offline

#3318 2020-02-13 14:52:07

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Post your handy self made command line utilities

Only had a quick glance, but quoting isn't right, you do when you shouldn't '~=' and you don't when you should;)
Advice, install shellcheck or shellcheck-static (AUR) which is without Haskell 'bloat' (if you don't need it).
Shellcheck will at least help you spot really bad things. It may be wrong in rare occasions.
The script masters here may have more good advices..
Welcome to arch wink

Offline

#3319 2020-02-13 15:48:27

neol09
Member
Registered: 2020-02-13
Posts: 13

Re: Post your handy self made command line utilities

I've used shellcheck in the past but I've never heard about shellcheck-static! sounds lovely.
will update the post implementing all of it's adivce

Thanks for the warm welcome!

Offline

#3320 2020-02-16 18:01:27

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Post your handy self made command line utilities

neol09 wrote:

[edit2] Is it over-complicated yet?

Well, kinda yes tongue for something that can be done with 'setxkbmap us/il/ru'
It does work though. I haven't got a use for it anyway but sure you can simplify it.
Hobby away I'm sure you figure it out.

edit: at least one example;)

setxkbmap -query -symbols | awk '/layout/{print $2}'

oops, of course I meant

setxkbmap -query |awk '/layout/{print $2}'

Last edited by qinohe (2020-02-16 18:52:48)

Offline

#3321 2020-02-17 04:47:16

neol09
Member
Registered: 2020-02-13
Posts: 13

Re: Post your handy self made command line utilities

up until recently i was using

setxkbmap -layout us,il,ru -option grp:lalt_lshift_toggle

The problem with that is that you can't get the active layout.
cause setxkbmap -query returns the whole array of languages in it's original order. so some form of scripting was necessary either way

I realize the simplest way of handling this would be something like

case setxkbmap -query | sed -n '/layout/{s/.* //p}' in
  us) setxkbmap il ;;
  il) setxkbmap ru ;;
  ru|*) setxkbmap us ;;
esac

infact that's what i started with trying to change two aspects of it
1) i wanted to declare languages once, hardcoding as little as possible
2) i wanted a wanted a way to switch back to the last active layout instead of having to cycle through all of them

The cache file was originally supposed to hold a current & last language aswell as a timestamp, the timestamp i've already gotten rid by taking a rest
I suppose i could get rid of the cache entirely if i were to pass the second language to setxkbmap aswell

setxkbmap "$1,$last"

But that would leave me with having to setxkbmap -query from every source i'd like to display this info, which is a bit troublesome for me.

I've polished (or made a mess of-) it some more, updating OP. really appreciate your feedback \o/
It is /pretty much/ done tho

Offline

#3322 2020-02-17 10:05:01

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Post your handy self made command line utilities

It's not that I don't like your script, but it 's like using a Caterpillar to get a sugar cube in your tea wink
I was thinking something like

lang() { [ -n "$1" ] && setxkbmap "$1" || setxkbmap -query | awk '/layout/{print $2}' }

Put that in your shell rc, use 'lang' to find out which lang is used & use 'lang il/ru/us' to change it...

Offline

#3323 2020-02-17 12:01:10

neol09
Member
Registered: 2020-02-13
Posts: 13

Re: Post your handy self made command line utilities

I'll have a cup of coffee, thank you lol

qinohe wrote:
lang() { [ -n "$1" ] && setxkbmap "$1" || setxkbmap -query | awk '/layout/{print $2}' }

There's an inherit flaw with this approach I believe, Doesn't setxkbmap take arguments exclusively in english?

But it's true that script is too specific for my scenario to be useful, oh well.


here's a snippet before i shut up, for those evil web-services that accept pictures in textboxes
I'll spare you from the boiler-plating this time wink

scrot -q 100 -z -s -e 'xclip -sel clip -t $$(file -b --mime-type "$f") $f; rm $f'

Offline

#3324 2020-02-17 13:41:36

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Post your handy self made command line utilities

neol09 wrote:

There's an inherit flaw with this approach I believe, Doesn't setxkbmap take arguments exclusively in english?

Right, haven't thought about that, I never use Hebrew or Cyrillic keyboards.
Try to think outside the box, I'm sure can let that one-liner help you, keybinding springs to mind...

Now I shut up too, this isn't a support thread and I don't need to be tapped on the finger...
If you need more open a thread.
BTW. I'll have coffee too..;)

Offline

#3325 2020-02-18 16:15:43

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Post your handy self made command line utilities

@neol09, I was editing a dmenu script and thought why not dmenu...

A keyboard switcher in dmenu

#!/bin/sh

[ -f "$XDG_CONFIG_HOME"/dmenu/dmenurc ] &&
 . "$XDG_CONFIG_HOME"/dmenu/dmenurc || dmindex='dmenu -l 5'

trap clean EXIT HUP INT QUIT TERM
clean() {
  unset kbd && exit 0
}

_menu() {
  kbl=$(printf "il\nru\nus\nlang" \
   | $dmindex -p 'keyboard layout:' -w 240)
}
_menu

if [ "$kbl" = lang ]; then
   setxkbmap -query | awk '/layout/{print $2}' \
   | $dmindex -p 'current layout:' -w 240

elif [ -n "$kbl" ]; then
  setxkbmap "$kbl"
fi

If you don't have a dmenurc here is one:

# RAL 9004 0n RAL 9003 & choice RAL 9007 or reversed.
dmindex='dmenu -i -l 10 -fn DejaVu-Sans-Mono-10 -nb #2e3032 -nf #f4f8f4 -sb #2e3032 -sf #8f8f8c'
#dmindex='dmenu -i -l 10 -fn DejaVu-Sans-Mono-10 -nb #f4f8f4 -nf #2e3032 -sb #8f8f8c -sf #2e3032'

Offline

Board footer

Powered by FluxBB