literate-dotfiles/dotfiles/polybar.org
2020-04-06 00:11:57 +02:00

21 KiB

Polybar Configuration

Colors

  [colors]
  bg = ${xrdb:background}
  red = ${xrdb:color1}
  green = ${xrdb:color2}
  yellow = ${xrdb:color3}
  blue = ${xrdb:color4}
  purple = ${xrdb:color5}
  aqua = ${xrdb:color6}
  gray = ${colors.color7}
  darkgray = ${colors.color8}
  darkred = ${xrdb:color9}
  darkgreen = ${xrdb:color10}
  darkyellow = ${xrdb:color11}
  darkblue = ${xrdb:color12}
  darkpurple = ${xrdb:color13}
  darkaqua = ${xrdb:color14}
  fg = ${xrdb:foreground}

VM

  [global/wm]
  margin-top = 0
  margin-bottom = 0

Top Bar

  [bar/top]
  enable-ipc = true
  override-redirect = false
  bottom = false
  fixed-center = false

  width = 100%
  height = 27
  radius = 0.0

  background = ${colors.bg}
  foreground = ${colors.fg}

  line-size = 2

  border-size = 0

  padding-left = 2
  padding-right = 12

  module-margin-left = 1
  module-margin-right = 1

  font-0 = Hack Nerd Font:style=Medium,Regular:pixelsize=10;1
  font-1 = unifont:fontformat=truetype:size=8:antialias=false;0
  font-2 = Weather Icons:size=10;1

  modules-left = i3 bspwm xwindow
  modules-center =
  modules-right = pulseaudio xbacklight redshift wireguard nordvpn lockscreen dunst packages unread_news unread_mail temperature battery date

  wm-restack = i3

  tray-position = right
  tray-detached = true
  tray-padding = 0
  tray-background = ${colors.bg}

Show Windows Title

  [module/xwindow]
  type = internal/xwindow
  label = %title:0:30:...%

I3 - Information about workspaces and active one

  [module/i3]
  type = internal/i3

  enable-scroll = false
  wrapping-scroll = false
  format = <label-state> <label-mode>
  index-sort = true
  label-mode-foreground = ${colors.bg}
  label-mode-background = ${colors.fg}
  label-mode-padding = 1

  # focused = Active workspace on focused monitor
  label-focused = %icon% %index%
  label-focused-foreground = ${colors.fg}
  label-focused-background = ${colors.bg}
  label-focused-underline = ${colors.aqua}
  label-focused-padding = 1

  # unfocused = Inactive workspace on any monitor
  label-unfocused = %icon% %index%
  label-unfocused-foreground = ${colors.fg}
  label-unfocused-background = ${colors.bg}
  label-unfocused-underline = ${colors.bg}
  label-unfocused-padding = 1

  # visible = Active workspace on unfocused monitor
  label-visible = %icon% %index%
  label-visible-background = ${colors.bg}
  label-visible-underline = ${colors.aqua}
  label-visible-padding = 1

  # urgent = Workspace with urgency hint set
  label-urgent = %icon% %index%
  label-urgent-background = ${colors.red}
  label-urgent-padding = 1

BSPWM

  [module/bspwm]
  type = internal/bspwm

  pin-workspaces = true
  enable-click = true
  enable-scroll = false

  ; Use fuzzy (partial) matching on labels when assigning
  ; icons to workspaces
  ; Example: code;♚ will apply the icon to all workspaces
  ; containing 'code' in the label
  fuzzy-match = false


  ws-icon-0 = code;
  ws-icon-1 = web;
  ws-icon-2 = mail;
  ws-icon-3 = misc;
  ws-icon-4 = figs;ﬧ
  ws-icon-default =

  label-focused = %icon%
  label-focused-foreground = ${colors.bg}
  label-focused-background = ${colors.aqua}
  label-focused-underline= ${colors.aqua}
  label-focused-padding = 1

  label-occupied = %icon%
  label-occupied-underline = ${colors.fg}
  label-occupied-padding = 1

  label-urgent = %icon%
  label-urgent-background = ${colors.red}
  label-urgent-padding = 1

  label-empty = %icon%
  label-empty-foreground = ${colors.fg}
  label-empty-padding = 1

Music using MPD

  [module/mpd]
  type = internal/mpd
  format-online = <icon-prev> <toggle> <icon-next> %{A1:$TERMINAL -e "ncmpcpp" &:}<label-song>%{A}

  format-online-spacing = 0
  format-online-padding = 0

  label-offline = mpd is offline
  format-offline = <label-offline>
  format-offline-foreground = #777

  label-song = %artist% - %title%
  label-song-maxlen = 30
  label-song-ellipsis = true

  icon-prev = 寧
  icon-stop = 栗
  icon-play = 契
  icon-pause = 
  icon-next = 嶺
  icon-random = 咽
  icon-repeat = 凌

  # Used to display the state of random/repeat/repeatone/single
  # Only applies if <icon-[random|repeat|repeatone|single]> is used
  toggle-on-foreground = ${colors.fg}
  toggle-off-foreground = ${colors.gray}

  bar-progress-width = 15
  bar-progress-indicator = |
  bar-progress-fill = ─
  bar-progress-empty = ─

