From 6706c93797ab52b1015edae9b51b5ee68f19b679 Mon Sep 17 00:00:00 2001 From: Tristan Ferrua Date: Sun, 31 Jan 2021 14:50:15 +0000 Subject: [PATCH] =?UTF-8?q?Added=20support=20for=20=C3=9Cberzug=20to=20dis?= =?UTF-8?q?play=20album=20art?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/miniplayer | 73 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 64 insertions(+), 9 deletions(-) diff --git a/bin/miniplayer b/bin/miniplayer index d2a3bd9..7f0c014 100755 --- a/bin/miniplayer +++ b/bin/miniplayer @@ -7,6 +7,7 @@ import ffmpeg import pixcat import time import configparser +import ueberzug.lib.v0 as ueberzug from PIL import Image, ImageDraw # Get config @@ -16,7 +17,8 @@ config.read(os.path.expanduser("~/.config/miniplayer/config")) if "player" not in config.sections(): config["player"] = {"music_directory": "~/Music", "font_width": 11, - "font_height": 24 + "font_height": 24, + "image_method": "pixcat" } if "mpd" not in config.sections(): @@ -41,6 +43,9 @@ MUSICDIR = os.path.expanduser(MUSICDIR) MPDHOST = mpd_config.get("host", "localhost") MPDPORT = mpd_config.getint("port", 6600) +# What to use to draw images +IMAGEMETHOD = player_config.get("image_method", "pixcat") + def albumArtSize(album_space, window_width): """ @@ -99,6 +104,9 @@ class Player: self.help = False self.cleared = False + # Ueberzug placement + self.art_placement = None + def fitText(self): """ @@ -344,15 +352,40 @@ class Player: self.win.refresh() + def hideAlbumArt(self): + """ + A function that hides the album art + """ + if IMAGEMETHOD == "ueberzug": + self.art_placement.visibility = ueberzug.Visibility.INVISIBLE + + def drawAlbumArt(self): """ A function to draw the album art """ - ( - pixcat.Image(self.album_art_loc) - .thumbnail(self.image_width_px ) - .show(x=(self.window_width - self.image_width)//2, y=self.image_y_pos) - ) + + if IMAGEMETHOD == "ueberzug": + # Figure out new placement + self.art_placement.x = (self.window_width - self.image_width)//2 + self.art_placement.y = self.image_y_pos + + # Figure out height and width + self.art_placement.width = self.image_width + self.art_placement.height = self.album_space + + # Update image + self.art_placement.path = self.album_art_loc + + # Display image + self.art_placement.visibility = ueberzug.Visibility.VISIBLE + + elif IMAGEMETHOD == "pixcat": + ( + pixcat.Image(self.album_art_loc) + .thumbnail(self.image_width_px ) + .show(x=(self.window_width - self.image_width)//2, y=self.image_y_pos) + ) def centerText(self, y: int, string: str): @@ -434,6 +467,8 @@ class Player: # Check if state is stop if state == 1: self.win.clear() + self.hideAlbumArt() + infomsg = "Put some beats on!" self.win.addstr(self.window_height // 2, (self.window_width - len(infomsg)) // 2, infomsg) @@ -446,7 +481,19 @@ class Player: self.drawAlbumArt() - def loop(self): + @ueberzug.Canvas() + def loop(self, canvas): + """ + The main program loop + """ + + if self.art_placement is None and IMAGEMETHOD == "ueberzug": + # Create album art placement if we are using ueberzug + self.art_placement = canvas.create_placement( + "art", + scaler=ueberzug.ScalerOption.FIT_CONTAIN.value + ) + try: i = 0 while True: @@ -461,6 +508,7 @@ class Player: self.draw() else: + self.hideAlbumArt() self.drawHelp() e = time.perf_counter() @@ -483,6 +531,13 @@ class Player: print(error) -player = Player() -player.loop() +try: + player = Player() + player.loop() +except ConnectionRefusedError: + curses.nocbreak() + curses.endwin() + print(f"Could not connect to mpd on {MPDHOST}:{MPDPORT}") + +