You are not logged in.

#326 2018-11-08 17:12:47

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

Re: Dmenu Hacking Thread

Unstack wrote:

dmenu hack for querying Google. [GIF Preview (>1mb)]

Tell me what you think.

Hey Unstack, I like your tool, especially the 'back & cancel' button.
I'm a nitwit in python but I'm going to try to use that part for other dmenu script, like osx-finder or git-jumper.

As for what your tool actually does, I miss the address together with the search, so you get something like: search enter back enter back enter back etc...
If the URL was visible in the search it would be a winner , for me;)


A little tool I made, alarm based on 'sleep':

#!/bin/sh
set -xe
if [ -f "$XDG_CONFIG_HOME"/dmenu/dmenurc ]; then
  . "$XDG_CONFIG_HOME"/dmenu/dmenurc
else
  demcmd='dmenu'
  dmalarm='dmenu'
fi

alarm_clock() {
  reason=$(echo '' | $demcmd -p "ReasonForAlarm?" -w '375')
  if [ -z "$reason" ]; then  reason=Alarm; fi
  timer=$(echo '' | $demcmd -p "TimeToAlarm:" -w '375')
  (sleep "$timer" | $dmalarm -p "$reason" -w '540' -x '675' -y '500')
}
alarm_clock

exit 0

+ teh config lines:

demcmd='dmenu -i -fn Monospace-14:normal -nb #222222 -nf #ff7f50 -sb #222222 -sf #f5deb3'
dmalarm='dmenu -fn Monospace-40:normal -sb #222222 -sf #dd0000'

edit: add e to dmcmd, which is also in my list...
edit2: dmenu can run multiple instances of the same script
I added  a reason for alarm(default=Alarm), like movie start or taxi, etc
Right now it shows 16 characters increase last width to change that.

Last edited by qinohe (2018-11-20 16:11:30)

Offline

#327 2018-11-13 11:59:46

msum
Member
Registered: 2017-04-21
Posts: 5

Re: Dmenu Hacking Thread

I often open a new terminal window and want to quickly jump to the working directory that I have in another window. Here is a fragment of my .bashrc for doing that:

choose_from_cwds() {
	for PROCID in $(pgrep '^bash$'); do
		readlink -e /proc/$PROCID/cwd
	done \
	| sort -u \
	| dmenu -l 5
}
alias chd='cd $(choose_from_cwds)'

Offline

#328 2018-11-16 16:13:46

msum
Member
Registered: 2017-04-21
Posts: 5

Re: Dmenu Hacking Thread

Here's another one for browsing git commits and selecting their hash values. I use it for example to select points for a diff.

choose_git_commit() {
    git log --date='format:%Y-%m-%d %H:%M' \
        | paste -sd'\t' \
        | sed 's/commit /\n/g; s/\(Author\|Date\)://g; s/<[^>]*>//g;
               s/ \+/ /g; s/\t\+/  /g' \
        | cut -c -150 \
        | dmenu -i -l 10 \
        | cut -d' ' -f 1
}
alias Gdd='git diff $(choose_git_commit) $(choose_git_commit)'

There is one problem with this script: it is very slow on my elderly laptop (ThinkPad X200). However, on a newer desktop PC, it works smoothly. Apparently, dmenu is not very well suited for long strings. Does anyone know how to improve it?

EDIT:
Solved the performance issue. The problem was not long strings, but tab characters. I changed the script accordingly, now it runs smoothly.

Last edited by msum (2018-11-16 16:23:11)

Offline

#329 2019-01-14 18:58:58

g4c9z
Member
Registered: 2019-01-14
Posts: 1

Re: Dmenu Hacking Thread

My script opens a file somewhere in your home directory by typing part of its name, rather than finding it in a file manager.  It's intended to be assigned to a keyboard combination, so you might press something like Win-Space, then start typing part of a filename, then press Enter.

#!/bin/bash

#This script lets a user select a file to open by typing part of its
#name, kind of like Quicksilver (only lighter-weight).  It's meant to
#be invoked by a shortcut key such as Windows-space.
#
#Depends on:
# dmenu
# locate / updatedb
# xdg-open

#Set this variable to the directory you want to open files in
DIRECTORY_TO_INCLUDE="$HOME"
TEMP_FILE="/tmp/filenames"
TEMP_DB="/tmp/files_db"
TEMP_RESULT="/tmp/result.txt"