Backlight level

  [module/xbacklight]
  type = internal/xbacklight

  format = <ramp>
  ramp-0 = 
  ramp-1 = 
  ramp-2 = 
  ramp-3 = 
  ramp-4 = 
  ramp-5 = 
  ramp-6 = 
  ramp-7 = 
  ramp-8 = 
  ramp-9 = 
  ramp-10 = 
  ramp-11 = 

CPU Usage

  [module/cpu]
  type = internal/cpu
  interval = 2

  format-prefix-foreground = ${colors.fg}
  format-underline = ${colors.bg}

  label = %{A1:$TERMINAL -e "htop" &:}﬙ %percentage:2%%%{A}

Date

  [module/date]
  type = internal/date
  interval = 1

  # Open google calendar on left right
  date = %{A1:$TERMINAL -e "ikhal" &:} %d-%m%{A}
  date-alt = "%{A1:$TERMINAL -e \"ikhal\" &:} %d-%m-%Y%{A}"

  time =  %H:%M
  time-alt =  %H:%M:%S

  label = %date% %time%

Sound Volume

  [module/pulseaudio]
  type = internal/pulseaudio

  sink = alsa_output.pci-0000_00_1f.3.analog-stereo

  # Open pavucontrol on left right
  format-volume =%{A1:pavucontrol &:}<label-volume> <bar-volume>%{A}

  label-volume = %percentage%%
  label-volume-foreground = ${root.foreground}

  label-muted = ﱝ muted

  bar-volume-width = 10
  bar-volume-foreground-0 = ${colors.green}
  bar-volume-foreground-1 = ${colors.green}
  bar-volume-foreground-2 = ${colors.blue}
  bar-volume-foreground-3 = ${colors.blue}
  bar-volume-foreground-4 = ${colors.red}
  bar-volume-foreground-5 = ${colors.red}
  bar-volume-foreground-6 = ${colors.red}
  bar-volume-gradient = false
  bar-volume-indicator = |
  bar-volume-indicator-font = 2
  bar-volume-fill = ─
  bar-volume-fill-font = 2
  bar-volume-empty = ─
  bar-volume-empty-font = 2
  bar-volume-empty-foreground = ${colors.fg}

Battery

  [module/battery]
  type = internal/battery

  battery = BAT0
  adapter = AC
  full-at = 95

  format-charging = %{A1:xfce4-power-manager-settings &:}<ramp-capacity>  <label-charging>%{A}
  format-charging-underline = ${colors.blue}

  format-discharging = %{A1:xfce4-power-manager-settings &:}<ramp-capacity>  <label-discharging>%{A}
  format-discharging-underline = ${colors.red}

  label-full = %{A1:xfce4-power-manager-settings &:} %{A}
  format-full-underline = ${colors.bg}

  ramp-capacity-0 = 
  ramp-capacity-1 = 
  ramp-capacity-2 = 
  ramp-capacity-3 = 
  ramp-capacity-4 = 

Temperature

  [module/temperature]
  type = internal/temperature
  interval = 2
  thermal-zone = 0
  warn-temperature = 70
  hwmon-path = /sys/devices/platform/coretemp.0/hwmon/hwmon2/temp1_input
  units = false
  format = %{A1:$TERMINAL --class="Floating" -e "watch sensors" &:}<ramp> <label>%{A}
  format-warn = <ramp> <label-warn>
  label = %temperature-c%°C
  label-warn = %temperature-c%°C
  label-warn-foreground = ${colors.fg}
  format-warn-underline = ${colors.red}
  ramp-0 = 
  ramp-1 = 
  ramp-2 = 
  ramp-3 = 
  ramp-4 = 
  ramp-foreground = ${colors.fg}

Unread Mails

  [module/unread_mail]
  type = custom/script

  format-underline = ${colors.bg}
  click-left = ~/.config/polybar/scripts/mail-open.sh
  click-right = ~/.config/polybar/scripts/mail-refresh.sh
  format = <label>
  exec = ~/.config/polybar/scripts/mail-status.sh
  interval = 2

Unread Mail Scripts

  mail_nb=`du -a ~/.mail/*/Inbox/new/* 2>/dev/null | wc -l`

  if [ "$mail_nb" -eq "0" ]; then
    echo "";
  else
    echo "%{F#859900} $mail_nb%{F-}";
  fi

