You are not logged in.

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

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 884

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

Board footer

Powered by FluxBB