make_files_db() {
    #Create the database
    #Unforunately there seems to be no good way to use updatedb to create just a single database with files from
    #multiple different directories.
    #There are 2 versions of updatedb, and GNU locate has an extra option --localpaths which lets you specify
    #multiple paths with one command.  mlocate, the version I'm using, doesn't.
    #However, mlocate is apparently newer, and has the improvement that it reuses the old database
    #to save time, and apparently if you install both then they'll both build their databases in the background.
    updatedb -l 0 -U "$DIRECTORY_TO_INCLUDE" -o "$TEMP_DB" --prune-bind-mounts no

    #Save only the filenames, not the full path, in the temporary file (see below for why).
    #Skip hidden files.
    #The "while read line" trick is like a for loop, but iterates over lines instead of space-delimited
    #items.  I found the trick here:
    #http://ubergibson.com/article/iterating-through-lines-in-the-bash-shell
    locate "$DIRECTORY_TO_INCLUDE" -d "$TEMP_DB" | grep -v "/\." | while read line ; do basename "$line" ; done  > "$TEMP_FILE"
}

if [ "$1" == "-r" ]
then
    #User selected to rebuild the temporary file
    make_files_db
    echo "Temporary file rebuilt"
    exit 0
elif [ ! -f "$TEMP_FILE" ]
then
    echo "Building temporary file... (Use $0 -r to rebuild it - it's recommended to set this to happen regularly, e.g. daily or when your computer starts)"
    make_files_db
fi

#Select a file to edit.
#The selection menu only displays the file's name here, not
#the full path.
#The reason for that is that otherwise there would be too many choices matching the letters you type -
#if the letters match a directory name, then all the files in that directory would also be included in the choices.
chosen_file=`cat "$TEMP_FILE" | dmenu -i`

open_file() {
    #For many programs there's a benefit to setting the current directory to the directory of the file
    cd `dirname $1`

    #Open the file according to how they configured that type of file in their desktop environment
    xdg-open "$1"
}

if [ -n "$chosen_file" ]  #If a file was selected
then
	#Prepend a / to increase the chances of matching only one file.
    #The first grep is for directories, and ensures that only the directory itself is matched and not things in the directory.
    #The second grep excludes hidden files.
    locate -d "$TEMP_DB" /$chosen_file | grep "$chosen_file$" | grep -v "/\." > "$TEMP_RESULT"

	#Count how many files have the filename that was selected.
	lines=`cat "$TEMP_RESULT" | wc -l`

	if [ "1" = "$lines" ]
	then
		#If there's exactly one file of that name, edit it.

		#Get the full path of the file
		full_file=`cat "$TEMP_RESULT"`
	
		open_file "$full_file"
	elif [ "0" = "$lines" ]
	then
		echo "Not found"
	else
		#There were multiple files that share the same filename.
		#Ask the user which file they want to edit by displaying the full path
		#this time.
		full_file=`cat "$TEMP_RESULT" | dmenu -i -p "Which $chosen_file?"`

		if [ -n "$full_file" ]
		then
			open_file "$full_file"
		fi
	fi
fi

Offline

#330 2019-06-17 13:44:16

qurn
Member
Registered: 2017-10-13
Posts: 5

Re: Dmenu Hacking Thread

This is dmenu_beautiful_man.sh
It opens manpages in llpp after a conversion. It's a bit nicer to read than in the terminal in a monospaced font.

#!/bin/bash

if [[ -f $HOME/.config/dmenurc ]]; then
  . $HOME/.config/dmenurc
else
  DMENU="dmenu -i"
fi

PAGE=$( apropos -s 1 . | awk '{print $1}' | $DMENU )
file=$(mktemp /tmp/beautiful_man.XXXXXXXXX)
man -t "$PAGE" > $file
llppac -t ps $file

Works nice with this in the llpp.conf

...
    trim-margins='true'
    trim-fuzz='-2/-2/2/0'
...

Offline

#331 2020-01-19 17:47:48

jmarc
Member
Registered: 2018-09-13
Posts: 7

Re: Dmenu Hacking Thread

hi all,

i don't know how to parse a m3u file with dmenu !

here is my stations in radios.m3u

radios.m3u

#EXTM3U 
#EXTINF:-1,Funk Radio
http://server3.digital-webstream.de:12160
#EXTINF:-1,Funk Radio 2
http://radiomeuh.ice.infomaniak.ch/radiomeuh-128.mp3
#EXTINF:-1,Radio NOVA
http://broadcast.infomaniak.net/radionova-high.mp3
cat radios.m3u | cut -d ',' -f2 | tail -n +2 | dmenu -l 20 -p "Radio:" | xargs -r mpv &

