Skip to content

Commit 9682eeb

Browse files
♻️ refactor(dmenu_center): Rely on rofi instead of dmenu2.
1 parent 2c6ded1 commit 9682eeb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+315
-175
lines changed

.bin/audio.move.sink.all

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env sh
22

33
sink="$(audio.sinks | \
4-
dmenu_center --rest -i -t -p 'sink' -sb '#F24130' -l 10 | \
4+
dmenu_center -i -t -p 'sink' -sb '#F24130' -l 10 | \
55
cut -d' ' -f1)"
66

77
if [ "$sink" = '' ]; then

.bin/audio.move.sink.input

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/usr/bin/env sh
22

33
input="$(audio.inputs | \
4-
dmenu_center --rest -i -t -p 'input' -sb '#F24130' -l 10 | \
4+
dmenu_center -i -t -p 'input' -sb '#F24130' -l 10 | \
55
cut -d' ' -f1)"
66

77
if [ "$input" = '' ]; then
88
exit 1
99
fi
1010

1111
sink="$(audio.sinks | \
12-
dmenu_center --rest -i -t -p 'sink' -sb '#F24130' -l 10 | \
12+
dmenu_center -i -t -p 'sink' -sb '#F24130' -l 10 | \
1313
cut -d' ' -f1)"
1414

1515
if [ "$sink" = '' ]; then

.bin/audio.move.source.all

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env sh
22

33
source="$(audio.sources | \
4-
dmenu_center --rest -i -t -p 'source' -sb '#F24130' -l 10 | \
4+
dmenu_center -i -t -p 'source' -sb '#F24130' -l 10 | \
55
cut -d' ' -f1)"
66

77
if [ "$source" = '' ]; then

.bin/audio.move.source.output

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/usr/bin/env sh
22

33
output="$(audio.outputs | \
4-
dmenu_center --rest -i -t -p 'output' -sb '#F24130' -l 10 | \
4+
dmenu_center -i -t -p 'output' -sb '#F24130' -l 10 | \
55
cut -d' ' -f1)"
66

77
if [ "$output" = '' ]; then
88
exit 1
99
fi
1010

1111
source="$(audio.sources | \
12-
dmenu_center --rest -i -t -p 'source' -sb '#F24130' -l 10 | \
12+
dmenu_center -i -t -p 'source' -sb '#F24130' -l 10 | \
1313
cut -d' ' -f1)"
1414

1515
if [ "$source" = '' ]; then

.bin/audio.set.default.sink

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env sh
22

33
sink="$(audio.sinks | \
4-
dmenu_center --rest -i -t -p 'default sink' -sb '#F24130' -l 10 | \
4+
dmenu_center -i -t -p 'default sink' -sb '#F24130' -l 10 | \
55
cut -d' ' -f1)"
66

77
if [ "$sink" = '' ]; then

.bin/audio.set.default.source

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env sh
22

33
source="$(audio.sources | \
4-
dmenu_center --rest -i -t -p 'default source' -sb '#F24130' -l 10 | \
4+
dmenu_center -i -t -p 'default source' -sb '#F24130' -l 10 | \
55
cut -d' ' -f1)"
66

77
if [ "$source" = '' ]; then

.bin/bib.selection.local

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#!/usr/bin/env python3
22
# vi:syntax=python
33

4-
import arrow
54
from collections import defaultdict
65

7-
# prompt = ['dmenu_center', '--rest', '-i', '-t', '-p', 'bib', '-sb', '#8080ff', '-l', '10']
6+
# prompt = ['dmenu_center', ''-i', '-t', '-p', 'bib', '-sb', '#8080ff', '-l', '10']
87
prompt = ['fzf-menu', '--lines', '10', '-m', '--prompt', 'bib > ']
98
querystring = 'ext:bib'
109
sortby = 'mtime'

.bin/clipboard.menu

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ done
4242
# argument to override an earlier one. That is, if the user passes in `-l`, our
4343
# one will be ignored.
4444
chosen_line=$(printf '%s\n' "${ordered_selections[@]}" | \
45-
dmenu_center --rest -l 10 -sb '#488281' -p '' -i -t "$@")
45+
dmenu_center -l 10 -sb '#488281' -p '' -i -t "$@")
4646

4747
[[ $chosen_line ]] || exit 1
4848

.bin/courriel.abook

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env sh
22

