Cleanup JsonUI and add support for a resize method

This commit is contained in:
Maxime Coste 2016-03-06 14:23:56 +00:00
parent 433940485f
commit b832a61193
2 changed files with 28 additions and 18 deletions

View File

@ -276,14 +276,11 @@ parse_json(const char* pos, const char* end)
return {}; return {};
if (*pos == ',') if (*pos == ',')
{
++pos; ++pos;
continue; else if (*pos == ']')
}
if (*pos == ']')
return Result{std::move(array), pos+1}; return Result{std::move(array), pos+1};
else else
throw runtime_error("unable to parse array, expected , or ]"); throw runtime_error("unable to parse array, expected ',' or ']'");
} }
} }
if (*pos == '{') if (*pos == '{')
@ -312,17 +309,14 @@ parse_json(const char* pos, const char* end)
return {}; return {};
if (*pos == ',') if (*pos == ',')
{
++pos; ++pos;
continue; else if (*pos == '}')
}
if (*pos == '}')
return Result{std::move(object), pos+1}; return Result{std::move(object), pos+1};
else else
throw runtime_error("unable to parse object, expected , or }"); throw runtime_error("unable to parse object, expected ',' or '}'");
} }
} }
return {}; throw runtime_error("Could not parse json");
} }
void JsonUI::eval_json(const Value& json) void JsonUI::eval_json(const Value& json)
@ -350,6 +344,15 @@ void JsonUI::eval_json(const Value& json)
m_pending_keys.push_back(key); m_pending_keys.push_back(key);
} }
} }
else if (method == "resize")
{
if (params.size() != 2)
throw runtime_error("resize expects 2 parameters");
CharCoord dim{params[0].as<int>(), params[1].as<int>()};
m_dimensions = dim;
m_pending_keys.push_back(resize(dim));
}
else else
throw runtime_error("unknown method"); throw runtime_error("unknown method");
} }
@ -377,18 +380,26 @@ void JsonUI::parse_requests(EventMode mode)
if (size == -1 or size == 0) if (size == -1 or size == 0)
break; break;
m_incoming_text += StringView{buf, buf + size}; m_requests += StringView{buf, buf + size};
} }
if (not m_incoming_text.empty()) if (not m_requests.empty())
{ {
Value json; Value json;
const char* pos; const char* pos;
std::tie(json, pos) = parse_json(m_incoming_text.begin(), m_incoming_text.end()); std::tie(json, pos) = parse_json(m_requests.begin(), m_requests.end());
if (json) if (json)
{ {
eval_json(json); try
m_incoming_text = String{pos, m_incoming_text.end()}; {
eval_json(json);
}
catch (runtime_error& error)
{
write_stderr(format("error while executing request '{}': '{}'",
StringView{m_requests.begin(), pos}, error.what()));
}
m_requests = String{pos, m_requests.end()};
} }
} }
@ -403,5 +414,4 @@ UnitTest test_json_parser{[]()
kak_assert(value); kak_assert(value);
}}; }};
} }

View File

@ -55,7 +55,7 @@ private:
FDWatcher m_stdin_watcher; FDWatcher m_stdin_watcher;
Vector<Key> m_pending_keys; Vector<Key> m_pending_keys;
CharCoord m_dimensions; CharCoord m_dimensions;
String m_incoming_text; String m_requests;
}; };
} }