How to display just the name of the station and play the url ?

Thanks for your help.

Offline

#332 2020-01-19 18:10:52

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

Re: Dmenu Hacking Thread

Works here...

cat radio.m3u | cut -d ',' -f2 | tail -n +2 | dmenu -l 20 -p "Radio:" | xargs -r mocp -p

Are you sure the stations you use work? I tested on some stream I always use.

Offline

#333 2020-01-19 18:29:17

jmarc
Member
Registered: 2018-09-13
Posts: 7

Re: Dmenu Hacking Thread

The display is :

Radio: 
Funk Radio
http://server3.digital-webstream.de:12160
Funk Radio 2
http://radiomeuh.ice.infomaniak.ch/radiomeuh-128.mp3
Radio NOVA
http://broadcast.infomaniak.net/radionova-high.mp3

I don't want to view the url !

Last edited by jmarc (2020-01-19 18:31:38)

Offline

#334 2020-01-19 21:32:25

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 28,494
Website

Re: Dmenu Hacking Thread

Try this:

#!/usr/bin/env bash

file="$HOME/stations.txt"
line=$(awk -F',' '/^#/ {print $2}' "$file")
snip=$(printf '%s\n' "${line[@]}" | dmenu "$@")

if [[ -n $snip ]]; then
    mpv $(awk -v quote="$snip" '$0 ~ (quote"$") {getline; print}' "$file")
fi

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Online

#335 2020-01-19 22:55:23

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

Re: Dmenu Hacking Thread

@JWR, damn I was creating a solution too you were just faster wink

#!/bin/sh
radio=radio.m3u
choice=$(awk -F ','  '{print $2}' "$radio" | awk 'NF{print}' | dmenu -l 20 -p "Radio:")
mpv $(awk "f{print;f=0; exit} /$choice/{f=1}" "$radio")

or replace the choice line both lines with 'sed'

choice=$(sed '/M3U/d;/http/d;s/#EXTINF:-1,//g;/^$/d' "$radio" | dmenu -l 20 -p "Radio:")
mpv $(sed -n "/$choice/{n;p;}" "$radio")

Last edited by qinohe (2020-01-20 01:57:46)

Offline

#336 2020-01-20 20:34:22

jmarc
Member
Registered: 2018-09-13
Posts: 7

Re: Dmenu Hacking Thread

Its just awesome this code work for me :

choice=$(awk -F ','  '{print $2}' "$radio" | awk 'NF{print}' | dmenu -l 20 -p "Radio:")
mpv $(sed -n "/$choice/{n;p;}" "$radio")

I use this for IPTV channels too.

I think it's ugly but i add this

sed 's/.$//'

to remove this charater

^M

at the end of each line of my file !


Great thanks for you all , and long life to dmenu.

Now i really have to learn awk,sed,etc.....;)

Offline

#337 2020-01-20 21:18:59

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

Re: Dmenu Hacking Thread

Nice, great to see it works for you, though, jasonwryan's solution should be working too, he's just looking at it differently and there's a check to see if the file exists, mine hasn't

I have used your M3U file from#331 so I'm not sure why there was an '^M' at the end of each line?, in my try's there wasn't.
Still learning how to make better and cleaner code, I promise you this still open for improvement, have fun with it;)

Offline

#338 2020-01-23 10:28:58

sekret
Member
Registered: 2013-07-22
Posts: 231

Re: Dmenu Hacking Thread

Password manager using lesspass.

#!/bin/sh

# DEFAULTS
counter=1
password_length=35
options="-luds" #lowercase, uppercase, digits, special characters

# dmenu
DMENU_PASS="dmenu -nf red -nb red -sf red -sb black"

config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/dmenu-lesspass"
cache="$config_dir/items"
mkdir -p "$config_dir"
touch "$cache"

# select / add item and login (format: URL LOGIN)
item=$(echo -e "$(cat "$cache")" | dmenu -p "Item: ") || exit

# master password
master=$(echo | $DMENU_PASS -p "Master Password: ") || exit

# move item and login to top / add them
sed  -i "/$item/d" "$cache" #remove it
sed  -i "1i $item" "$cache" #insert it on line 1
# Problem with this approach: If the items file is empty, it doesn't work. So the first account needs to be included manually. I'm no sed-magician, so... ;-)