Refresh Mail Scripts

  dunstify --replace=98465 "Mails " "Syncing...";
  ~/scripts/checkmail.sh -q && \
      mail_nb=`du -a ~/.mail/*/Inbox/new/* 2>/dev/null | wc -l` && \
      if [ "$mail_nb" -eq "0" ]; then
          dunstify --replace=98465 "Mails " "No new mail";
      fi

Open Mails

  $TERMINAL -e "tmux new-session -A -s neomutt neomutt" &

Unread News

  [module/unread_news]
  type = custom/script

  format-underline = ${colors.bg}
  click-left = ~/.config/polybar/scripts/news-open.sh
  click-right = ~/.config/polybar/scripts/news-refresh.sh &
  format = <label>
  exec = ~/.config/polybar/scripts/news-status.sh
  interval = 60

Unread News Scripts

  news_nb=`newsboat -x print-unread 2>/dev/null | cut -d " " -f1`

 if [ -z "$news_nb" ] || [ "$news_nb" -eq "0" ]; then
    echo "";
  else
    echo "%{F#859900}  $news_nb%{F-}";
  fi

Refresh News Scripts

  dunstify --replace=38492 "Newsboat " "Reloading...";

  if pgrep -x "newsboat" >/dev/null; then
    # If newsboat is already running, try to refresh in the tmux session
    tmux send-keys -t newsboat R
  else
    newsboat -x reload && /
        news_nb=`newsboat -x print-unread | cut -d " " -f1` && \
        dunstify --replace=38492 "Newsboat " "$news_nb Unread News";
  fi

Open Newsboat

  $TERMINAL -e "tmux new-session -A -s newsboat newsboat" &

Redshift

  [module/redshift]
  type = custom/script

  format-underline = ${colors.bg}
  click-left = ~/.config/polybar/scripts/redshift-toggle.sh
  format = <label>
  exec = ~/.config/polybar/scripts/redshift-status.sh
  interval = 2

Redshift - Status

  if pgrep -x "redshift" >/dev/null; then
      echo "望";
  else
      echo "盛";
  fi

Toggle Redshift

  if pgrep -x "redshift" >/dev/null; then
      killall redshift && \
      dunstify --replace=36492 "Redshift 望" "Turned off";
  else
      nohup redshift > /dev/null 2>&1 &
      dunstify --replace=36492 "Redshift 望" "Starting...";
  fi

NordVPN

  [module/nordvpn]
  type = custom/script

  format-underline = ${colors.bg}
  click-left = ~/.config/polybar/scripts/nordvpn-toggle.sh
  format = <label>
  exec = ~/.config/polybar/scripts/nordvpn-status.sh
  interval = 2

Nordvpn Status script

  tmpfile="/tmp/vpnstatus";

  if [ -f $tmpfile ] && grep -q "on" $tmpfile; then
    echo "%{F#859900}%{F-}";
  else
    echo "";
  fi

Nordvpn Toggle

  tmpfile="/tmp/vpnstatus";

  if [ -f $tmpfile ] && grep -q "on" $tmpfile; then
    nordvpn disconnect && \
        dunstify --replace=23198 "VPN" "Disconnected" && \
        echo "off" > $tmpfile;
  else
    country=`cat ~/bin/nordvpn_countries.txt | sed 's/\s*\t\s*/ /g ; s/\s/\n/g ; s/_/ /g ; /^[a-zA-Z]/!d ; s/\(.*\)/\L\1/' | rofi -i -dmenu | sed 's/\s/_/g'`;
    dunstify --replace=23198 "VPN" "Connecting to $country...";
    nordvpn connect $country && \
        dunstify --replace=23198 "VPN" "Connected to $country" && \
        echo "on" > $tmpfile;
  fi

Packages

  [module/packages]
  type = custom/script

  format-underline = ${colors.bg}
  click-left = ~/.config/polybar/scripts/packages-update.sh
  click-right = ~/.config/polybar/scripts/packages-refresh.sh
  format = <label>
  exec = ~/.config/polybar/scripts/packages-status.sh
  interval = 600

Packages Status script

  yay -Sy > /dev/null 2>&1 && \
      package_nb=`yay -Qu 2> /dev/null | wc -l` || \
      package_nb=0


  if [ "$package_nb" -eq "0" ]; then
      echo "";
  else
      echo "%{F#859900} $package_nb%{F-}";
  fi

Refresh New Packages script

  dunstify --replace=64654 'Packages ' "Refreshing..."

  yay -Sy > /dev/null 2>&1 && \
      package_nb=`yay -Qu 2> /dev/null | wc -l`;

  if [ "$package_nb" -eq "0" ]; then
    dunstify --replace=64654 'Packages ' "No upgrade available"
  else
    dunstify --replace=64654 'Packages ' "$(package_nb) upgrade(s) available"
  fi

