diff options
author | xsmile | 2017-04-03 12:49:55 +0200 |
---|---|---|
committer | xsmile | 2017-04-03 12:49:55 +0200 |
commit | 1e361329df7798275282b491a58b38495d6e8679 (patch) | |
tree | 43d0bc18a8ac48bd3508a2d2cd5826b04c0ec5bc /command_pyroscope.cc | |
parent | 22b8e44314300a39a782f51c39c1ac3f11c59af6 (diff) | |
download | aur-1e361329df7798275282b491a58b38495d6e8679.tar.gz |
update
Diffstat (limited to 'command_pyroscope.cc')
-rw-r--r-- | command_pyroscope.cc | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/command_pyroscope.cc b/command_pyroscope.cc index 182456e35eb7..496017f21ec2 100644 --- a/command_pyroscope.cc +++ b/command_pyroscope.cc @@ -471,6 +471,80 @@ torrent::Object cmd_log_messages(const torrent::Object::string_type& arg) { } +torrent::Object +d_multicall_filtered(const torrent::Object::list_type& args) { + if (args.size() < 2) + throw torrent::input_error("d.multicall.filtered requires at least 2 arguments."); + torrent::Object::list_const_iterator arg = args.begin(); + + // Find the given view + core::ViewManager* viewManager = control->view_manager(); + core::ViewManager::iterator viewItr; + + if (!arg->as_string().empty()) + viewItr = viewManager->find(arg->as_string()); + else + viewItr = viewManager->find("default"); + + if (viewItr == viewManager->end()) + throw torrent::input_error("Could not find view."); + + // Make a filtered copy of the current item list + core::View::base_type dlist; + (*viewItr)->filter_by(*++arg, dlist); + + // Generate result by iterating over all items + torrent::Object resultRaw = torrent::Object::create_list(); + torrent::Object::list_type& result = resultRaw.as_list(); + ++arg; // skip to first command + + for (core::View::iterator item = dlist.begin(); item != dlist.end(); ++item) { + // Add empty row to result + torrent::Object::list_type& row = result.insert(result.end(), torrent::Object::create_list())->as_list(); + + // Call the provided commands and assemble their results + for (torrent::Object::list_const_iterator command = arg; command != args.end(); command++) { + const std::string& cmdstr = command->as_string(); + row.push_back(rpc::parse_command(rpc::make_target(*item), cmdstr.c_str(), cmdstr.c_str() + cmdstr.size()).first); + } + } + + return resultRaw; +} + + +torrent::Object::value_type apply_string_contains(bool ignore_case, const torrent::Object::list_type& args) { + if (args.size() < 2) { + throw torrent::input_error("string.contains[_i] takes at least two arguments!"); + } + + torrent::Object::list_const_iterator itr = args.begin(); + std::string text = itr->as_string(); + if (ignore_case) + std::transform(text.begin(), text.end(), text.begin(), ::tolower); + + for (++itr; itr != args.end(); ++itr) { + std::string substr = itr->as_string(); + if (ignore_case) + std::transform(substr.begin(), substr.end(), substr.begin(), ::tolower); + if (substr.empty() || text.find(substr) != std::string::npos) + return 1; + } + + return 0; +} + + +torrent::Object cmd_string_contains(rpc::target_type target, const torrent::Object::list_type& args) { + return apply_string_contains(false, args); +} + +// XXX: Will NOT work correctly for non-ASCII strings! +torrent::Object cmd_string_contains_i(rpc::target_type target, const torrent::Object::list_type& args) { + return apply_string_contains(true, args); +} + + // Backports from 0.9.2 #if (API_VERSION < 3) template <typename InputIterator, typename OutputIterator> OutputIterator @@ -543,8 +617,11 @@ void initialize_command_pyroscope() { CMD2_ANY_STRING("system.env", _cxxstd_::bind(&cmd_system_env, _cxxstd_::placeholders::_2)); CMD2_ANY("ui.current_view", _cxxstd_::bind(&cmd_ui_current_view)); CMD2_ANY_LIST("system.random", &apply_random); + CMD2_ANY_LIST("d.multicall.filtered", _cxxstd_::bind(&d_multicall_filtered, _cxxstd_::placeholders::_2)); #endif + CMD2_ANY_LIST("string.contains", &cmd_string_contains); + CMD2_ANY_LIST("string.contains_i", &cmd_string_contains_i); CMD2_ANY_LIST("compare", &apply_compare); CMD2_ANY("ui.bind_key", &apply_ui_bind_key); CMD2_VAR_VALUE("ui.bind_key.verbose", 1); |