Skip to content

Commit

Permalink
Back-compat for clink set.
Browse files Browse the repository at this point in the history
- `clink set old_name` shows values of corresponding new settings.
- `clink set old_name clear` applies defaults to corresponding new settings.
- `clink set old_name value` applies value to corresponding new settings.

Also fixed some slightly incorrect/incomplete migration mappings from
old to new settings.
  • Loading branch information
chrisant996 committed Apr 7, 2021
1 parent 6d2165e commit b7ccf59
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 33 deletions.
60 changes: 47 additions & 13 deletions clink/app/src/loader/set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ static bool print_value(const char* key)
const setting* setting = settings::find(key);
if (setting == nullptr)
{
std::vector<settings::setting_name_value> migrated_settings;
if (migrate_setting(key, nullptr, migrated_settings))
{
bool ret = true;
bool printed = false;
for (const auto& pair : migrated_settings)
{
if (printed)
puts("");
else
printed = true;
ret = print_value(pair.name.c_str()) && ret;
}
return ret;
}

printf("ERROR: Setting '%s' not found.\n", key);
return false;
}
Expand All @@ -124,31 +140,31 @@ static bool print_value(const char* key)
}

//------------------------------------------------------------------------------
static bool set_value(const char* key, char** argv=nullptr, int argc=0)
static bool set_value_impl(const char* key, const char* value)
{
setting* setting = settings::find(key);
if (setting == nullptr)
{
std::vector<settings::setting_name_value> migrated_settings;
if (migrate_setting(key, value, migrated_settings))
{
bool ret = true;
for (const auto& pair : migrated_settings)
ret = set_value_impl(pair.name.c_str(), pair.value.c_str()) && ret;
return ret;
}

printf("ERROR: Setting '%s' not found.\n", key);
return false;
}

str<> value;
if (!argc)
if (!value)
{
setting->set();
}
else
{
for (int c = argc; c--;)
{
if (value.length())
value << " ";
value << *argv;
argv++;
}

if (!setting->set(value.c_str()))
if (!setting->set(value))
{
printf("ERROR: Failed to set value '%s'.\n", key);
return false;
Expand All @@ -157,10 +173,28 @@ static bool set_value(const char* key, char** argv=nullptr, int argc=0)

str<> result;
setting->get_descriptive(result);
printf("Setting '%s' %sset to '%s'\n", key, argc ? "" : "re", result.c_str());
printf("Setting '%s' %sset to '%s'\n", key, value ? "" : "re", result.c_str());
return true;
}

//------------------------------------------------------------------------------
static bool set_value(const char* key, char** argv=nullptr, int argc=0)
{
if (!argc)
return set_value_impl(key, nullptr);

str<> value;
for (int c = argc; c--;)
{
if (value.length())
value << " ";
value << *argv;
argv++;
}

return set_value_impl(key, value.c_str());
}

//------------------------------------------------------------------------------
static void print_help()
{
Expand Down
15 changes: 15 additions & 0 deletions clink/core/include/core/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "str.h"

#include <map>
#include <vector>

class setting;

Expand Down Expand Up @@ -41,6 +42,20 @@ setting* find(const char* name);
bool load(const char* file);
bool save(const char* file);

struct setting_name_value
{
setting_name_value(const char* name, const char* value)
: name(name)
, value(value)
{
}

str_moveable name;
str_moveable value;
};

bool migrate_setting(const char* name, const char* value, std::vector<setting_name_value>& out);

};


Expand Down
77 changes: 57 additions & 20 deletions clink/core/src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static bool set_setting(const char* name, const char* value, const char* comment
}

//------------------------------------------------------------------------------
static bool migrate_setting(const char* name, const char* value)
bool migrate_setting(const char* name, const char* value, std::vector<setting_name_value>& out)
{
// `esc_clears_line` is no longer a setting; bind `\e[27;27~` to whatever
// command is desired in the inputrc file (defaults to `clink-reset-line`).
Expand All @@ -102,29 +102,43 @@ static bool migrate_setting(const char* name, const char* value)
// file and `set LS_COLORS` to set the colors. Also certain `color.*` Clink
// settings.

out.clear();

if (stricmp(name, "exec_match_style") == 0)
{
int x = atoi(value);
set_setting("exec.path", (x>=0) ? "1" : "0");
set_setting("exec.cwd", (x>=1) ? "1" : "0");
set_setting("exec.dirs", (x>=2) ? "1" : "0");
int x = value ? atoi(value) : 2;
out.emplace_back("exec.enable", (x>=0) ? "1" : "0");
if (x >= 0)
{
out.emplace_back("exec.path", (x>=0) ? "1" : "0");
out.emplace_back("exec.cwd", (x>=1) ? "1" : "0");
out.emplace_back("exec.dirs", (x>=2) ? "1" : "0");
}
return true;
}
else if (stricmp(name, "prompt_colour") == 0)
{
int attr = atoi(value);
int attr = value ? atoi(value) : -1;
if (attr < 0)
{
if (!value)
{
out.emplace_back("color.prompt", "");
return true;
}
return false;
}
static const char* const dos_color_names[] = { "bla", "blu", "red", "cya", "gre", "mag", "yel", "whi" };
str<> tmp;
if (attr & 0x08)
tmp << "bri ";
tmp << dos_color_names[attr & 0x07];
return set_setting("color.prompt", tmp.c_str());
out.emplace_back("color.prompt", tmp.c_str());
return true;
}
else if (stricmp(name, "strip_crlf_on_paste") == 0)
{
switch (atoi(value))
switch (value ? atoi(value) : 2)
{
case 0: value = "crlf"; break;
case 1: value = "delete"; break;
Expand All @@ -135,24 +149,39 @@ static bool migrate_setting(const char* name, const char* value)
else if (stricmp(name, "ansi_code_support") == 0)
{
name = "terminal.emulation";
value = atoi(value) ? "auto" : "native";
value = (!value || atoi(value)) ? "auto" : "native";
}
else if (stricmp(name, "history_file_lines") == 0)
{
int x = value ? atoi(value) : 2500;
bool disable = x < 0;
out.emplace_back("history.save", disable ? "0" : "1");
if (!disable)
{
if (x > 0)
out.emplace_back("history.max_lines", value);
else if (x == 0)
out.emplace_back("history.max_lines", "50000"); // Simulate unlimited.
else
out.emplace_back("history.max_lines", "10000");
}
}
else
{
static constexpr struct {
const char* old_name;
const char* new_name;
const char* default_value;
} map_names[] =
{ // OLD NAME NEW NAME
{ "ctrld_exits", "cmd.ctrld_exits" },
{ "space_prefix_match_files", "exec.space_prefix" },
{ "terminate_autoanswer", "cmd.auto_answer" },
{ "history_file_lines", "history.max_lines" },
{ "history_ignore_space", "history.ignore_space" },
{ "history_dupe_mode", "history.dupe_mode" },
{ "history_io", "history.save" },
{ "history_expand_mode", "history.expand_mode" },
{ "use_altgr_substitute", "terminal.use_altgr_substitute" },
{ "ctrld_exits", "cmd.ctrld_exits", "1" },
{ "space_prefix_match_files", "exec.space_prefix", "1" },
{ "terminate_autoanswer", "cmd.auto_answer", "0" },
{ "history_ignore_space", "history.ignore_space", "0" },
{ "history_dupe_mode", "history.dupe_mode", "2" },
{ "history_io", "history.shared", "0" },
{ "history_expand_mode", "history.expand_mode", "4" },
{ "use_altgr_substitute", "terminal.use_altgr_substitute", "1" },
};

const char* old_name = name;
Expand All @@ -162,14 +191,17 @@ static bool migrate_setting(const char* name, const char* value)
if (stricmp(old_name, map_name.old_name) == 0)
{
name = map_name.new_name;
if (!value)
value = map_name.default_value;
break;
}

if (!name)
return false;
}

return set_setting(name, value);
out.emplace_back(name, value);
return true;
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -267,7 +299,12 @@ bool load(const char* file)
// Migrate old setting.
if (migrating)
{
migrate_setting(line_data, value);
std::vector<settings::setting_name_value> migrated_settings;
if (migrate_setting(line_data, value, migrated_settings))
{
for (const auto& pair : migrated_settings)
set_setting(pair.name.c_str(), pair.value.c_str());
}
continue;
}

Expand Down

0 comments on commit b7ccf59

Please sign in to comment.