3-
address=$(notmuch-abook export -f email -s name | uniq | dmenu_center --rest -p '' -t -i -sb '#994115' -l 10)
3+
address=$(notmuch-abook export -f email -s name | uniq | dmenu_center -p '' -z -i -sb '#994115' -l 10)
44

55
if [ -z "$address" ] ; then
66
exit 1

.bin/dmenu_center

+54-127
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,60 @@
11
#!/usr/bin/env python3
22

3-
# Copyright 2013 Ryan McGowan
4-
#
5-
# Licensed under the Apache License, Version 2.0 (the "License");
6-
# you may not use this file except in compliance with the License.
7-
# You may obtain a copy of the License at
8-
#
9-
# http://www.apache.org/licenses/LICENSE-2.0
10-
#
11-
# Unless required by applicable law or agreed to in writing, software
12-
# distributed under the License is distributed on an "AS IS" BASIS,
13-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
# See the License for the specific language governing permissions and
15-
# limitations under the License.
3+
from argparse import ArgumentParser, BooleanOptionalAction
4+
from subprocess import call
5+
from sys import argv
166

17-
import sys
18-
import os
19-
import json
20-
from subprocess import call, check_output
217

22-
# to redirect xlib output
23-
# note that there is a dedicated method to do this in python >3.4
24-
# called contextlib.redirect_stdout
25-
from contextlib import contextmanager
26-
27-
28-
def fileno(file_or_fd):
29-
fd = getattr(file_or_fd, 'fileno', lambda: file_or_fd)()
30-
if not isinstance(fd, int):
31-
raise ValueError("Expected a file (`.fileno()`) or a file descriptor")
32-
return fd
33-
34-
35-
@contextmanager
36-
def redirect(fro, to):
37-
38-
stdout_fd = fileno(fro)
39-
# copy stdout_fd before it is overwritten
40-
# NOTE: `copied` is inheritable on Windows when duplicating a standard
41-
# stream
42-
with os.fdopen(os.dup(stdout_fd), 'wb') as copied:
43-
fro.flush() # flush library buffers that dup2 knows nothing about
44-
try:
45-
os.dup2(fileno(to), stdout_fd) # $ exec >&to
46-
except ValueError: # filename
47-
with open(to, 'wb') as to_file:
48-
os.dup2(to_file.fileno(), stdout_fd) # $ exec > to
49-
try:
50-
yield fro # allow code to be run with the redirected fro
51-
finally:
52-
# restore fro to its previous value
53-
# NOTE: dup2 makes stdout_fd inheritable unconditionally
54-
fro.flush()
55-
os.dup2(copied.fileno(), stdout_fd) # $ exec >&copied
56-
57-
58-
def parse_dmenu_args(args):
59-
60-
split = args.index('--rest') if '--rest' in args else len(args)
61-
62-
args, rest = args[:split], args[split + 1:]
63-
64-
argc = len(args)
65-
# Get arguments from the command line.
66-
67-
runnable = args[0] if argc > 0 else 'dmenu'
68-
69-
# 20% padding means only 80% of the screen is used by dmenu with 10%
70-
# padding on each side.
71-
padding = float(args[1]) if argc > 1 else .24
72-
73-
# Font size and lineheight are in points
74-
line_height = int(args[2]) if argc > 2 else 24
75-
76-
font = args[3] if argc > 3 else 'LiterationMono Nerd Font Mono:12'
77-
78-
with redirect(sys.stdout, sys.stderr):
79-
from Xlib import display
80-
current_display = display.Display()
81-
current_screen = current_display.screen()
82-
83-
workspaces = json.loads(check_output(['i3-msg', '-t', 'get_workspaces']))
84-
workspace = next(workspace for workspace in workspaces if workspace['focused'])
85-
86-
screenwidth = workspace['rect']['width'] * 1.0
87-
xoffset = int(round(padding / 2 * screenwidth))
88-
width = screenwidth - 2 * xoffset
89-
90-
wholeheight = current_screen['height_in_pixels'] * 1.0
91-
screenheight = workspace['rect']['height'] * 1.0
92-
s = screenheight / wholeheight
93-
height_in_mms = current_screen['height_in_mms'] * 1.0
94-
height_in_inches = height_in_mms / 25.4 * s
95-
height_in_points = height_in_inches * 72
96-
pixels_per_point = screenheight / height_in_points
97-
98-
height = int(round(line_height * pixels_per_point, 0))
99-
yoffset = int(round((screenheight - height) / 2))
100-
height = screenheight - 2 * yoffset
101-
102-
# Set some default values for dmenu args
103-
dmenu_run_args = {
104-
'runnable': runnable,
105-
'xoffset': str(xoffset),
106-
'yoffset': str(yoffset),
107-
'width': str(width),
108-
'height': str(height),
109-
'font': font,
110-
'nb': '#1a1a1a'
111-
}
112-
113-
return dmenu_run_args, rest
114-
115-
116-
def main(args):
117-
dmenu_run_args, rest = parse_dmenu_args(args)
118-
cmd = [dmenu_run_args['runnable']]
119-
cmd += ['-fn', dmenu_run_args['font']]
120-
cmd += ['-x', dmenu_run_args['xoffset']]
121-
cmd += ['-y', dmenu_run_args['yoffset']]
122-
cmd += ['-h', dmenu_run_args['height']]
123-
cmd += ['-w', dmenu_run_args['width']]
124-
cmd += ['-nb', dmenu_run_args['nb']]
125-
cmd.extend(rest)
126-
return call(cmd)
127-
128-
129-
def console_main():
130-
sys.exit(main(sys.argv[1:]))
8+
css_variables = {
9+
'fn': 'font',
10+
'nb': 'normal background',
11+
'nf': 'normal foreground',
12+
'sb': 'selected background',
13+
'sf': 'selected foreground',
14+
'l': 'number of lines'
15+
}
13116

