<commit_before>#include <thread>

#include "bar.hpp"
#include "lemonbuddy.hpp"
#include "modules/mpd.hpp"
#include "utils/config.hpp"
#include "utils/string.hpp"
#include "utils/memory.hpp"

using namespace modules;
using namespace mpd;

MpdModule::MpdModule(std::string name_)
  : EventModule(name_), icons(std::make_unique<drawtypes::IconMap>())
{
  // Load configuration values {{{
  this->mpd_host = config::get<std::string>(this->name(), "host", "127.0.0.1");
  this->mpd_port = config::get<int>(this->name(), "port", 6600);
  this->mpd_pass = config::get<std::string>(this->name(), "password", "");
  // }}}

  // Add formats and elements {{{
  this->formatter->add(FORMAT_ONLINE, TAG_LABEL_SONG, {
    TAG_BAR_PROGRESS, TAG_TOGGLE,      TAG_LABEL_SONG,      TAG_LABEL_TIME,
    TAG_ICON_RANDOM,  TAG_ICON_REPEAT, TAG_ICON_REPEAT_ONE, TAG_ICON_PREV,
    TAG_ICON_STOP,    TAG_ICON_PLAY,   TAG_ICON_PAUSE,      TAG_ICON_NEXT });
  this->formatter->add(FORMAT_OFFLINE, "", { TAG_LABEL_OFFLINE });

  if (this->formatter->has(TAG_ICON_PLAY) || this->formatter->has(TAG_TOGGLE))
    this->icons->add("play", drawtypes::get_config_icon(name(), get_tag_name(TAG_ICON_PLAY)));
  if (this->formatter->has(TAG_ICON_PAUSE) || this->formatter->has(TAG_TOGGLE))
    this->icons->add("pause", drawtypes::get_config_icon(name(), get_tag_name(TAG_ICON_PAUSE)));
  if (this->formatter->has(TAG_ICON_STOP))
    this->icons->add("stop", drawtypes::get_config_icon(name(), get_tag_name(TAG_ICON_STOP)));
  if (this->formatter->has(TAG_ICON_PREV))
    this->icons->add("prev", drawtypes::get_config_icon(name(), get_tag_name(TAG_ICON_PREV)));
  if (this->formatter->has(TAG_ICON_NEXT))
    this->icons->add("next", drawtypes::get_config_icon(name(), get_tag_name(TAG_ICON_NEXT)));
  if (this->formatter->has(TAG_ICON_RANDOM))
    this->icons->add("random", drawtypes::get_config_icon(name(), get_tag_name(TAG_ICON_RANDOM)));
  if (this->formatter->has(TAG_ICON_REPEAT))
    this->icons->add("repeat", drawtypes::get_config_icon(name(), get_tag_name(TAG_ICON_REPEAT)));
  if (this->formatter->has(TAG_ICON_REPEAT_ONE))
    this->icons->add("repeat_one", drawtypes::get_config_icon(name(), get_tag_name(TAG_ICON_REPEAT_ONE)));

  if (this->formatter->has(TAG_LABEL_SONG)) {
    this->label_song = drawtypes::get_optional_config_label(name(), get_tag_name(TAG_LABEL_SONG), "%artist% - %title%");
    this->label_song_tokenized = this->label_song->clone();
  }
  if (this->formatter->has(TAG_LABEL_TIME)) {
    this->label_time = drawtypes::get_optional_config_label(name(), get_tag_name(TAG_LABEL_TIME), "%elapsed% / %total%");
    this->label_time_tokenized = this->label_time->clone();
  }
  if (this->formatter->has(TAG_ICON_RANDOM) || this->formatter->has(TAG_ICON_REPEAT) || this->formatter->has(TAG_ICON_REPEAT_ONE)) {
    this->toggle_on_color = config::get<std::string>(name(), "toggle_on-foreground", "");
    this->toggle_off_color = config::get<std::string>(name(), "toggle_off-foreground", "");
  }
  if (this->formatter->has(TAG_LABEL_OFFLINE, FORMAT_OFFLINE))
    this->label_offline = drawtypes::get_config_label(name(), get_tag_name(TAG_LABEL_OFFLINE));
  if (this->formatter->has(TAG_BAR_PROGRESS)) {
    this->bar_progress = drawtypes::get_config_bar(name(), get_tag_name(TAG_BAR_PROGRESS));
  }
  // }}}

  // Sign up for stdin events {{{
  register_command_handler(name());
  // }}}
}

MpdModule::~MpdModule()
{
  std::lock_guard<concurrency::SpinLock> lck(this->update_lock);
  this->status.reset();
}

void MpdModule::start()
{
  this->mpd = std::make_unique<mpd::Connection>(this->mpd_host, this->mpd_port, this->mpd_pass);

  this->synced_at = std::chrono::system_clock::now();
  this->sync_interval = config::get<float>(name(), "interval", this->sync_interval) * 1000;

  try {
    this->mpd->connect();
    this->status = this->mpd->get_status();
    this->status->update(-1, this->mpd);
  } catch (mpd::Exception &e) {
    log_error(e.what());
    this->mpd->disconnect();
  }

  this->EventModule::start();
}

