Skip to content

Commit

Permalink
Added custom ansi colors for terminals (celebrating v666 youtu.be/26P…
Browse files Browse the repository at this point in the history
…RhCJtgPQ).

████ ███  To request new features or in case this commit breaks something for you,
████ ███  please, create a new github issue with all possible information for me,
▓███▀█▄   but never share your API Keys!
▒▓██ ███
░▒▓█ ███  Signed-off-by: Carles Tubio <ctubio@users.noreply.github.com>
 _________________________________________
/ Hello, WORLD!                           \
|                                         |
\ pssst.. 1.00000000 BTC = 48338.28 EUR.  /
 -----------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
  • Loading branch information
ctubio committed Oct 12, 2021
1 parent 1a3e5d4 commit 8a249a1
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 46 deletions.
12 changes: 7 additions & 5 deletions Makefile
Expand Up @@ -2,7 +2,7 @@ K ?= K.sh
MAJOR = 0
MINOR = 6
PATCH = 6
BUILD = 5
BUILD = 6

OBLIGATORY = DISCLAIMER: This is strict non-violent software: \n$\
if you hurt other living creatures, please stop; \n$\
Expand Down Expand Up @@ -53,13 +53,15 @@ KARGS := -std=c++20 -O3 -pthread \
K-$(KSRC)-client.o \
libuv.a \
)) \
$(addprefix -include , \
$(realpath src/bin/$(KSRC)/$(KSRC).disk.S) \
$(addprefix -include , $(realpath \
$(addprefix src/bin/$(KSRC)/$(KSRC)., \
$(addsuffix .S, disk) \
$(addsuffix .h, ansi) \
) \
$(addprefix src/lib/Krypto.ninja-, \
$(addsuffix .S, disk) \
$(addsuffix .h, lang data apis bots) \
) \
) \
))) \
-D'DEBUG_FRAMEWORK="Krypto.ninja-test.h"' \
-D'DEBUG_SCENARIOS=<$(or \
$(realpath src/bin/$(KSRC)/$(KSRC).test.h), \
Expand Down
2 changes: 0 additions & 2 deletions src/bin/+portfolios/+portfolios.data.h
Expand Up @@ -248,8 +248,6 @@ namespace analpaper {
};
void read_from_gw(const Connectivity &raw) {
greenGateway = raw;
K.log("GW " + K.gateway->exchange, "network state changed to",
string(online() ? "" : "DIS") + "CONNECTED");
broadcast();
K.repaint();
};
Expand Down
2 changes: 1 addition & 1 deletion src/bin/+portfolios/+portfolios.main.h
Expand Up @@ -10,7 +10,7 @@ class Portfolios: public KryptoNinja {
[&](const Connectivity &rawdata) { engine.read(rawdata); },
[&](const Ticker &rawdata) { engine.read(rawdata); },
[&](const Wallet &rawdata) { engine.read(rawdata); },
[&](const unsigned int &tick) { engine.timer_1s(tick); }
[&](const unsigned int &tick ) { engine.timer_1s(tick); }
};
};
private:
Expand Down
27 changes: 27 additions & 0 deletions src/bin/scaling-bot/scaling-bot.ansi.h
@@ -0,0 +1,27 @@
//! \file
//! \brief Color palette override for terminals.
//! \note ANSI color codes to override ANSI_* colors:
//! "0" BLACK "4" BLUE
//! "1" RED "5" MAGENTA
//! "2" GREEN "6" CYAN
//! "3" YELLOW "7" WHITE

//! \def
//! \brief Define color red as magenta.
#define ANSI_RED "5"

//! \def
//! \brief Define color magenta as red.
#define ANSI_MAGENTA "1"

//! \def
//! \brief Define color green as yellow.
#define ANSI_GREEN "3"

//! \def
//! \brief Define color yellow as green.
#define ANSI_YELLOW "2"