13217
if __name__ == '__main__':
133-
console_main()
18+
19+
parser = ArgumentParser()
20+
for key, value in css_variables.items():
21+
parser.add_argument('-{}'.format(key), help=value)
22+
23+
parser.add_argument('-t', help='match filter words separately', dest="tokenize", action="store_true")
24+
parser.add_argument('-tokenize', help='match filter words separately', action="store_true")
25+
parser.add_argument('-z', help='fuzzy matching', dest="matching", action="store_const", const="fuzzy")
26+
parser.add_argument('-matching', help='rofi\'s -matching option', default="normal")
27+
28+
parser.add_argument('-sort', help='rofi\'s -sort/-no-sort option', action=BooleanOptionalAction, default=True)
29+
parser.add_argument('-sorting-method', help='rofi\'s -sorting-method option', default="fzf")
30+
31+
parser.add_argument('-fullscreen', help='display menu in fullscreen', action="store_true")
32+
parser.add_argument('-mask', help='mask input', action="store_true")
33+
34+
args, rest = parser.parse_known_intermixed_args(argv[1:])
35+
kw = vars(args)
36+
37+
css_variable_defs = ''.join([
38+
'{}: {};'.format(key, value) for key in css_variables if (value := kw.get(key)) is not None
39+
])
40+
41+
theme_str = '* {{{}}}'.format(css_variable_defs)
42+
location = '2' if args.fullscreen else None
43+
44+
cmd: tuple[str] = (
45+
'rofi',
46+
'-dmenu',
47+
'-theme',
48+
'fullscreen' if args.fullscreen else 'oneline' if args.l is None else 'multiline',
49+
*(('-location', location) if location is not None else ()),
50+
'-theme-str',
51+
theme_str,
52+
*(('-matching', args.matching) if args.matching is not None else ()),
53+
*(('-t',) if args.tokenize else ()),
54+
*(('-password',) if args.mask else ()),
55+
'-sort' if args.sort else '-no-sort',
56+
'-sorting-method', args.sorting_method,
57+
*rest
58+
)
59+
60+
call(cmd)

.bin/dmenu_center_run

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
#!/usr/bin/env sh
1+
#!/usr/bin/env dash
22

3-
dmenu_center 'dmenu_run' --rest -p '' -sb '#789048'
3+
dmenu_path | \
4+
dmenu_center -p '' -sb '#789048' "$@" | \
5+
"${SHELL:-"/usr/bin/env dash"}" &

.bin/firefox.history

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ cp "$src" "$db"
1111
url=$(sqlite3 -separator ' -- ' "$db" 'SELECT title, url FROM (SELECT visit_date, visit_type,'\
1212
'url, title FROM moz_historyvisits JOIN moz_places ON moz_historyvisits.place_id ='\
1313
'moz_places.id AND visit_type = 1 GROUP BY url ORDER BY visit_date DESC);' |\
14-
dmenu_center --rest -p '' -sb '#54A8A8' -i -t -l 10)
14+
dmenu_center -p '' -sb '#54A8A8' -i -z -fullscreen)
1515