bool MpdModule::has_event()
{
  bool has_event = false;

  if (!this->mpd->connected()) {<commit_msg>fix(mpd): Disconnect on destruct<commit_after>#include <thread>

#include "bar.hpp"
#include "lemonbuddy.hpp"
#include "modules/mpd.hpp"
#include "utils/config.hpp"
#include "utils/string.hpp"
#include "utils/memory.hpp"

using namespace modules;
using namespace mpd;

MpdModule::MpdModule(std::string name_)
  : EventModule(name_), icons(std::make_unique<drawtypes::IconMap>())
{
  // Load configuration values {{{
  this->mpd_host = config::get<std::string>(this->name(), "host", "127.0.0.1");
  this->mpd_port = config::get<int>(this->name(), "port", 6600);
  this->mpd_pass = config::get<std::string>(this->name(), "password", "");
  // }}}

  // Add formats and elements {{{
  this->formatter->add(FORMAT_ONLINE, TAG_LABEL_SONG, {
    TAG_BAR_PROGRESS, TAG_TOGGLE,      TAG_LABEL_SONG,      TAG_LABEL_TIME,
    TAG_ICON_RANDOM,  TAG_ICON_REPEAT, TAG_ICON_REPEAT_ONE, TAG_ICON_PREV,
    TAG_ICON_STOP,    TAG_ICON_PLAY,   TAG_ICON_PAUSE,      TAG_ICON_NEXT });
  this->formatter->add(FORMAT_OFFLINE, "", { TAG_LABEL_OFFLINE });

  if (this->formatter->has(TAG_ICON_PLAY) || this->formatter->has(TAG_TOGGLE))
    this->icons->add("play", drawtypes::get_config_icon(name(), get_tag_name(TAG_ICON_PLAY)));
  if (this->formatter->has(TAG_ICON_PAUSE) || this->formatter->has(TAG_TOGGLE))
    this->icons->add("pause", drawtypes::get_config_icon(name(), get_tag_name(TAG_ICON_PAUSE)));
  if (this->formatter->has(TAG_ICON_STOP))
    this->icons->add("stop", drawtypes::get_config_icon(name(), get_tag_name(TAG_ICON_STOP)));
  if (this->formatter->has(TAG_ICON_PREV))
    this->icons->add("prev", drawtypes::get_config_icon(name(), get_tag_name(TAG_ICON_PREV)));
  if (this->formatter->has(TAG_ICON_NEXT))
    this->icons->add("next", drawtypes::get_config_icon(name(), get_tag_name(TAG_ICON_NEXT)));
  if (this->formatter->has(TAG_ICON_RANDOM))
    this->icons->add("random", drawtypes::get_config_icon(name(), get_tag_name(TAG_ICON_RANDOM)));
  if (this->formatter->has(TAG_ICON_REPEAT))
    this->icons->add("repeat", drawtypes::get_config_icon(name(), get_tag_name(TAG_ICON_REPEAT)));
  if (this->formatter->has(TAG_ICON_REPEAT_ONE))
    this->icons->add("repeat_one", drawtypes::get_config_icon(name(), get_tag_name(TAG_ICON_REPEAT_ONE)));

  if (this->formatter->has(TAG_LABEL_SONG)) {
    this->label_song = drawtypes::get_optional_config_label(name(), get_tag_name(TAG_LABEL_SONG), "%artist% - %title%");
    this->label_song_tokenized = this->label_song->clone();
  }
  if (this->formatter->has(TAG_LABEL_TIME)) {
    this->label_time = drawtypes::get_optional_config_label(name(), get_tag_name(TAG_LABEL_TIME), "%elapsed% / %total%");
    this->label_time_tokenized = this->label_time->clone();
  }
  if (this->formatter->has(TAG_ICON_RANDOM) || this->formatter->has(TAG_ICON_REPEAT) || this->formatter->has(TAG_ICON_REPEAT_ONE)) {
    this->toggle_on_color = config::get<std::string>(name(), "toggle_on-foreground", "");
    this->toggle_off_color = config::get<std::string>(name(), "toggle_off-foreground", "");
  }
  if (this->formatter->has(TAG_LABEL_OFFLINE, FORMAT_OFFLINE))
    this->label_offline = drawtypes::get_config_label(name(), get_tag_name(TAG_LABEL_OFFLINE));
  if (this->formatter->has(TAG_BAR_PROGRESS)) {
    this->bar_progress = drawtypes::get_config_bar(name(), get_tag_name(TAG_BAR_PROGRESS));
  }
  // }}}

  // Sign up for stdin events {{{
  register_command_handler(name());
  // }}}
}

MpdModule::~MpdModule()
{
  std::lock_guard<concurrency::SpinLock> lck(this->update_lock);
  {
    if (this->mpd->connected()) {
      try {
        this->mpd->disconnect();
      } catch (mpd::Exception &e) {
        get_logger()->debug(e.what());
      }
    }
  }
}

void MpdModule::start()
{
  this->mpd = std::make_unique<mpd::Connection>(this->mpd_host, this->mpd_port, this->mpd_pass);

  this->synced_at = std::chrono::system_clock::now();
  this->sync_interval = config::get<float>(name(), "interval", this->sync_interval) * 1000;

  try {
    this->mpd->connect();
    this->status = this->mpd->get_status();
    this->status->update(-1, this->mpd);
  } catch (mpd::Exception &e) {
    log_error(e.what());
    this->mpd->disconnect();
  }

  this->EventModule::start();
}

bool MpdModule::has_event()
{
  bool has_event = false;

  if (!this->mpd->connected()) {
{'fraction_non_alphanumeric': 0.18835412953060013, 'fraction_numerical': 0.0035650623885918, 'mean_word_length': 5.825628548256286, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 215, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1735148', 'n_tokens_mistral': 3224, 'n_tokens_neox': 3143, 'n_words': 372}