//! \def
//! \brief Define color cyan as blue.
#define ANSI_CYAN "4"
5 changes: 1 addition & 4 deletions src/bin/scaling-bot/scaling-bot.data.h
Expand Up @@ -67,7 +67,7 @@ namespace analpaper {
or (order.isPong and order.status == Status::Working)
) K.log("GW " + K.gateway->exchange,
string(order.side == Side::Bid
? ANSI_PUKE_CYAN + (order.isPong?"PONG":"PING") + " TRADE BUY "
? ANSI_HIGH_CYAN + (order.isPong?"PONG":"PING") + " TRADE BUY "
: ANSI_PUKE_MAGENTA + (order.isPong?"PONG":"PING") + " TRADE SELL "
)
+ K.gateway->decimal.amount.str(order.quantity)
Expand Down Expand Up @@ -284,9 +284,6 @@ namespace analpaper {
else if (quote.state == QuoteState::DisabledQuotes)
reason = "DISABLED " + ANSI_PUKE_WHITE
+ "because an admin considered it";
else if (quote.state == QuoteState::Disconnected)
reason = " PAUSED " + ANSI_PUKE_WHITE
+ "because the exchange seems down";
return reason;
};
void calcRawQuotes() override {
Expand Down
6 changes: 3 additions & 3 deletions src/bin/scaling-bot/scaling-bot.main.h
Expand Up @@ -11,8 +11,8 @@ class ScalingBot: public KryptoNinja {
[&](const Wallet &rawdata) { engine.read(rawdata); },
[&](const Levels &rawdata) { engine.read(rawdata); },
[&](const Order &rawdata) { engine.read(rawdata); },
[&](const unsigned int &tick) { engine.timer_1s(tick); },
[&]() { engine.quit(); }
[&](const unsigned int &tick ) { engine.timer_1s(tick); },
[&]( ) { engine.quit(); }
};
arguments = {
{
Expand Down Expand Up @@ -189,7 +189,7 @@ string ScalingBot::terminal() {
+ ANSI_END_LINE;
for (const auto &it : openOrders) {
data += ANSI_PUKE_WHITE + ""
+ (it.side == Side::Bid ? ANSI_PUKE_CYAN + "BID" : ANSI_PUKE_MAGENTA + "ASK")
+ (it.side == Side::Bid ? ANSI_HIGH_CYAN + "BID" : ANSI_PUKE_MAGENTA + "ASK")
+ " > "
+ K.gateway->decimal.amount.str(it.quantity)
+ ' ' + K.gateway->base + " at price "
Expand Down
7 changes: 2 additions & 5 deletions src/bin/stable--bot/stable--bot.data.h
Expand Up @@ -35,7 +35,7 @@ namespace analpaper {
if (!order.orderId.empty() and order.justFilled)
K.log("GW " + K.gateway->exchange,
string(order.side == Side::Bid
? ANSI_PUKE_CYAN + "TRADE BUY "
? ANSI_HIGH_CYAN + "TRADE BUY "
: ANSI_PUKE_MAGENTA + "TRADE SELL "
)
+ K.gateway->decimal.amount.str(order.justFilled)
Expand Down Expand Up @@ -131,9 +131,6 @@ namespace analpaper {
reason = "DISABLED " + ANSI_PUKE_WHITE
+ "because " + (quote.side == Side::Bid ? "--bid-price" : "--ask-price")
+ " was not set";
else if (quote.state == QuoteState::Disconnected)
reason = " PAUSED " + ANSI_PUKE_WHITE
+ "because the exchange seems down";
return reason;
};
void calcRawQuotes() override {
Expand Down Expand Up @@ -274,7 +271,7 @@ namespace analpaper {
{};
void read_from_gw(const Connectivity &raw) {
greenGateway = raw;
if (!(bool)greenGateway)
if (!ready())
quotes.offline();
};
bool ready() const {
Expand Down
4 changes: 2 additions & 2 deletions src/bin/stable--bot/stable--bot.main.h
Expand Up @@ -10,8 +10,8 @@ class StableBot: public KryptoNinja {
[&](const Wallet &rawdata) { engine.read(rawdata); },
[&](const Levels &rawdata) { engine.read(rawdata); },
[&](const Order &rawdata) { engine.read(rawdata); },
[&](const unsigned int &tick) { engine.timer_1s(tick); },
[&]() { engine.quit(); }
[&](const unsigned int &tick ) { engine.timer_1s(tick); },
[&]( ) { engine.quit(); }
};
arguments = {
{
Expand Down
5 changes: 1 addition & 4 deletions src/bin/trading-bot/trading-bot.data.h
Expand Up @@ -1161,7 +1161,7 @@ namespace tribeca {
const bool is_bid = last.side == Side::Bid;
K.log("GW " + K.gateway->exchange,
string(is_bid
? ANSI_PUKE_CYAN + (last.isPong?"PONG":"PING") + " TRADE BUY "
? ANSI_HIGH_CYAN + (last.isPong?"PONG":"PING") + " TRADE BUY "
: ANSI_PUKE_MAGENTA + (last.isPong?"PONG":"PING") + " TRADE SELL "
)
+ K.gateway->decimal.amount.str(filled.quantity) + ' '
Expand Down Expand Up @@ -2032,9 +2032,6 @@ namespace tribeca {
else if (quote.state == QuoteState::DisabledQuotes)
reason = "DISABLED " + ANSI_PUKE_WHITE
+ "because an admin considered it";
else if (quote.state == QuoteState::Disconnected)
reason = " PAUSED " + ANSI_PUKE_WHITE
+ "because the exchange seems down";
return reason;
};
void calcRawQuotes() override {
Expand Down
9 changes: 4 additions & 5 deletions src/bin/trading-bot/trading-bot.main.h
Expand Up @@ -12,8 +12,8 @@ class TradingBot: public KryptoNinja {
[&](const Levels &rawdata) { engine.read(rawdata); },
[&](const Order &rawdata) { engine.read(rawdata); },
[&](const Trade &rawdata) { engine.read(rawdata); },
[&](const unsigned int &tick) { engine.timer_1s(tick); },
[&]() { engine.quit(); }
[&](const unsigned int &tick ) { engine.timer_1s(tick); },
[&]( ) { engine.quit(); }
};
arguments = {
{
Expand All @@ -24,8 +24,7 @@ class TradingBot: public KryptoNinja {
ANSI_NEW_LINE "or use '0' for cross margin; default AMOUNT is '1'"},
{"wallet-limit", "AMOUNT", "0", "set AMOUNT in base currency to limit the balance,"
ANSI_NEW_LINE "otherwise the full available balance can be used"}
},
nullptr
}
};
};
private:
Expand Down Expand Up @@ -120,7 +119,7 @@ string TradingBot::terminal() {
+ ANSI_END_LINE;
for (const auto &it : openOrders) {
data += ANSI_PUKE_WHITE + ""
+ (it.side == Side::Bid ? ANSI_PUKE_CYAN + "BID" : ANSI_PUKE_MAGENTA + "ASK")
+ (it.side == Side::Bid ? ANSI_HIGH_CYAN + "BID" : ANSI_PUKE_MAGENTA + "ASK")
+ " > "
+ K.gateway->decimal.amount.str(it.quantity)
+ ' ' + K.gateway->base + " at price "
Expand Down
9 changes: 8 additions & 1 deletion src/lib/Krypto.ninja-apis.h
Expand Up @@ -280,7 +280,7 @@ namespace ₿ {
/**/ virtual bool async_wallet() { return false; }; // call async wallet data from exchange.
/**/ virtual vector<Wallet> sync_wallet() { return {}; }; // call sync wallet data from exchange.
//EO non-free Gw class member functions from lib build-*/lib/K-*.a (it just redefines all virtual gateway functions above)...
void online(const Connectivity &connectivity) {
virtual void online(const Connectivity &connectivity) {
async.connectivity.try_write(connectivity);
if (!(bool)connectivity)
async.levels.try_write({});
Expand Down Expand Up @@ -444,6 +444,13 @@ namespace ₿ {
virtual json handshake() const = 0;
virtual void pairs(string&) const = 0;
virtual string nonce() const = 0;
void online(const Connectivity &connectivity) override {
print("network state changed to", string((bool)connectivity
? ""
: "DIS"
) + "CONNECTED");
GwExchangeData::online(connectivity);
};
};

class Gw: public GwExchange {
Expand Down
28 changes: 18 additions & 10 deletions src/lib/Krypto.ninja-bots.h
Expand Up @@ -116,7 +116,7 @@ namespace ₿ {
halt(EXIT_FAILURE);
};
static void wtf(const int sig) {
epilogue = ANSI_PUKE_CYAN + "Errrror: " + strsignal(sig) + ' ';
epilogue = ANSI_HIGH_CYAN + "Errrror: " + strsignal(sig) + ' ';
const string mods = changelog();
if (mods.empty()) {
epilogue += "(Three-Headed Monkey found):" ANSI_NEW_LINE + epitaph
Expand Down Expand Up @@ -211,8 +211,7 @@ namespace ₿ {
}
puke_rainbow(stamp() +
prefix + ANSI_PUKE_RED +
" Warrrrning: " + ANSI_HIGH_RED +
reason
" Warrrrning: " + lines(reason)
);
};
void beep() const {
Expand Down Expand Up @@ -269,6 +268,12 @@ namespace ₿ {
<< (epilogue.empty() ? "" : ANSI_NEW_LINE);
};
private:
string lines(string reason) const {
string::size_type n = 0;
while ((n = reason.find(ANSI_NEW_LINE, n + 2)) != string::npos)
reason.insert(n + 2, ANSI_HIGH_RED);
return ANSI_HIGH_RED + reason;
};
void puke_rainbow(const string &rain) const {
string puke = rain + ANSI_PUKE_WHITE
+ '.' + ANSI_RESET
Expand Down Expand Up @@ -317,7 +322,10 @@ namespace ₿ {
};
protected:
using MutableUserArguments = unordered_map<string, variant<string, int, double>>;
pair<vector<Argument>, function<void(MutableUserArguments&)>> arguments;
struct {
vector<Argument> options;
function<void(MutableUserArguments&)> fn = nullptr;
} arguments;
private:
MutableUserArguments args;
public:
Expand Down Expand Up @@ -381,9 +389,9 @@ namespace ₿ {
if (!arg<int>("autobot")) long_options.push_back(
{"autobot", "1", nullptr, "automatically start trading on boot"}
);
for (const Argument &it : arguments.first)
for (const Argument &it : arguments.options)
long_options.push_back(it);
arguments.first.clear();
arguments.options.clear();
for (const Argument &it : (vector<Argument>){
{"heartbeat", "1", nullptr, "print detailed output about most important data"},
{"debug-orders", "1", nullptr, "print detailed output about order states"},
Expand Down Expand Up @@ -477,9 +485,9 @@ namespace ₿ {
if (proactive)
gobeep = arg<int>("beep");
colorful = arg<int>("colors");
if (arguments.second) {
arguments.second(args);
arguments.second = nullptr;
if (arguments.fn) {
arguments.fn(args);
arguments.fn = nullptr;
}
if (arg<int>("naked"))
display = {};
Expand Down Expand Up @@ -1274,7 +1282,7 @@ namespace ₿ {
const string reason = explainState(quote);
if (!reason.empty())
K.log("QP", (quote.side == Side::Bid
? ANSI_PUKE_CYAN + "BID"
? ANSI_HIGH_CYAN + "BID"
: ANSI_PUKE_MAGENTA + "ASK"
) + ANSI_PUKE_WHITE + " quoting", reason);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Krypto.ninja-data.h
Expand Up @@ -1136,7 +1136,7 @@ namespace ₿ {
if (!key.empty())
warn.emplace_back("Ignored .key file because .crt file is missing");
warn.emplace_back("Connected web clients will enjoy unsecure SSL encryption.."
ANSI_NEW_LINE + ANSI_HIGH_RED +
ANSI_NEW_LINE
"(because the private key is visible in the source!). See --help argument to setup your own SSL");
if (!SSL_CTX_use_certificate(ctx,
PEM_read_bio_X509(BIO_new_mem_buf((void*)
Expand Down
23 changes: 20 additions & 3 deletions src/lib/Krypto.ninja-lang.h
Expand Up @@ -94,7 +94,7 @@ using Clock = long long int;
chrono::system_clock::now().time_since_epoch() \
).count()
//! \since Beginning of execution.
static auto
static auto __attribute__ ((unused))
Tbegin = Tstamp;
#define Tspent Tstamp - Tbegin

Expand All @@ -106,16 +106,33 @@ static auto
#endif

//! \def
//! \brief I like turtles.
static bool colorful = true;
//! \brief ANSI escape sequences.
static auto __attribute__ ((unused))
colorful = true;
#ifndef ANSI_BLACK
#define ANSI_BLACK "0"
#endif
#ifndef ANSI_RED
#define ANSI_RED "1"
#endif
#ifndef ANSI_GREEN
#define ANSI_GREEN "2"
#endif
#ifndef ANSI_YELLOW
#define ANSI_YELLOW "3"
#endif
#ifndef ANSI_BLUE
#define ANSI_BLUE "4"
#endif
#ifndef ANSI_MAGENTA
#define ANSI_MAGENTA "5"
#endif
#ifndef ANSI_CYAN
#define ANSI_CYAN "6"
#endif
#ifndef ANSI_WHITE
#define ANSI_WHITE "7"
#endif
#define ANSI_COLOR(a) string(colorful ? a : "")
#define ANSI_COLORS_SIZE(a) (colorful ? a * 7 : 0 )
#define ANSI_SYMBOL_SIZE(a) (a * 2)
Expand Down

0 comments on commit 8a249a1

Please sign in to comment.