# exceptions
case "$(echo $item | awk '{print $1}')" in
	paypal.com)
		password_length=20
		;;
esac


# Put it into the world. There are two ways to do so:
############
# Option 1 #
############
# Let xdotool type the password for you.
xdotool type "$(echo $master | lpcli $item $options -n $password_length -c $counter -p | sed '$!d')"

############
# Option 2 #
############
# Put the password into the clipboard. The used lpcli program will remove it after 10 seconds, so don't wait too long
#echo $master | lpcli $item $options -n $password_length -c $counter

Offline

#339 2020-01-26 11:43:55

jmarc
Member
Registered: 2018-09-13
Posts: 7

Re: Dmenu Hacking Thread

With your advice and some code in this topics i came with a youtube viewer but  get videos from invidio.us :

#!/bin/sh 
channelsyt="NBA;https://invidio.us/channel/NBA
BBALLBREAKDOWN;https://www.invidio.us/channel/bballbreakdown
Basketball Box;https://www.invidio.us/channel/UCrAkEqmj8JUNBLNJNUl0fkg
House of Highlights;https://www.invidio.us/channel/UCqQo7ewe87aYAe7ub5UqXMw
MLG Highlights;https://invidio.us/channel/UC-XWpctw55Q6b_AHo8rkJgw
ESPN;https://invidio.us/channel/ESPN
NBA On ESPN;https://invidio.us/channel/UCVSSpcmZD2PwPBqb8yKQKBA
"

url_vid="https://invidio.us"
channelname=$(echo "$channelsyt" | sed 's/.*    \+//' | dmenu -l 20)
urlchannel=$(echo "$channelname" | cut -d';' -f2-)

output="$(wget -qO- "$urlchannel")"

select="$(echo "$output"| grep '<p><a href="/watch?v=' | cut -s -d\" -f3 | cut -s -f1 -d'/' | cut -s -f1 -d'<' | cut -c 2-200 | dmenu -l 20)"

url="${url_vid}$(echo "$output"|grep "$select"|head -n1|cut -d\" -f2)"
url_param="$(echo "$output"|grep "$select"|head -n1|cut -d\" -f2)"
url="${url_vid}${url_param}"

mpv ${url}

Last edited by jmarc (2020-01-26 17:06:23)

Offline

#340 2020-01-26 14:47:48

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

Re: Dmenu Hacking Thread

Nice, though I haven't looked any further than this line but that's choir of pipes;)
Try this line:

select="$(sed 's/<[^<>]*>//g' "$output" | dmenu -l 20)"

Offline

#341 2020-01-28 14:50:30

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

Re: Dmenu Hacking Thread

Disregard the above completely, I have used the wrong file and realized that later - sorry about that.

The first 'url=' variable can be removed it does nothing
There were still some changes I could make to shorten the script and remove some pipes.

The lines that matter

channelname=$(echo "$channelsyt" | dmenu -l 20)
select="$(echo "$output" | grep '<a href="/watch' | sed 's/<[^<>]*>//g' | awk '{$1=$1};1' | dmenu -l 20)"
or
select="$(echo "$output" | grep '<a href="/watch' | sed 's/<[^<>]*>//g;s/^[ \t]*//g'  | dmenu -l 20)"

There may be more you can do but hey have fun;)

edit:added a second 'select' all with sed;)
one more thing, 'head -n1' in 'url_param' does nothing, remove it.

Last edited by qinohe (2020-01-28 17:13:18)

Offline

#342 2020-01-28 22:07:12

jmarc
Member
Registered: 2018-09-13
Posts: 7

Re: Dmenu Hacking Thread

Great thanks qinohe for your code , indeed it is more efficient.

How just display the name of the channel and  then select the url with dmenu ?

Offline

#343 2020-01-29 15:15:33

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

Re: Dmenu Hacking Thread

Thanks for your help 'T' wink

@jmarc, Well seems your wish was heard, I had help for that.
I'm still busy understanding it myself completely but it works like a charm.
Be grateful to the creator...

#!/bin/sh

url_vid="https://invidio.us"

