summaryrefslogtreecommitdiff
path: root/bm
blob: 9b303551deb5d8a5baa61ae486a9e66fe0539c64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/sh

#{{{ printhelp
printhelp () {
  printf "bm: browser menu script; interface for browsers.\n"
  printf "  -b) basilisk interface\n"
  printf "  -d) firedragon interface\n"
  printf "  -f) firefox interface\n"
  printf "  -l) librewolf interface\n"
  printf "  -q) qutebrowser interface\n"
  printf "  -x) use X11 menu (dmenu)\n"
  printf "  -w) use Wayland menu (tofi)\n"
  printf "  -d ARG) provide menu args\n"
  printf "  -u ARG) provide URL to open\n"
  exit
}
#}}}

#{{{ BROWSERSBYCMD
# Native binaries
type basilisk >/dev/null 2>&1 && CANBSC="bsc"
type firedragon >/dev/null 2>&1 && CANFDC="fdc"
type firefox >/dev/null 2>&1 && CANFFC="ffc"
type librewolf >/dev/null 2>&1 && CANLWC="lwc"
type qutebrowser >/dev/null 2>&1 && CANQBC="qbc"
type brave >/dev/null 2>&1 && CANBRA="brave"
[ -n "$WAYLAND_DISPLAY" ] && \
  type wl-copy >/dev/null 2>&1 && CANCLIPB="clip"
[ -z "$WAYLAND_DISPLAY" -a -n "$DISPLAY" ] && \
  type xclip >/dev/null 2>&1 && CANCLIPB="clip"
# Flatpaks
LIST="$(flatpak list)"
echo "$LIST" | grep -q "org.mozilla.firefox" && CANFFC="ffc"
echo "$LIST" | grep -q "com.brave.Browser" && CANBRA="brave"
echo "$LIST" | grep -q "io.gitlab.librewolf-community" && \
  CANLWC="lwc"
echo "$LIST" | grep -q "org.qutebrowser.qutebrowser" && \
  CANQBC="qbc"
#}}}

#{{{ getopts
unset ARG
while getopts "hbBcrflqxwd:u:" o; do case "${o}" in
  b) BSC="y" ;;
  B) BRA="y" ;;
  r) FDC="y" ;;
  f) FFC="y" ;;
  l) LWC="y" ;;
  q) QBC="y" ;;
  c) CLIPB="y" ;;
  x) ARG="$ARG-x " ;;
  w) ARG="$ARG-w " ;;
  d) ARG="$ARG-d $OPTARG ";;
  u) URL="$OPTARG " ;;
  *) printhelp ;;
esac done
#}}}

#{{{ clip
clip () {
  ARG="$@"
  if [ -n "$WAYLAND_DISPLAY" -a -n "$CANCLIPB" ]; then
    echo -n $URL | wl-copy ${ARG%u}
  elif [ -n "$DISPLAY" -a -n "$CANCLIPB" ]; then
    echo -n $URL | xclip -selection clipboard ${ARG%-u}
  fi
}
#}}}

#{{{ bra
bra () {
  if [ -n "$CANBRA" ]; then
    if type brave >/dev/null 2>&2; then
      brave $@
    else
      flatpak run com.brave.Browser $@
    fi
  fi
}
#}}}

#{{{ Check display server if -w or -x not provided
if [ -z "$DMENU" ]; then
  if [ -n "$WAYLAND_DISPLAY" ]; then
    DMENU="bemenu"
  elif [ -n "$DISPLAY" ]; then
    DMENU="dmenu"
  else
    print "Can't tell if Wayland or X; what gives?\n"
    exit 1
  fi
fi
#}}}

#{{{ main
if [ -n "$BSC$FDC$FFC$LWC$QBC" ]; then
  ARG="$ARG -u $URL"
fi
echo $ARG
[ -n "$BSC" ] && exec bsc $ARG
[ -n "$FDC" ] && exec fdc $ARG
[ -n "$FFC" ] && exec ffc $ARG
[ -n "$LWC" ] && exec lwc $ARG
[ -n "$QBC" ] && exec qbc $ARG
[ -n "$BRA" ] && exec bra $ARG
[ -n "$CLIPB" ] && exec clip $ARG
if [ -z "$CLIPB$BSC$FDC$FFC$LWC$QBC" ]; then
  BROWSERSINSTALLED=""
  for i in CLIPB BSC FDC FFC LWC QBC BRA; do
    if [ -n "$(eval "echo \$CAN${i}")" ]; then
      [ -n "$BROWSERSINSTALLED" ] && STARTLINE="\n" || STARTLINE="" ;
      [ -z "$URL" -a "$i" = "CLIPB" ] && continue
      BROWSERSINSTALLED="${BROWSERSINSTALLED}${STARTLINE}$(eval "echo \$CAN${i}")"
    fi
  done
  echo "$BROWSERSINSTALLED"
  [ -z "$BROWSERSINSTALLED" ] && \
    printf "No suitable browsers!\n" && exit 1
  BROWSERSEL="$(echo "$BROWSERSINSTALLED" | $DMENU -p "Browsers" -l 10 $ARG)"
  [ -z "$BROWSERSEL" ] && \
    printf "No browser selected!\n" && exit 1
  case "$BROWSERSEL" in
    clip)
      $BROWSERSEL $ARG
      ;;
    brave)
      bra $URL
      ;;
    *)
      $BROWSERSEL $ARG -u $URL
      ;;
  esac
fi
#}}}