1616
rm "$db"
1717

.bin/geolocation.manual.menu

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mkdir -p "$(dirname "$MANUAL")"
77

88
tag=$(jq < "$CONFIG" '.tag | keys[]' -r |\
99
sort |\
10-
dmenu_center --rest -p '' -sb '#338844' -i -t -l 10)
10+
dmenu_center -p '' -sb '#338844' -i -t -l 10)
1111

1212
if [ "$tag" = '' ] ; then
1313
exit 1

.bin/google.withlr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
LANG="${HOME}/.config/google/search/lr"
44

5-
lr="$(< "$LANG" dmenu_center --rest -p ' lr' -sb '#1851CE')"
5+
lr="$(< "$LANG" dmenu_center -p ' lr' -sb '#1851CE')"
66

77
if [ -z "$lr" ] ; then
88
exit 1

.bin/menu.edit

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env sh
22

3-
file=$(fasd -fRl | dmenu_center --rest -i -t -p '' -sb '#24A946' -l 10)
3+
file=$(fasd -fRl | dmenu_center -i -z -p '' -sb '#24A946' -l 10)
44

55
if [ "$file" != '' ]; then
66
terminator -b -x vim "$file"

.bin/menu.gpu

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env sh
22
mode="$(printf '%s\n' battery balanced performance |
3-
dmenu_center --rest -sb '#D24858' -p 'mode')"
3+
dmenu_center -sb '#D24858' -p 'mode')"
44

55
if [ -z "$mode" ]; then
66
exit 101

.bin/menu.journalctl.follow

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env sh
22
manager="$(printf '%s\n' --system --user |
3-
dmenu_center --rest -sb '#D24858' -p 'manager')"
3+
dmenu_center -sb '#D24858' -p 'manager')"
44

55
if [ -z "$manager" ]; then
66
exit 101

.bin/menu.keyboard

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env sh
22

3-
kb=$(localectl list-x11-keymap-layouts | dmenu_center \
4-
--rest -i -t -p '' -sb '#3A352F')
3+
kb="$(
4+
localectl list-x11-keymap-layouts | \
5+
dmenu_center -i -t -p '' -sb '#3A352F'
6+
)"
57

68
if [ "$kb" != '' ]; then
79
setxkbmap "$kb"

.bin/menu.library

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env sh
22

33
OPENER="${1:-xdg-open}"
4-
file=$(find "$HOME/library" -type f | dmenu_center --rest -i -t -p '' -sb '#285577' -l 10)
4+
file=$(find "$HOME/library" -type f | dmenu_center -i -z -p '' -sb '#285577' -l 10)
55

66
if [ "$file" != '' ]; then
77
"$OPENER" "$file"

.bin/menu.man

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env sh
22

3-
name="$(< "$HOME/.cache/dmenu_run" dmenu_center --rest -i -p '' -sb '#24A946')"
3+
name="$(< "$HOME/.cache/dmenu_run" dmenu_center -i -p '' -sb '#24A946')"
44

55
if [ -n "$name" ]; then
66
if man "$name" > /dev/null 2>&1; then

.bin/menu.nav

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env sh
22

3-
wd=$(fasd -dRl | dmenu_center --rest -i -t -p '' -sb '#E15E32' -l 10)
3+
wd=$(fasd -dRl | dmenu_center -i -z -p '' -sb '#E15E32' -l 10)
44

55
if [ "$wd" != '' ]; then
66
terminator -b --working-directory "$wd" -x ranger

.bin/menu.netctl.status

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#!/usr/bin/env sh
22

33
profiles=$(wifi.profiles | sort -k1,1 | cut -b3-)
4-
profile=$(echo "$profiles" | dmenu_center --rest -i -t -sb '#D24858' -p "profile to watch" -l 10)
4+
profile="$(
5+
echo "$profiles" | \
6+
dmenu_center -i -z -sb '#D24858' -p "profile to watch" -l 10
7+
)"
58

69
if [ "$profile" = '' ]; then
710
exit 102

0 commit comments

Comments
 (0)