Advertisement
Guest User

Untitled

a guest
Feb 28th, 2024
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.89 KB | Source Code | 0 0
  1. #!/bin/bash
  2.  
  3. ###################################################################
  4. #Script Name    :music_bar
  5. #Description    :polybar module for displaying music bar
  6. #Args           :
  7. #Author         :unixsilk
  8. #Email          :
  9. ###################################################################
  10.  
  11. function check_if_something_plays() {
  12.  
  13.     no_player=$(playerctl status 2>&1)
  14.     if [ "${no_player}" == "No players found" ]; then
  15.         exit # Exit the entire script without any further output
  16.     fi
  17.  
  18. }
  19.  
  20. check_if_something_plays
  21.  
  22. find_playing_player() {
  23.  
  24.     for each_player in $(playerctl -l); do
  25.         status=$(playerctl -p "${each_player}" status)
  26.         if [ "${status}" == "Playing" ]; then
  27.             player="${each_player}"
  28.             break
  29.         else
  30.             exit
  31.         fi
  32.     done
  33.  
  34. }
  35.  
  36. find_string_length() {
  37.  
  38.     grab_artist=$(playerctl -p "${player}" metadata artist)
  39.     grab_title=$(playerctl -p "${player}" metadata title)
  40.  
  41.     combined_length=$((${#grab_artist} + ${#grab_title}))
  42.  
  43.     if [[ ${combined_length} -ge 55 ]]; then
  44.         length="greater"
  45.     else
  46.         length="lesser"
  47.     fi
  48.  
  49. }
  50.  
  51. function set_timestamps() {
  52.  
  53.     current_duration=$(playerctl -p "${player}" metadata --format '{{duration(position)}}')
  54.     total_duration=$(playerctl -p "${player}" metadata --format '{{duration(mpris:length)}}')
  55.  
  56. }
  57.  
  58. function print_firefox_bar_moving() {
  59.  
  60.     title_string_length=${#grab_title}
  61.  
  62.     counter=0
  63.  
  64.     for ((each = 0; each <= title_string_length; each++)); do
  65.         echo -e "${begin_white}""" "${grab_artist:0:15}" "•" "${end_color}""${grab_title:counter:55}"
  66.         ((counter++))
  67.         sleep 0.19
  68.     done
  69.  
  70. }
  71.  
  72. function print_firefox_bar_static() {
  73.     echo "${begin_white}""" "${grab_artist}" "•" "${end_color}""${grab_title}"
  74.  
  75. }
  76.  
  77. function print_other_player_bar_moving() {
  78.  
  79.     title_string_length=${#grab_title}
  80.  
  81.     counter=0
  82.  
  83.     for ((each = 0; each <= title_string_length; each++)); do
  84.         set_timestamps
  85.         echo -e "${begin_yellow}""${current_duration}""/""${total_duration}" "${begin_white}""" "${grab_artist:0:15}" "•" "${end_color}""${grab_title:counter:40}"
  86.         ((counter++))
  87.         sleep 0.19
  88.     done
  89. }
  90.  
  91. function print_other_player_bar_static() {
  92.  
  93.     echo -e "${begin_yellow}""${current_duration}""/""${total_duration}" "${end_color}""${begin_white}""" "${grab_artist:0:9}" "•" "${end_color}""${grab_title}"
  94.  
  95. }
  96.  
  97. function define_colors() {
  98.  
  99.     begin_yellow="%{F#F0C674}"
  100.     begin_white="%{F#FFFFFF}"
  101.     end_color="%{F-}"
  102. }
  103.  
  104. #Find which player is playing currently and define that as variable $player
  105. find_playing_player
  106.  
  107. #find the string length of title and artist
  108. find_string_length
  109.  
  110. #invoke ANSI colors for Polybar
  111. define_colors
  112.  
  113. combine_values="${player}-${length}"
  114.  
  115. case "${combine_values}" in
  116. firefox*-greater)
  117.     print_firefox_bar_moving
  118.     ;;
  119. firefox*-lesser)
  120.     print_firefox_bar_static
  121.     ;;
  122. *-greater)
  123.     set_timestamps
  124.     print_other_player_bar_moving
  125.     ;;
  126. *-lesser)
  127.     set_timestamps
  128.     print_other_player_bar_static
  129.     ;;
  130. *)
  131.     exit
  132.     ;;
  133. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement