tg_cli: autosplit.

This commit is contained in:
levlam 2022-01-24 17:40:16 +03:00
parent 6001485409
commit 6860a7855b
1 changed files with 31 additions and 32 deletions

View File

@ -441,6 +441,14 @@ class CliClient final : public Actor {
return ' ';
}
static vector<Slice> autosplit(Slice str) {
return full_split(trim(str), get_delimiter(str));
}
static vector<string> autosplit_str(Slice str) {
return transform(autosplit(str), [](Slice slice) { return slice.str(); });
}
int64 as_chat_id(Slice str) const {
str = trim(str);
if (str == "me") {
@ -473,8 +481,7 @@ class CliClient final : public Actor {
}
static vector<int32> as_chat_filter_ids(Slice chat_filter_ids) {
return transform(full_split(trim(chat_filter_ids), get_delimiter(chat_filter_ids)),
[](Slice str) { return as_chat_filter_id(str); });
return transform(autosplit(chat_filter_ids), as_chat_filter_id);
}
static td_api::object_ptr<td_api::ChatList> as_chat_list(string chat_list) {
@ -488,8 +495,7 @@ class CliClient final : public Actor {
}
vector<int64> as_chat_ids(Slice chat_ids) const {
return transform(full_split(trim(chat_ids), get_delimiter(chat_ids)),
[this](Slice str) { return as_chat_id(str); });
return transform(autosplit(chat_ids), [this](Slice str) { return as_chat_id(str); });
}
static int64 as_message_id(Slice str) {
@ -501,7 +507,7 @@ class CliClient final : public Actor {
}
static vector<int64> as_message_ids(Slice message_ids) {
return transform(full_split(trim(message_ids), get_delimiter(message_ids)), as_message_id);
return transform(autosplit(message_ids), as_message_id);
}
static int64 as_message_thread_id(Slice str) {
@ -548,7 +554,7 @@ class CliClient final : public Actor {
}
vector<int64> as_user_ids(Slice user_ids) const {
return transform(full_split(user_ids, get_delimiter(user_ids)), [this](Slice str) { return as_user_id(str); });
return transform(autosplit(user_ids), [this](Slice str) { return as_user_id(str); });
}
static int64 as_basic_group_id(Slice str) {
@ -612,10 +618,10 @@ class CliClient final : public Actor {
expected_size);
}
static td_api::object_ptr<td_api::InputFile> as_input_file(string str) {
static td_api::object_ptr<td_api::InputFile> as_input_file(Slice str) {
str = trim(str);
if ((str.size() >= 20 && is_base64url(str)) || begins_with(str, "http")) {
return as_remote_file(str);
return as_remote_file(str.str());
}
auto r_file_id = to_integer_safe<int32>(str);
if (r_file_id.is_ok()) {
@ -623,9 +629,9 @@ class CliClient final : public Actor {
}
if (str.find(';') < str.size()) {
auto res = split(str, ';');
return as_generated_file(res.first, res.second);
return as_generated_file(res.first.str(), res.second.str());
}
return as_local_file(str);
return as_local_file(str.str());
}
static td_api::object_ptr<td_api::inputThumbnail> as_input_thumbnail(td_api::object_ptr<td_api::InputFile> input_file,
@ -670,8 +676,8 @@ class CliClient final : public Actor {
}
template <class T>
static vector<T> to_integers(Slice ids_string) {
return transform(transform(full_split(ids_string, get_delimiter(ids_string)), trim<Slice>), to_integer<T>);
static vector<T> to_integers(Slice integers) {
return transform(transform(autosplit(integers), trim<Slice>), to_integer<T>);
}
static void get_args(string &args, string &arg) {
@ -1476,7 +1482,7 @@ class CliClient final : public Actor {
}
static auto as_passport_element_types(Slice types) {
return transform(full_split(types, get_delimiter(types)), [](Slice str) { return as_passport_element_type(str); });
return transform(autosplit(types), as_passport_element_type);
}
static td_api::object_ptr<td_api::InputPassportElement> as_input_passport_element(const string &passport_element_type,
@ -1485,7 +1491,7 @@ class CliClient final : public Actor {
vector<td_api::object_ptr<td_api::InputFile>> input_files;
td_api::object_ptr<td_api::InputFile> selfie;
if (!arg.empty()) {
auto files = full_split(arg);
auto files = autosplit(arg);
CHECK(!files.empty());
if (with_selfie) {
selfie = as_input_file(files.back());
@ -2206,7 +2212,7 @@ class CliClient final : public Actor {
string language_code;
string keys;
get_args(args, language_code, keys);
send_request(td_api::make_object<td_api::getLanguagePackStrings>(language_code, full_split(keys)));
send_request(td_api::make_object<td_api::getLanguagePackStrings>(language_code, autosplit_str(keys)));
} else if (op == "glpss") {
string language_database_path;
string language_pack;
@ -2566,14 +2572,13 @@ class CliClient final : public Actor {
string stickers;
get_args(args, title, name, stickers);
auto input_stickers =
transform(full_split(stickers, get_delimiter(stickers)),
[op](const string &sticker) -> td_api::object_ptr<td_api::InputSticker> {
if (op == "cnssa") {
return td_api::make_object<td_api::inputStickerAnimated>(as_input_file(sticker), "😀");
} else {
return td_api::make_object<td_api::inputStickerStatic>(as_input_file(sticker), "😀", nullptr);
}
});
transform(autosplit(stickers), [op](Slice sticker) -> td_api::object_ptr<td_api::InputSticker> {
if (op == "cnssa") {
return td_api::make_object<td_api::inputStickerAnimated>(as_input_file(sticker), "😀");
} else {
return td_api::make_object<td_api::inputStickerStatic>(as_input_file(sticker), "😀", nullptr);
}
});
send_request(td_api::make_object<td_api::createNewStickerSet>(my_id_, title, name, false,
std::move(input_stickers), "tg_cli"));
} else if (op == "sss") {
@ -3292,12 +3297,7 @@ class CliClient final : public Actor {
send_request(
td_api::make_object<td_api::toggleChatDefaultDisableNotification>(chat_id, default_disable_notification));
} else if (op == "spchats" || op == "spchatsa" || begins_with(op, "spchats-")) {
vector<string> chat_ids_str = full_split(args, ' ');
vector<int64> chat_ids;
for (auto &chat_id_str : chat_ids_str) {
chat_ids.push_back(as_chat_id(chat_id_str));
}
send_request(td_api::make_object<td_api::setPinnedChats>(as_chat_list(op), std::move(chat_ids)));
send_request(td_api::make_object<td_api::setPinnedChats>(as_chat_list(op), as_chat_ids(args)));
} else if (op == "sca") {
ChatId chat_id;
string message_thread_id;
@ -3710,7 +3710,7 @@ class CliClient final : public Actor {
ChatId chat_id;
string question;
get_args(args, chat_id, question, args);
auto options = full_split(args);
auto options = autosplit_str(args);
td_api::object_ptr<td_api::PollType> poll_type;
if (op == "squiz") {
poll_type = td_api::make_object<td_api::pollTypeQuiz>(narrow_cast<int32>(options.size() - 1),
@ -4191,8 +4191,7 @@ class CliClient final : public Actor {
ChatId chat_id;
string available_reactions;
get_args(args, chat_id, available_reactions);
send_request(
td_api::make_object<td_api::setChatAvailableReactions>(chat_id, full_split(available_reactions, ' ')));
send_request(td_api::make_object<td_api::setChatAvailableReactions>(chat_id, autosplit_str(available_reactions)));
} else if (op == "scd") {
ChatId chat_id;
string description;