weechat-np/np.pl
2025-03-07 23:10:51 -06:00

83 lines
2.8 KiB
Perl

# np.pl
# https://git.everypizza.im/n/weechat-np
#
# Originally https://github.com/stackptr/weechat-mpd-np/
#
# Colorized now playing script for playerctl.
#
# Use /np to print current song to buffer
use strict;
use warnings;
my $descr = 'Colorized now playing script for playerctl';
weechat::register('np', 'stackptr', '0.1', 'GPL3', $descr, '', '');
weechat::hook_command('np', $descr, '',
"Settings:
Turn on/off color formatting for messages:
/set plugins.var.perl.np.use_color <on|off>
Default: On
Define colors for messages:
/set plugins.var.perl.np.color.{title,artist,album,date} <\"color\">
Defaults: title - lightmagenta
artist - blue
album - lightgreen
date - yellow
Define format for now playing:
/set plugins.var.perl.np.format <format>
\%title\%, \%artist\%, \%album\% in addition to text
Default: \"\%title\% by \%artist\% from \%album\%\"
", '', 'start', '');
return weechat::WEECHAT_RC_OK;
sub start {
my $output = "/me is now playing: ";
my %data = (
"title" => `playerctl metadata title`,
"artist" =>`playerctl metadata artist`,
"album" => `playerctl metadata album`,
);
chomp %data;
%data = addColor(\%data) unless weechat::config_get_plugin("use_color") eq "off";
# Todo: Add ability to change foramt
$output .= $data{"title"}." by ".$data{"artist"}." from ".$data{"album"}."";
weechat::command(weechat::current_buffer, $output)
}
sub addColor {
# Color table to translate names to color codes
my %color_table = (white => "00", black => "01", darkblue => "02", darkgreen => "03", lightred => "04",
darkred => "05", magenta => "06", orange => "07", yellow => "08", lightgreen => "09",
cyan => "10", lightcyan => "11", lightblue => "12", lightmagenta => "13", gray => "14",
lightgray => "15");
# Make sure colors are either defined or defaults
weechat::config_set_plugin("color.title", "lightmagenta") if weechat::config_get_plugin("color.title") eq "";
weechat::config_set_plugin("color.artist", "lightblue") if weechat::config_get_plugin("color.artist") eq "";
weechat::config_set_plugin("color.album", "lightgreen") if weechat::config_get_plugin("color.album") eq "";
# Convert to raw color codes
my $color_title = "\cC" . $color_table{(weechat::config_get_plugin("color.title"))};
my $color_artist = "\cC" . $color_table{(weechat::config_get_plugin("color.artist"))};
my $color_album = "\cC" . $color_table{(weechat::config_get_plugin("color.album"))};
my $reset_color = "\cC00";
return (
"title" => $color_title.$_[0]{"title"}.$reset_color,
"artist" => $color_artist.$_[0]{"artist"}.$reset_color,
"album" => $color_album.$_[0]{"album"}.$reset_color
)
}