channels="NBA;https://invidio.us/channel/NBA
BBALLBREAKDOWN;https://www.invidio.us/channel/bballbreakdown
Basketball Box;https://www.invidio.us/channel/UCrAkEqmj8JUNBLNJNUl0fkg
House of Highlights;https://www.invidio.us/channel/UCqQo7ewe87aYAe7ub5UqXMw
MLG Highlights;https://invidio.us/channel/UC-XWpctw55Q6b_AHo8rkJgw
ESPN;https://invidio.us/channel/ESPN
NBA On ESPN;https://invidio.us/channel/UCVSSpcmZD2PwPBqb8yKQKBA\
"
  
sel=$(echo "$channels" | cut -d';' -f1 | dmenu -l 20)
list=$(curl -s $(echo "$channels" | sed -n "s/^$sel;//p") | sed -n 's/.*<p><a href="\(\/watch?v=[^"]*\)">\([^<]*\).*/  \1;\2/p')
sel2=$(echo "$list" | cut -d';' -f2 | dmenu -l 20)
mpv ${url_vid}$(echo "$list" | sed -n "s/;$sel2$//p")

Offline

#344 2020-01-29 19:15:20

jmarc
Member
Registered: 2018-09-13
Posts: 7

Re: Dmenu Hacking Thread

Damn man , so thankfull for your solution.

I must have to delete this space !

([^<]*\).*/  \1;\2/p')
([^<]*\).*/\1;\2/p')

Encore merci , thank you.

Offline

#345 2020-01-29 20:14:43

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

Re: Dmenu Hacking Thread

jmarc wrote:

I must have to delete this space !

([^<]*\).*/  \1;\2/p')
([^<]*\).*/\1;\2/p')

Copy paste fault..

Have been crafting a little further...
I have removed ';' between the name and the site, better readable script..
Mind '\' at the end of the channels var.

If you want to stay 'sed' all the way, here's an example;)

url_vid="https://invidio.us"
  
channels="NBA https://invidio.us/channel/NBA
ESPN https://invidio.us/channel/ESPN\
"

sel=$(echo "$channels" | sed 's/\ [^ ]*$//' | dmenu -l 20)
list=$(curl -s $(echo "$channels" | sed -n "s/^$sel//p") | sed -n 's/.*<p><a href="\(\/watch?v=[^"]*\)">\([^<]*\).*/\1;\2/p')
sel2=$(echo "$list" | sed 's/.*;//'| dmenu -l 20)
mpv ${url_vid}$(echo "$list" | sed -n "s/;$sel2$//p")

Offline

#346 2020-01-29 21:21:29

jmarc
Member
Registered: 2018-09-13
Posts: 7

Re: Dmenu Hacking Thread

Thanks , it work also like a charm.

Offline

#347 2020-01-29 22:56:51

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 28,494
Website

Re: Dmenu Hacking Thread

Less pipes:

#!/usr/bin/env bash

options=(
'NBA;https://invidio.us/channel/NBA'
'BBALLBREAKDOWN;https://www.invidio.us/channel/bballbreakdown'
'Basketball Box;https://www.invidio.us/channel/UCrAkEqmj8JUNBLNJNUl0fkg'
'House of Highlights;https://www.invidio.us/channel/UCqQo7ewe87aYAe7ub5UqXMw'
'MLG Highlights;https://invidio.us/channel/UC-XWpctw55Q6b_AHo8rkJgw'
'ESPN;https://invidio.us/channel/ESPN'
'NBA On ESPN;https://invidio.us/channel/UCVSSpcmZD2PwPBqb8yKQKBA'
)

chans=$(printf "%s\n" "${options[@]%%;*}" | dmenu -i)

if [[ -n $chans ]]; then
    mpv $(awk -F';' -v sel="$chans" 'index($1, sel) {print $2}'\
        <(printf "%s\n" "${options[@]}"))
fi

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Online

#348 2020-01-30 00:11:44

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

Re: Dmenu Hacking Thread

Cool;) Using an array have not even thought about that.
The downside of it is (in my opinion) all is on one line, where would I put the newline '\n' or 'RS'?

Offline

#349 2020-01-30 02:01:01

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 28,494
Website

Re: Dmenu Hacking Thread

The array is split on to separate lines before being passed to dmenu. Is that what you mean?


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Online

#350 2020-01-30 02:16:04

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

Re: Dmenu Hacking Thread

chans=$(printf "%s\n" "${options[@]%%;*}" | dmenu -i)

Yes, but the line in the dmenu menu is one line.
I have tried a lot to get them under each other, no luck.

Something like this;

echo "mocp\njukebox\npavucontrol" | dmenu -l 3

Offline

Board footer

Powered by FluxBB