From b832a61193bf2d5599babb9b925d4027c531f54b Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 6 Mar 2016 14:23:56 +0000 Subject: [PATCH] Cleanup JsonUI and add support for a resize method --- src/json_ui.cc | 44 +++++++++++++++++++++++++++----------------- src/json_ui.hh | 2 +- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/json_ui.cc b/src/json_ui.cc index 67b21bda..913113de 100644 --- a/src/json_ui.cc +++ b/src/json_ui.cc @@ -276,14 +276,11 @@ parse_json(const char* pos, const char* end) return {}; if (*pos == ',') - { ++pos; - continue; - } - if (*pos == ']') + else if (*pos == ']') return Result{std::move(array), pos+1}; else - throw runtime_error("unable to parse array, expected , or ]"); + throw runtime_error("unable to parse array, expected ',' or ']'"); } } if (*pos == '{') @@ -312,17 +309,14 @@ parse_json(const char* pos, const char* end) return {}; if (*pos == ',') - { ++pos; - continue; - } - if (*pos == '}') + else if (*pos == '}') return Result{std::move(object), pos+1}; 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) @@ -350,6 +344,15 @@ void JsonUI::eval_json(const Value& json) 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(), params[1].as()}; + m_dimensions = dim; + m_pending_keys.push_back(resize(dim)); + } else throw runtime_error("unknown method"); } @@ -377,18 +380,26 @@ void JsonUI::parse_requests(EventMode mode) if (size == -1 or size == 0) 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; 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) { - eval_json(json); - m_incoming_text = String{pos, m_incoming_text.end()}; + try + { + 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); }}; - } diff --git a/src/json_ui.hh b/src/json_ui.hh index 7e2af6d0..ec48bf41 100644 --- a/src/json_ui.hh +++ b/src/json_ui.hh @@ -55,7 +55,7 @@ private: FDWatcher m_stdin_watcher; Vector m_pending_keys; CharCoord m_dimensions; - String m_incoming_text; + String m_requests; }; }