Simplify code to format “«unit» / Selection” bundle/menu items

This commit is contained in:
Allan Odgaard
2014-11-20 22:20:32 +01:00
parent fd07d7cf3b
commit eedf8122ea
2 changed files with 4 additions and 40 deletions

View File

@@ -1560,25 +1560,6 @@ doScroll:
// = Bundle Items =
// ================
// FIXME copy/paste from bundles/src/query.cc
static std::string format_bundle_item_title (std::string title, bool hasSelection)
{
static std::string const kSelectionSubString = " / Selection";
std::string::size_type pos = title.find(kSelectionSubString);
if(pos == 0 || pos == std::string::npos)
return title;
if(hasSelection)
{
std::string::size_type from = title.rfind(' ', pos - 1);
if(from == std::string::npos)
return title.erase(0, pos + 3);
return title.erase(from + 1, pos + 3 - from - 1);
}
return title.erase(pos, kSelectionSubString.size());
}
- (std::map<std::string, std::string>)variablesForBundleItem:(bundles::item_ptr const&)item
{
std::map<std::string, std::string> res = oak::basic_environment();
@@ -1624,7 +1605,7 @@ static std::string format_bundle_item_title (std::string title, bool hasSelectio
[self recordSelector:@selector(executeCommandWithOptions:) withArgument:ns::to_dictionary(item->plist())];
auto command = parse_command(item);
command.name = format_bundle_item_title(command.name, self.hasSelection);
command.name = name_with_selection(item, self.hasSelection);
if([self.delegate respondsToSelector:@selector(bundleItemPreExec:completionHandler:)])
{
[self.delegate bundleItemPreExec:command.pre_exec completionHandler:^(BOOL success){
@@ -2643,7 +2624,7 @@ static char const* kOakMenuItemTitle = "OakMenuItemTitle";
{
NSString* title = objc_getAssociatedObject(aMenuItem, kOakMenuItemTitle) ?: aMenuItem.title;
objc_setAssociatedObject(aMenuItem, kOakMenuItemTitle, title, OBJC_ASSOCIATION_RETAIN);
[aMenuItem updateTitle:[NSString stringWithCxxString:format_bundle_item_title(to_s(title), [self hasSelection])]];
[aMenuItem updateTitle:[NSString stringWithCxxString:format_string::replace(to_s(title), "\\b(\\w+) / (Selection)\\b", [self hasSelection] ? "$2" : "$1")]];
if([aMenuItem action] == @selector(cut:))
[aMenuItem setTitle:@"Cut"];

View File

@@ -2,6 +2,7 @@
#include <text/ctype.h>
#include <text/parse.h>
#include <text/trim.h>
#include <regexp/format_string.h>
#include <oak/callbacks.h>
namespace bundles
@@ -296,27 +297,9 @@ namespace bundles
return res;
}
static std::string format_bundle_item_title (std::string title, bool hasSelection)
{
static std::string const kSelectionSubString = " / Selection";
std::string::size_type pos = title.find(kSelectionSubString);
if(pos == 0 || pos == std::string::npos)
return title;
if(hasSelection)
{
std::string::size_type from = title.rfind(' ', pos - 1);
if(from == std::string::npos)
return title.erase(0, pos + 3);
return title.erase(from + 1, pos + 3 - from - 1);
}
return title.erase(pos, kSelectionSubString.size());
}
std::string name_with_selection (item_ptr const& item, bool hasSelection)
{
return format_bundle_item_title(item->name(), hasSelection);
return format_string::replace(item->name(), "\\b(\\w+) / (Selection)\\b", hasSelection ? "$2" : "$1");
}
std::string menu_path (item_ptr item)