11
11
#include < chainparamsbase.h>
12
12
#include < clientversion.h>
13
13
#include < compat.h>
14
+ #include < policy/feerate.h>
14
15
#include < rpc/client.h>
15
16
#include < rpc/mining.h>
16
17
#include < rpc/protocol.h>
31
32
#include < string>
32
33
#include < tuple>
33
34
35
+ #ifndef WIN32
36
+ #include < unistd.h>
37
+ #endif
38
+
34
39
#include < event2/buffer.h>
35
40
#include < event2/keyvalq_struct.h>
36
41
#include < support/events.h>
@@ -51,6 +56,9 @@ static constexpr int8_t UNKNOWN_NETWORK{-1};
51
56
/* * Default number of blocks to generate for RPC generatetoaddress. */
52
57
static const std::string DEFAULT_NBLOCKS = " 1" ;
53
58
59
+ /* * Default -color setting. */
60
+ static const std::string DEFAULT_COLOR_SETTING{" auto" };
61
+
54
62
static void SetupCliArgs (ArgsManager& argsman)
55
63
{
56
64
SetupHelpOptions (argsman);
@@ -66,6 +74,8 @@ static void SetupCliArgs(ArgsManager& argsman)
66
74
argsman.AddArg (" -addrinfo" , " Get the number of addresses known to the node, per network and total." , ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
67
75
argsman.AddArg (" -getinfo" , " Get general information from the remote server. Note that unlike server-side RPC calls, the results of -getinfo is the result of multiple non-atomic requests. Some entries in the result may represent results from different states (e.g. wallet balance may be as of a different block from the chain state reported)" , ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
68
76
argsman.AddArg (" -netinfo" , " Get network peer connection information from the remote server. An optional integer argument from 0 to 4 can be passed for different peers listings (default: 0). Pass \" help\" for detailed help documentation." , ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
77
+
78
+ argsman.AddArg (" -color=<when>" , strprintf (" Color setting for CLI output (default: %s). Valid values: always, auto (add color codes when standard output is connected to a terminal and OS is not WIN32), never." , DEFAULT_COLOR_SETTING), ArgsManager::ALLOW_STRING, OptionsCategory::OPTIONS);
69
79
argsman.AddArg (" -named" , strprintf (" Pass named instead of positional arguments (default: %s)" , DEFAULT_NAMED), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
70
80
argsman.AddArg (" -rpcclienttimeout=<n>" , strprintf (" Timeout in seconds during HTTP requests, or 0 for no timeout. (default: %d)" , DEFAULT_HTTP_CLIENT_TIMEOUT), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
71
81
argsman.AddArg (" -rpcconnect=<ip>" , strprintf (" Send commands to node running on <ip> (default: %s)" , DEFAULT_RPCCONNECT), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
@@ -355,7 +365,9 @@ class GetinfoRequestHandler: public BaseRequestHandler
355
365
result.pushKV (" chain" , UniValue (batch[ID_BLOCKCHAININFO][" result" ][" chain" ]));
356
366
if (!batch[ID_WALLETINFO][" result" ].isNull ()) {
357
367
result.pushKV (" coinjoin_balance" , batch[ID_WALLETINFO][" result" ][" coinjoin_balance" ]);
368
+ result.pushKV (" has_wallet" , true );
358
369
result.pushKV (" keypoolsize" , batch[ID_WALLETINFO][" result" ][" keypoolsize" ]);
370
+ result.pushKV (" walletname" , batch[ID_WALLETINFO][" result" ][" walletname" ]);
359
371
if (!batch[ID_WALLETINFO][" result" ][" unlocked_until" ].isNull ()) {
360
372
result.pushKV (" unlocked_until" , batch[ID_WALLETINFO][" result" ][" unlocked_until" ]);
361
373
}
@@ -882,6 +894,105 @@ static void GetWalletBalances(UniValue& result)
882
894
result.pushKV (" balances" , balances);
883
895
}
884
896
897
+ /* *
898
+ * ParseGetInfoResult takes in -getinfo result in UniValue object and parses it
899
+ * into a user friendly UniValue string to be printed on the console.
900
+ * @param[out] result Reference to UniValue result containing the -getinfo output.
901
+ */
902
+ static void ParseGetInfoResult (UniValue& result)
903
+ {
904
+ if (!find_value (result, " error" ).isNull ()) return ;
905
+
906
+ std::string RESET, GREEN, BLUE, YELLOW, MAGENTA, CYAN;
907
+ bool should_colorize = false ;
908
+
909
+ #ifndef WIN32
910
+ if (isatty (fileno (stdout))) {
911
+ // By default, only print colored text if OS is not WIN32 and stdout is connected to a terminal.
912
+ should_colorize = true ;
913
+ }
914
+ #endif
915
+
916
+ if (gArgs .IsArgSet (" -color" )) {
917
+ const std::string color{gArgs .GetArg (" -color" , DEFAULT_COLOR_SETTING)};
918
+ if (color == " always" ) {
919
+ should_colorize = true ;
920
+ } else if (color == " never" ) {
921
+ should_colorize = false ;
922
+ } else if (color != " auto" ) {
923
+ throw std::runtime_error (" Invalid value for -color option. Valid values: always, auto, never." );
924
+ }
925
+ }
926
+
927
+ if (should_colorize) {
928
+ RESET = " \x1B [0m" ;
929
+ GREEN = " \x1B [32m" ;
930
+ BLUE = " \x1B [34m" ;
931
+ YELLOW = " \x1B [33m" ;
932
+ MAGENTA = " \x1B [35m" ;
933
+ CYAN = " \x1B [36m" ;
934
+ }
935
+
936
+ std::string result_string = strprintf (" %sChain: %s%s\n " , BLUE, result[" chain" ].getValStr (), RESET);
937
+ result_string += strprintf (" Blocks: %s\n " , result[" blocks" ].getValStr ());
938
+ result_string += strprintf (" Headers: %s\n " , result[" headers" ].getValStr ());
939
+ result_string += strprintf (" Verification progress: %.4f%%\n " , result[" verificationprogress" ].get_real () * 100 );
940
+ result_string += strprintf (" Difficulty: %s\n\n " , result[" difficulty" ].getValStr ());
941
+
942
+ result_string += strprintf (
943
+ " %sNetwork: in %s, out %s, total %s, mn_in %s, mn_out %s, mn_total %s%s\n " ,
944
+ GREEN,
945
+ result[" connections" ][" in" ].getValStr (),
946
+ result[" connections" ][" out" ].getValStr (),
947
+ result[" connections" ][" total" ].getValStr (),
948
+ result[" connections" ][" mn_in" ].getValStr (),
949
+ result[" connections" ][" mn_out" ].getValStr (),
950
+ result[" connections" ][" mn_total" ].getValStr (),
951
+ RESET);
952
+ result_string += strprintf (" Version: %s\n " , result[" version" ].getValStr ());
953
+ result_string += strprintf (" Time offset (s): %s\n " , result[" timeoffset" ].getValStr ());
954
+ const std::string proxy = result[" proxy" ].getValStr ();
955
+ result_string += strprintf (" Proxy: %s\n " , proxy.empty () ? " N/A" : proxy);
956
+ result_string += strprintf (" Min tx relay fee rate (%s/kB): %s\n\n " , CURRENCY_UNIT, result[" relayfee" ].getValStr ());
957
+
958
+ if (!result[" has_wallet" ].isNull ()) {
959
+ const std::string walletname = result[" walletname" ].getValStr ();
960
+ result_string += strprintf (" %sWallet: %s%s\n " , MAGENTA, walletname.empty () ? " \"\" " : walletname, RESET);
961
+
962
+ result_string += strprintf (" %sCoinJoin balance:%s %s\n " , CYAN, RESET, result[" coinjoin_balance" ].getValStr ());
963
+
964
+ result_string += strprintf (" Keypool size: %s\n " , result[" keypoolsize" ].getValStr ());
965
+ if (!result[" unlocked_until" ].isNull ()) {
966
+ result_string += strprintf (" Unlocked until: %s\n " , result[" unlocked_until" ].getValStr ());
967
+ }
968
+ result_string += strprintf (" Transaction fee rate (-paytxfee) (%s/kB): %s\n\n " , CURRENCY_UNIT, result[" paytxfee" ].getValStr ());
969
+ }
970
+ if (!result[" balance" ].isNull ()) {
971
+ result_string += strprintf (" %sBalance:%s %s\n\n " , CYAN, RESET, result[" balance" ].getValStr ());
972
+ }
973
+
974
+ if (!result[" balances" ].isNull ()) {
975
+ result_string += strprintf (" %sBalances%s\n " , CYAN, RESET);
976
+
977
+ size_t max_balance_length{10 };
978
+
979
+ for (const std::string& wallet : result[" balances" ].getKeys ()) {
980
+ max_balance_length = std::max (result[" balances" ][wallet].getValStr ().length (), max_balance_length);
981
+ }
982
+
983
+ for (const std::string& wallet : result[" balances" ].getKeys ()) {
984
+ result_string += strprintf (" %*s %s\n " ,
985
+ max_balance_length,
986
+ result[" balances" ][wallet].getValStr (),
987
+ wallet.empty () ? " \"\" " : wallet);
988
+ }
989
+ result_string += " \n " ;
990
+ }
991
+
992
+ result_string += strprintf (" %sWarnings:%s %s" , YELLOW, RESET, result[" warnings" ].getValStr ());
993
+ result.setStr (result_string);
994
+ }
995
+
885
996
/* *
886
997
* Call RPC getnewaddress.
887
998
* @returns getnewaddress response as a UniValue object.
@@ -1003,9 +1114,13 @@ static int CommandLineRPC(int argc, char *argv[])
1003
1114
UniValue result = find_value (reply, " result" );
1004
1115
const UniValue& error = find_value (reply, " error" );
1005
1116
if (error.isNull ()) {
1006
- if (gArgs .IsArgSet (" -getinfo" ) && !gArgs .IsArgSet (" -rpcwallet" )) {
1007
- GetWalletBalances (result); // fetch multiwallet balances and append to result
1117
+ if (gArgs .GetBoolArg (" -getinfo" , false )) {
1118
+ if (!gArgs .IsArgSet (" -rpcwallet" )) {
1119
+ GetWalletBalances (result); // fetch multiwallet balances and append to result
1120
+ }
1121
+ ParseGetInfoResult (result);
1008
1122
}
1123
+
1009
1124
ParseResult (result, strPrint);
1010
1125
} else {
1011
1126
ParseError (error, strPrint, nRet);
0 commit comments