Packages Update

  $TERMINAL -e "yay" &

Dunst

  [module/dunst]
  type = custom/script

  format-underline = ${colors.bg}
  click-left = ~/.config/polybar/scripts/dunst-toggle.sh
  format = <label>
  exec = ~/.config/polybar/scripts/dunst-status.sh
  interval = 2

Dunst Status script

  tmpfile="/tmp/dunststatus";

  if [ -f $tmpfile ]; then
    if grep -q "on" $tmpfile; then
      echo "";
    elif grep -q "off" $tmpfile; then
      echo "";
    fi
  else
    echo "";
  fi

Dunst Toggle

  tmpfile="/tmp/dunststatus";

  if [ -f $tmpfile ] && grep -q "off" $tmpfile ; then
      killall -SIGUSR2 dunst && \
          echo "on" > $tmpfile;
      dunstify --replace=16549 "Notifications " "Activated";
  else
      dunstify --replace=16549 "Notifications " "Deactivated";
      sleep 1 && \
          killall -SIGUSR1 dunst && \
          echo "off" > $tmpfile;
  fi

Lock Screen

  [module/lockscreen]
  type = custom/script

  format-underline = ${colors.bg}
  click-left = ~/.config/polybar/scripts/lockscreen-toggle.sh
  click-right = ~/scripts/lockscreen.sh
  format = <label>
  exec = ~/.config/polybar/scripts/lockscreen-status.sh

  interval = 2

Lock screen Status script

  if pgrep -x "xautolock" >/dev/null
  then
      echo "";
  else
      echo "";
  fi

Toggle Automatic Lock Screen

  if pgrep -x "xautolock" >/dev/null ; then
      pkill xautolock && \
          dunstify --replace=13602 'Lock Screen ' 'Desactivated'
  else
      xautolock -locker "~/scripts/lockscreen.sh" -detectsleep -time 30 -notify 60 -notifier "dunstify --replace=31846 -u critical -t 10000 -- 'Locking Screen' '60 seconds'" &
      dunstify --replace=13602 'Lock Screen ' 'Activated'
  fi

Network

  [module/wired-network]
  type = internal/network
  interface = enp0s20f0u9u4

  label-connected = %local_ip%
  label-connected-foreground = ${color.fg}

  format-connected-prefix = " "
  format-connected = <label-connected>

  label-disconnected = ""
  label-disconnected-foreground = ${colors.gray}

  format-disconnected = <label-disconnected>

  [module/wireless-network]
  type = internal/network
  interface = wlp2s0

  label-connected = %essid%
  label-connected-foreground = ${color.fg}

  format-connected-prefix = " "
  format-connected = <label-connected>

  label-disconnected = ""
  label-disconnected-foreground = ${colors.gray}

  format-disconnected = <label-disconnected>

Wireguard

  [module/wireguard]
  type = custom/script

  format-underline = ${colors.bg}
  click-left = ~/.config/polybar/scripts/wireguard-toggle.sh
  format = <label>
  exec = ~/.config/polybar/scripts/wireguard-status.sh

  interval = 2

Wireguard Connection Status

  config="pivpn"

  connection=$(sudo wg show "$config" 2>/dev/null | head -n 1 | awk '{print $NF }')

  if [ "$connection" = "$config" ]; then
      echo "%{F#859900}%{F-}"
  else
      echo ""
  fi

Toggle Wireguard Connection

  config="pivpn"

  connection=$(sudo wg show "$config" 2>/dev/null | head -n 1 | awk '{print $NF }')

  if [ "$connection" = "$config" ]; then
      sudo wg-quick down "$config" && \
          dunstify --replace=83244 "Wireguard" "Disconnected from $config"
  else
      sudo wg-quick up "$config" && \
          dunstify --replace=83244 "Wireguard" "Connected to $config"
  fi

Screenshot

  [module/screenshot]
  type = custom/text
  content = ""
  click-left = ~/bin/screenshot

Scripts

Launch

  # Terminate already running bar instances
  killall -q polybar

  # Wait until the processes have been shut down
  while pgrep -u $UID -x polybar >/dev/null; do sleep 1; done

  # Launch Polybar, using default config location ~/.config/polybar/config
  polybar top >>/tmp/polybar.log 2>&1 &
  # polybar bottom &

  echo "Polybar launched..."

Toggle

  # Usage : toggle.sh top

  pid=$(pgrep -f "polybar $1")

  if [ ! -z "$pid" ]; then
      polybar-msg -p $pid cmd toggle >/dev/null 2>&1
  fi