task: Removed torrent module
This commit is contained in:
parent
ff7e15db88
commit
55f5a6fb70
34
config
34
config
@ -126,7 +126,7 @@ font-3 = NotoSans-Regular:size=8;-1
|
||||
font-4 = MaterialIcons:size=10;-1
|
||||
|
||||
modules-left = bspwm
|
||||
modules-right = rtorrent cpu memory
|
||||
modules-right = cpu memory
|
||||
; modules-right = cpu memory
|
||||
|
||||
[bar/external_bottom]
|
||||
@ -760,38 +760,6 @@ label-connected = %{T3}%local_ip%%{T-}
|
||||
;label-packetloss = %essid%
|
||||
;label-packetloss-foreground = #eefafafa
|
||||
|
||||
[module/rtorrent]
|
||||
type = internal/rtorrent
|
||||
|
||||
script = /home/jaagr/var/github/jaagr/lemonbuddy/scripts/torrents.sh
|
||||
rtorrent_session_dir = /home/jaagr/.cache/rtorrent
|
||||
display_count = 2
|
||||
title_maxlen = 30
|
||||
|
||||
; Available tags:
|
||||
; <label> (default)
|
||||
; <bar-progress>
|
||||
;format = <label>
|
||||
|
||||
; Available tokens:
|
||||
; %title%
|
||||
; %percentage%
|
||||
; Default- %label% (%percentage%)
|
||||
label = %{F#fba922}%{F-} %{F#eefafafa}%title% %percentage%%{F-}
|
||||
;label-foreground = #eefafafa
|
||||
|
||||
; Required if <bar-progress> is used
|
||||
;bar-progress-width = 10
|
||||
;bar-progress-format = %{+u}%{+o}%fill%%{-u}%{-o}%indicator%%{+u}%{+o}%empty%%{-u}%{-o}
|
||||
;bar-progress-indicator = |
|
||||
;bar-progress-indicator-foreground = ${BAR.foreground}
|
||||
;bar-progress-indicator-font = 2
|
||||
;bar-progress-fill = █
|
||||
;bar-progress-fill-foreground = #5a5
|
||||
;bar-progress-fill-font = 2
|
||||
;bar-progress-empty = █
|
||||
;bar-progress-empty-foreground = #555
|
||||
;bar-progress-empty-font = 2
|
||||
|
||||
[module/volume]
|
||||
type = internal/volume
|
||||
|
@ -1,39 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "modules/base.hpp"
|
||||
#include "drawtypes/bar.hpp"
|
||||
#include "drawtypes/label.hpp"
|
||||
|
||||
namespace modules
|
||||
{
|
||||
struct Torrent
|
||||
{
|
||||
std::string title;
|
||||
unsigned long data_total;
|
||||
unsigned long data_downloaded;
|
||||
unsigned long data_remaining;
|
||||
float perc_downloaded;
|
||||
std::unique_ptr<drawtypes::Label> label_tokenized;
|
||||
};
|
||||
|
||||
DefineModule(TorrentModule, InotifyModule)
|
||||
{
|
||||
static constexpr auto TAG_LABEL = "<label>";
|
||||
static constexpr auto TAG_BAR_PROGRESS = "<bar-progress>";
|
||||
|
||||
std::vector<std::unique_ptr<Torrent>> torrents;
|
||||
std::unique_ptr<drawtypes::Label> label;
|
||||
std::unique_ptr<drawtypes::Label> label_tokenized;
|
||||
std::unique_ptr<drawtypes::Bar> bar;
|
||||
std::string pipe_cmd;
|
||||
|
||||
std::vector<std::unique_ptr<Torrent>> &read_data_into(std::vector<std::unique_ptr<Torrent>> &container);
|
||||
|
||||
public:
|
||||
explicit TorrentModule(const std::string& name);
|
||||
|
||||
void start();
|
||||
bool on_event(InotifyEvent *event);
|
||||
bool build(Builder *builder, const std::string& tag);
|
||||
};
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Creates a summary of active torrents
|
||||
#
|
||||
|
||||
main() {
|
||||
local rtorrent_session_dir=${1:-"${HOME}/.cache/rtorrent"} ; shift
|
||||
local max_count=${1:-3} ; shift
|
||||
local cap=${1:-40} ; shift
|
||||
|
||||
local file target_dir chunks_wanted chunks_done chunks_total i
|
||||
|
||||
for file in $(find "$rtorrent_session_dir" -name '*.rtorrent' | sed -nr 's/^(.*)\.rtorrent$/\0/p'); do
|
||||
target_dir=$(sed -nr 's/.*directory[0-9]+:(.*)7:hashing.*/\1/p' "$file")
|
||||
|
||||
state=$(egrep -ro "statei([0-9]+)e13" "$file")
|
||||
state=${state##*i}
|
||||
state=${state%%e*}
|
||||
|
||||
chunks_done=$(egrep -ro "chunks_donei([0-9]+)e13" "$file")
|
||||
chunks_wanted=$(egrep -ro "chunks_wantedi([0-9]+)e8" "$file")
|
||||
chunks_done=${chunks_done##*i}
|
||||
chunks_done=${chunks_done%%e*}
|
||||
chunks_wanted=${chunks_wanted##*i}
|
||||
chunks_wanted=${chunks_wanted%%e*}
|
||||
chunks_total=$(( chunks_done + chunks_wanted ))
|
||||
|
||||
if (( $(sed -nr 's/.*statei([0-9]+)e13.*/\1/p' "$file") )); then
|
||||
[[ "$chunks_total" == "$chunks_wanted" ]] && [[ $chunks_done -eq 0 ]] && continue;
|
||||
|
||||
num_files=$(( num_files + 1 ))
|
||||
label=$(echo "$target_dir" | sed -nr 's/\//\n/gp' | tail -1)
|
||||
|
||||
if [[ ${#label} -gt $cap ]]; then
|
||||
label=${label:0:$cap}
|
||||
label="${label% *}..."
|
||||
fi
|
||||
|
||||
echo "${label}:${chunks_total:-0}:${chunks_done:-0}:${chunks_wanted:-0}"
|
||||
|
||||
i=$(( i + 1 ))
|
||||
|
||||
[[ $i -ge $max_count ]] && break
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
main "$@"
|
@ -41,7 +41,6 @@ set(SOURCE_FILES
|
||||
"src/modules/menu.cpp"
|
||||
"src/modules/script.cpp"
|
||||
"src/modules/text.cpp"
|
||||
# "src/modules/torrent.cpp"
|
||||
"src/services/builder.cpp"
|
||||
"src/services/command.cpp"
|
||||
"src/services/inotify.cpp"
|
||||
|
@ -13,7 +13,6 @@
|
||||
#include "modules/menu.hpp"
|
||||
#include "modules/script.hpp"
|
||||
#include "modules/text.hpp"
|
||||
#include "modules/torrent.hpp"
|
||||
#ifdef ENABLE_I3
|
||||
#include "modules/i3.hpp"
|
||||
#endif
|
||||
@ -172,9 +171,6 @@ void Bar::load()
|
||||
module = std::make_unique<modules::VolumeModule>(mod);
|
||||
#else
|
||||
throw CompiledWithoutModuleSupport("volume");
|
||||
#endif
|
||||
#if 0
|
||||
else if (type == "internal/rtorrent") module = std::make_unique<modules::TorrentModule>(mod);
|
||||
#endif
|
||||
else if (type == "custom/text") module = std::make_unique<modules::TextModule>(mod);
|
||||
else if (type == "custom/script") module = std::make_unique<modules::ScriptModule>(mod);
|
||||
|
@ -1,123 +0,0 @@
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <unistd.h>
|
||||
#include <sstream>
|
||||
|
||||
#include "lemonbuddy.hpp"
|
||||
#include "services/command.hpp"
|
||||
#include "services/builder.hpp"
|
||||
#include "utils/io.hpp"
|
||||
#include "utils/proc.hpp"
|
||||
#include "utils/string.hpp"
|
||||
#include "utils/config.hpp"
|
||||
#include "modules/torrent.hpp"
|
||||
|
||||
using namespace modules;
|
||||
|
||||
// TODO: Parse the torrent files internally instead of using the bash script
|
||||
|
||||
TorrentModule::TorrentModule(const std::string& name_) : InotifyModule(name_)
|
||||
{
|
||||
this->formatter->add(DEFAULT_FORMAT, TAG_LABEL, { TAG_LABEL, TAG_BAR_PROGRESS });
|
||||
|
||||
if (this->formatter->has(TAG_LABEL))
|
||||
this->label = drawtypes::get_optional_config_label(name(), get_tag_name(TAG_LABEL), "%title% (%percentage%)");
|
||||
if (this->formatter->has(TAG_BAR_PROGRESS))
|
||||
this->bar = drawtypes::get_config_bar(name(), get_tag_name(TAG_BAR_PROGRESS));
|
||||
|
||||
auto rtorrent_session_dir = config::get<std::string>(name(), "rtorrent_session_dir");
|
||||
|
||||
this->watch(rtorrent_session_dir);
|
||||
|
||||
this->pipe_cmd = config::get<std::string>(name(), "script")
|
||||
+ " " + rtorrent_session_dir
|
||||
+ " " + std::to_string(config::get<int>(name(), "display_count", 2))
|
||||
+ " " + std::to_string(config::get<int>(name(), "title_maxlen", 30));
|
||||
}
|
||||
|
||||
void TorrentModule::start()
|
||||
{
|
||||
this->InotifyModule<TorrentModule>::start();
|
||||
|
||||
std::thread manual_updater([&]{
|
||||
while (this->enabled()) {
|
||||
std::this_thread::sleep_for(5s);
|
||||
this->on_event(nullptr);
|
||||
}
|
||||
});
|
||||
this->threads.emplace_back(std::move(manual_updater));
|
||||
}
|
||||
|
||||
bool TorrentModule::on_event(InotifyEvent *event)
|
||||
{
|
||||
if (EXIT_SUCCESS != std::system("pgrep rtorrent >/dev/null")) {
|
||||
get_logger()->debug("[modules::Torrent] rtorrent is not running... ");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (event != nullptr) {
|
||||
log_trace(event->filename);
|
||||
}
|
||||
|
||||
this->torrents.clear();
|
||||
this->read_data_into(this->torrents);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TorrentModule::build(Builder *builder, const std::string& tag)
|
||||
{
|
||||
if (tag != TAG_LABEL && tag != TAG_BAR_PROGRESS)
|
||||
return false;
|
||||
|
||||
for (auto &torrent : this->torrents) {
|
||||
if (tag == TAG_LABEL)
|
||||
builder->node(torrent->label_tokenized);
|
||||
else if (tag == TAG_BAR_PROGRESS)
|
||||
builder->node(this->bar, torrent->perc_downloaded);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<Torrent>> &TorrentModule::read_data_into(std::vector<std::unique_ptr<Torrent>> &container)
|
||||
{
|
||||
try {
|
||||
std::string buf;
|
||||
|
||||
auto command = std::make_unique<Command>("/usr/bin/env\nsh\n-c\n"+ this->pipe_cmd);
|
||||
|
||||
command->exec(false);
|
||||
|
||||
while (!(buf = io::readline(command->get_stdout(PIPE_READ))).empty()) {
|
||||
auto values = string::split(buf, ':');
|
||||
|
||||
if (values.size() != 4) {
|
||||
log_error("Bad value received from torrent script:\n"+ buf);
|
||||
continue;
|
||||
}
|
||||
|
||||
auto torrent = std::make_unique<Torrent>();
|
||||
torrent->title = values[0];
|
||||
torrent->data_total = std::strtoul(values[1].c_str(), 0, 10);
|
||||
torrent->data_downloaded = std::strtoul(values[2].c_str(), 0, 10);
|
||||
torrent->data_remaining = std::strtoul(values[3].c_str(), 0, 10);
|
||||
torrent->perc_downloaded = (float) torrent->data_downloaded / torrent->data_total * 100.0 + 0.5f;
|
||||
|
||||
torrent->label_tokenized = this->label->clone();
|
||||
torrent->label_tokenized->replace_token("%title%", torrent->title);
|
||||
torrent->label_tokenized->replace_token("%percentage%", std::to_string((int) torrent->perc_downloaded)+"%");
|
||||
|
||||
container.emplace_back(std::move(torrent));
|
||||
}
|
||||
|
||||
command->wait();
|
||||
} catch (CommandException &e) {
|
||||
log_error(e.what());
|
||||
} catch (proc::ExecFailure &e) {
|
||||
log_error(e.what());
|
||||
}
|
||||
|
||||
return container;
|
||||
}
|
Loading…
Reference in New Issue
Block a user