plasma-show-stdout
A KDE Plasma 6 widget that displays stdout of shell scripts to the panel
Loading...
Searching...
No Matches
scriptModules.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) <2026> Anthony J. Greenberg
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7 *
8 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9 *
10 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11 *
12 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
13 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
14 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
15 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
16 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
17 * THE POSSIBILITY OF SUCH DAMAGE.
18 */
19
21
28
29#pragma once
30
31#include <string>
32#include <condition_variable>
33#include <chrono>
34#include <functional>
35#include <memory>
36#include <mutex>
37#include <filesystem>
38
39namespace PSSspace {
52 using MessageTranslator = std::function<std::string(const std::string &messageId, const std::string &argument)>;
74 [[nodiscard]] std::string translateMessage(const std::string &messageId, const std::string &argument);
82 [[nodiscard]] std::string runScript(const std::filesystem::path &script);
97 void truncateUtf8(std::string &text, size_t maxCodepoints);
103 public:
105 TimedModule() = default;
118 const std::chrono::duration<uint32_t> &executionInterval,
119 std::shared_ptr<std::condition_variable> stopSignal,
120 std::shared_ptr<std::mutex> mutex,
121 std::shared_ptr<bool> stop,
122 std::unique_ptr<std::condition_variable> signalOnChange,
123 const std::filesystem::path &script,
124 const size_t &outputLengthLimit,
125 std::unique_ptr<std::string> outputTarget
126 ) : refreshInterval_{executionInterval},
127 stopSignal_{std::move(stopSignal)},
128 mutex_{std::move(mutex)},
129 stop_{std::move(stop)},
130 signalToMain_{std::move(signalOnChange)},
131 script_{script},
132 outputLengthLimit_{outputLengthLimit},
133 outputString_{std::move(outputTarget)} {};
134
138 TimedModule(TimedModule &&toMove) noexcept = default;
144 TimedModule& operator=(TimedModule &&toMove) noexcept = default;
146 ~TimedModule() = default;
151 void operator()() const;
152 private:
154 std::chrono::duration<uint32_t> refreshInterval_;
156 std::shared_ptr<std::condition_variable> stopSignal_;
158 std::shared_ptr<std::mutex> mutex_;
160 std::shared_ptr<bool> stop_;
162 std::unique_ptr<std::condition_variable> signalToMain_;
164 std::filesystem::path script_;
166 size_t outputLengthLimit_ = 0;
168 std::unique_ptr<std::string> outputString_;
169 };
170
175 public:
177 SignalModule() = default;
190 std::unique_ptr<std::condition_variable> signalToExecute,
191 std::shared_ptr<std::mutex> mutex,
192 std::shared_ptr<bool> execute,
193 std::shared_ptr<bool> stop,
194 std::unique_ptr<std::condition_variable> signalOnChange,
195 const std::filesystem::path &script,
196 const size_t &outputLengthLimit,
197 std::unique_ptr<std::string> &outputTarget
198 ) : executionSignal_{std::move(signalToExecute)},
199 mutex_{std::move(mutex)},
200 execute_{std::move(execute)},
201 stop_{std::move(stop)},
202 signalToMain_{std::move(signalOnChange)},
203 script_{script},
204 outputLengthLimit_{outputLengthLimit},
205 outputString_{std::move(outputTarget)} {};
206
210 SignalModule(SignalModule &&toMove) noexcept = default;
216 SignalModule& operator=(SignalModule &&toMove) noexcept = default;
218 ~SignalModule() = default;
223 void operator()() const;
224 private:
226 std::unique_ptr<std::condition_variable> executionSignal_;
228 std::shared_ptr<std::mutex> mutex_;
230 std::shared_ptr<bool> execute_;
232 std::shared_ptr<bool> stop_;
234 std::unique_ptr<std::condition_variable> signalToMain_;
236 std::filesystem::path script_;
238 size_t outputLengthLimit_ = 0;
240 std::unique_ptr<std::string> outputString_;
241 };
242}
SignalModule(SignalModule &&toMove) noexcept=default
Move constructor.
SignalModule & operator=(SignalModule &&toMove) noexcept=default
Move assignment operator.
SignalModule()=default
Default constructor.
void operator()() const
SignalModule(std::unique_ptr< std::condition_variable > signalToExecute, std::shared_ptr< std::mutex > mutex, std::shared_ptr< bool > execute, std::shared_ptr< bool > stop, std::unique_ptr< std::condition_variable > signalOnChange, const std::filesystem::path &script, const size_t &outputLengthLimit, std::unique_ptr< std::string > &outputTarget)
Constructor with data.
Definition scriptModules.hpp:189
~SignalModule()=default
Destructor.
TimedModule()=default
Default constructor.
~TimedModule()=default
Destructor.
TimedModule(const std::chrono::duration< uint32_t > &executionInterval, std::shared_ptr< std::condition_variable > stopSignal, std::shared_ptr< std::mutex > mutex, std::shared_ptr< bool > stop, std::unique_ptr< std::condition_variable > signalOnChange, const std::filesystem::path &script, const size_t &outputLengthLimit, std::unique_ptr< std::string > outputTarget)
Constructor with data.
Definition scriptModules.hpp:117
TimedModule(TimedModule &&toMove) noexcept=default
Move constructor.
void operator()() const
TimedModule & operator=(TimedModule &&toMove) noexcept=default
Move assignment operator.
std::string translateMessage(const std::string &messageId, const std::string &argument)
Translate a user-facing backend message.
std::function< std::string(const std::string &messageId, const std::string &argument)> MessageTranslator
User-facing message translator hook.
Definition scriptModules.hpp:52
void setMessageTranslator(MessageTranslator translator)
Install the user-facing message translator.
void truncateUtf8(std::string &text, size_t maxCodepoints)
Truncate a UTF-8 string to a codepoint limit, in place.
std::string runScript(const std::filesystem::path &script)
Run a script once.