{"action":"created","comment":{"url":"https://api.github.com/repos/micropython/micropython/pulls/comments/31714346","id":31714346,"diff_hunk":"@@ -0,0 +1,175 @@\n+/*\n+ * This file is part of the Micro Python project, http://micropython.org/\n+ *\n+ * The MIT License (MIT)\n+ *\n+ * Copyright (c) 2015 Damien P. George\n+ *\n+ * Permission is hereby granted, free of charge, to any person obtaining a copy\n+ * of this software and associated documentation files (the \"Software\"), to deal\n+ * in the Software without restriction, including without limitation the rights\n+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n+ * copies of the Software, and to permit persons to whom the Software is\n+ * furnished to do so, subject to the following conditions:\n+ *\n+ * The above copyright notice and this permission notice shall be included in\n+ * all copies or substantial portions of the Software.\n+ *\n+ * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n+ * THE SOFTWARE.\n+ */\n+\n+\n+#include \"py/mpstate.h\"\n+\n+#include MICROPY_HAL_H\n+#include <windows.h>\n+#include <unistd.h>\n+\n+HANDLE std_in = NULL;\n+HANDLE con_out = NULL;\n+DWORD orig_mode;\n+\n+void mp_hal_stdio_mode_raw(void) {\n+    if (!std_in) {\n+        std_in = GetStdHandle(STD_INPUT_HANDLE);\n+    }\n+    if (!con_out) {\n+        con_out = CreateFile(\"CONOUT$\", GENERIC_READ | GENERIC_WRITE,\n+                      FILE_SHARE_READ | FILE_SHARE_WRITE,\n+                      NULL, OPEN_EXISTING, 0, 0);\n+    }\n+    GetConsoleMode(std_in, &orig_mode);\n+    DWORD mode = orig_mode;\n+    mode &= ~ENABLE_ECHO_INPUT;\n+    mode &= ~ENABLE_LINE_INPUT;\n+    mode &= ~ENABLE_PROCESSED_INPUT;\n+    SetConsoleMode(std_in, mode);\n+}\n+\n+void mp_hal_stdio_mode_orig(void) {\n+    assert(std_in);\n+    SetConsoleMode(std_in, orig_mode);\n+}\n+\n+void mp_hal_set_interrupt_char(char c) {\n+    assert(std_in);\n+    if (c == CHAR_CTRL_C) {\n+        DWORD mode;\n+        GetConsoleMode(std_in, &mode);\n+        mode |= ENABLE_PROCESSED_INPUT;\n+        SetConsoleMode(std_in, mode);\n+    } else {\n+        DWORD mode;\n+        GetConsoleMode(std_in, &mode);\n+        mode &= ~ENABLE_PROCESSED_INPUT;\n+        SetConsoleMode(std_in, mode);\n+    }\n+}\n+\n+void mp_hal_move_cursor_back(uint pos) {\n+    assert(con_out);\n+    CONSOLE_SCREEN_BUFFER_INFO info;\n+    GetConsoleScreenBufferInfo(con_out, &info);\n+    info.dwCursorPosition.X -= (short)pos;\n+    if (info.dwCursorPosition.X < 0) {\n+        info.dwCursorPosition.X = 0;\n+    }\n+    SetConsoleCursorPosition(con_out, info.dwCursorPosition);\n+}\n+\n+void mp_hal_erase_line_from_cursor() {\n+    assert(con_out);\n+    CONSOLE_SCREEN_BUFFER_INFO info;\n+    GetConsoleScreenBufferInfo(con_out, &info);\n+    const short len = info.dwSize.X - info.dwCursorPosition.X;\n+    DWORD written;\n+    FillConsoleOutputCharacter(con_out, ' ', len, info.dwCursorPosition, &written);\n+    FillConsoleOutputAttribute(con_out, info.wAttributes, len, info.dwCursorPosition, &written);\n+}\n+\n+typedef struct item_t {\n+    WORD vkey;\n+    const char *seq;\n+} item_t;\n+\n+// map virtual key codes to VT100 escape sequences\n+STATIC item_t keyCodeMap[] = {\n+    {VK_UP, \"[A\"},\n+    {VK_DOWN, \"[B\"},\n+    {VK_RIGHT, \"[C\"},\n+    {VK_LEFT, \"[D\"},\n+    {VK_HOME, \"[H\"},\n+    {VK_END, \"[F\"},\n+    {VK_DELETE, \"[3~\"},\n+    {0, \"\"} //sentinel\n+};\n+\n+STATIC const char *cur_esc_seq = NULL;\n+\n+STATIC int esc_seq_process_vk(int vk) {\n+    for (item_t *p = keyCodeMap; p->vkey != 0; ++p) {\n+        if (p->vkey == vk) {\n+            cur_esc_seq = p->seq;\n+            return 27; // ESC, start of escape sequence\n+        }\n+    }\n+    return 0; // nothing found\n+}\n+\n+STATIC int esc_seq_chr() {\n+    if (cur_esc_seq) {\n+        const char c = *cur_esc_seq++;\n+        if (c) {\n+            return c;\n+        }\n+        cur_esc_seq = NULL;\n+    }\n+    return 0;\n+}\n+\n+int mp_hal_stdin_rx_chr(void) {\n+    // currently processing escape seq?\n+    const int ret = esc_seq_chr();\n+    if (ret) {\n+        return ret;\n+    }\n+\n+    // poll until key which we handle is pressed\n+    DWORD num_read;\n+    INPUT_RECORD rec;\n+    for (;;) {\n+      if (!ReadConsoleInput(std_in, &rec, 1, &num_read) || !num_read) {\n+          return CHAR_CTRL_C; // EOF, ctrl-D\n+      }\n+      if (rec.EventType != KEY_EVENT || !rec.Event.KeyEvent.bKeyDown) { // only want key down events\n+          continue;\n+      }\n+      const char c = rec.Event.KeyEvent.uChar.AsciiChar;\n+      if (c) { // plain ascii char, return it\n+          return c;\n+      }\n+      const int ret = esc_seq_process_vk(rec.Event.KeyEvent.wVirtualKeyCode);\n+      if (ret) {\n+          DEBUG_printf(\"vk\");","path":"windows/windows_mphal.c","position":null,"original_position":159,"commit_id":"51fcfe4bba2112165f97f655f7c18ee6e33ee1e5","original_commit_id":"e310ca9b6bad60643dbec8dbe44244c252a9d635","user":{"login":"stinos","id":1902997,"avatar_url":"https://avatars.githubusercontent.com/u/1902997?v=3","gravatar_id":"","url":"https://api.github.com/users/stinos","html_url":"https://github.com/stinos","followers_url":"https://api.github.com/users/stinos/followers","following_url":"https://api.github.com/users/stinos/following{/other_user}","gists_url":"https://api.github.com/users/stinos/gists{/gist_id}","starred_url":"https://api.github.com/users/stinos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stinos/subscriptions","organizations_url":"https://api.github.com/users/stinos/orgs","repos_url":"https://api.github.com/users/stinos/repos","events_url":"https://api.github.com/users/stinos/events{/privacy}","received_events_url":"https://api.github.com/users/stinos/received_events","type":"User","site_admin":false},"body":"It can since I have DEBUG_printf print to the debugger instead of the commandline. But yes it's obviously an oversight of mine, fixed.","created_at":"2015-06-04T12:11:47Z","updated_at":"2015-06-04T12:11:47Z","html_url":"https://github.com/micropython/micropython/pull/1292#discussion_r31714346","pull_request_url":"https://api.github.com/repos/micropython/micropython/pulls/1292","_links":{"self":{"href":"https://api.github.com/repos/micropython/micropython/pulls/comments/31714346"},"html":{"href":"https://github.com/micropython/micropython/pull/1292#discussion_r31714346"},"pull_request":{"href":"https://api.github.com/repos/micropython/micropython/pulls/1292"}}},"pull_request":{"url":"https://api.github.com/repos/micropython/micropython/pulls/1292","id":36413091,"html_url":"https://github.com/micropython/micropython/pull/1292","diff_url":"https://github.com/micropython/micropython/pull/1292.diff","patch_url":"https://github.com/micropython/micropython/pull/1292.patch","issue_url":"https://api.github.com/repos/micropython/micropython/issues/1292","number":1292,"state":"open","locked":false,"title":"Readline improvements","user":{"login":"stinos","id":1902997,"avatar_url":"https://avatars.githubusercontent.com/u/1902997?v=3","gravatar_id":"","url":"https://api.github.com/users/stinos","html_url":"https://github.com/stinos","followers_url":"https://api.github.com/users/stinos/followers","following_url":"https://api.github.com/users/stinos/following{/other_user}","gists_url":"https://api.github.com/users/stinos/gists{/gist_id}","starred_url":"https://api.github.com/users/stinos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stinos/subscriptions","organizations_url":"https://api.github.com/users/stinos/orgs","repos_url":"https://api.github.com/users/stinos/repos","events_url":"https://api.github.com/users/stinos/events{/privacy}","received_events_url":"https://api.github.com/users/stinos/received_events","type":"User","site_admin":false},"body":"As per discussion in https://github.com/micropython/micropython/pull/1286, here are a couple of changes for readline","created_at":"2015-05-28T14:35:46Z","updated_at":"2015-06-04T12:11:47Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a76d909dbce8bfea2d0a38afa6bc48093235e98e","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/micropython/micropython/pulls/1292/commits","review_comments_url":"https://api.github.com/repos/micropython/micropython/pulls/1292/comments","review_comment_url":"https://api.github.com/repos/micropython/micropython/pulls/comments{/number}","comments_url":"https://api.github.com/repos/micropython/micropython/issues/1292/comments","statuses_url":"https://api.github.com/repos/micropython/micropython/statuses/51fcfe4bba2112165f97f655f7c18ee6e33ee1e5","head":{"label":"stinos:readline-improvements","ref":"readline-improvements","sha":"51fcfe4bba2112165f97f655f7c18ee6e33ee1e5","user":{"login":"stinos","id":1902997,"avatar_url":"https://avatars.githubusercontent.com/u/1902997?v=3","gravatar_id":"","url":"https://api.github.com/users/stinos","html_url":"https://github.com/stinos","followers_url":"https://api.github.com/users/stinos/followers","following_url":"https://api.github.com/users/stinos/following{/other_user}","gists_url":"https://api.github.com/users/stinos/gists{/gist_id}","starred_url":"https://api.github.com/users/stinos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stinos/subscriptions","organizations_url":"https://api.github.com/users/stinos/orgs","repos_url":"https://api.github.com/users/stinos/repos","events_url":"https://api.github.com/users/stinos/events{/privacy}","received_events_url":"https://api.github.com/users/stinos/received_events","type":"User","site_admin":false},"repo":{"id":19268673,"name":"micropython","full_name":"stinos/micropython","owner":{"login":"stinos","id":1902997,"avatar_url":"https://avatars.githubusercontent.com/u/1902997?v=3","gravatar_id":"","url":"https://api.github.com/users/stinos","html_url":"https://github.com/stinos","followers_url":"https://api.github.com/users/stinos/followers","following_url":"https://api.github.com/users/stinos/following{/other_user}","gists_url":"https://api.github.com/users/stinos/gists{/gist_id}","starred_url":"https://api.github.com/users/stinos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stinos/subscriptions","organizations_url":"https://api.github.com/users/stinos/orgs","repos_url":"https://api.github.com/users/stinos/repos","events_url":"https://api.github.com/users/stinos/events{/privacy}","received_events_url":"https://api.github.com/users/stinos/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/stinos/micropython","description":"The Micro Python project","fork":true,"url":"https://api.github.com/repos/stinos/micropython","forks_url":"https://api.github.com/repos/stinos/micropython/forks","keys_url":"https://api.github.com/repos/stinos/micropython/keys{/key_id}","collaborators_url":"https://api.github.com/repos/stinos/micropython/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/stinos/micropython/teams","hooks_url":"https://api.github.com/repos/stinos/micropython/hooks","issue_events_url":"https://api.github.com/repos/stinos/micropython/issues/events{/number}","events_url":"https://api.github.com/repos/stinos/micropython/events","assignees_url":"https://api.github.com/repos/stinos/micropython/assignees{/user}","branches_url":"https://api.github.com/repos/stinos/micropython/branches{/branch}","tags_url":"https://api.github.com/repos/stinos/micropython/tags","blobs_url":"https://api.github.com/repos/stinos/micropython/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/stinos/micropython/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/stinos/micropython/git/refs{/sha}","trees_url":"https://api.github.com/repos/stinos/micropython/git/trees{/sha}","statuses_url":"https://api.github.com/repos/stinos/micropython/statuses/{sha}","languages_url":"https://api.github.com/repos/stinos/micropython/languages","stargazers_url":"https://api.github.com/repos/stinos/micropython/stargazers","contributors_url":"https://api.github.com/repos/stinos/micropython/contributors","subscribers_url":"https://api.github.com/repos/stinos/micropython/subscribers","subscription_url":"https://api.github.com/repos/stinos/micropython/subscription","commits_url":"https://api.github.com/repos/stinos/micropython/commits{/sha}","git_commits_url":"https://api.github.com/repos/stinos/micropython/git/commits{/sha}","comments_url":"https://api.github.com/repos/stinos/micropython/comments{/number}","issue_comment_url":"https://api.github.com/repos/stinos/micropython/issues/comments{/number}","contents_url":"https://api.github.com/repos/stinos/micropython/contents/{+path}","compare_url":"https://api.github.com/repos/stinos/micropython/compare/{base}...{head}","merges_url":"https://api.github.com/repos/stinos/micropython/merges","archive_url":"https://api.github.com/repos/stinos/micropython/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/stinos/micropython/downloads","issues_url":"https://api.github.com/repos/stinos/micropython/issues{/number}","pulls_url":"https://api.github.com/repos/stinos/micropython/pulls{/number}","milestones_url":"https://api.github.com/repos/stinos/micropython/milestones{/number}","notifications_url":"https://api.github.com/repos/stinos/micropython/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/stinos/micropython/labels{/name}","releases_url":"https://api.github.com/repos/stinos/micropython/releases{/id}","created_at":"2014-04-29T08:04:48Z","updated_at":"2015-05-07T07:56:30Z","pushed_at":"2015-06-04T12:11:33Z","git_url":"git://github.com/stinos/micropython.git","ssh_url":"git@github.com:stinos/micropython.git","clone_url":"https://github.com/stinos/micropython.git","svn_url":"https://github.com/stinos/micropython","homepage":null,"size":20545,"stargazers_count":2,"watchers_count":2,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":2,"default_branch":"master"}},"base":{"label":"micropython:master","ref":"master","sha":"4d9cad180dbaf25cdbb9dd0c54a637483db933a7","user":{"login":"micropython","id":6298560,"avatar_url":"https://avatars.githubusercontent.com/u/6298560?v=3","gravatar_id":"","url":"https://api.github.com/users/micropython","html_url":"https://github.com/micropython","followers_url":"https://api.github.com/users/micropython/followers","following_url":"https://api.github.com/users/micropython/following{/other_user}","gists_url":"https://api.github.com/users/micropython/gists{/gist_id}","starred_url":"https://api.github.com/users/micropython/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/micropython/subscriptions","organizations_url":"https://api.github.com/users/micropython/orgs","repos_url":"https://api.github.com/users/micropython/repos","events_url":"https://api.github.com/users/micropython/events{/privacy}","received_events_url":"https://api.github.com/users/micropython/received_events","type":"Organization","site_admin":false},"repo":{"id":15337142,"name":"micropython","full_name":"micropython/micropython","owner":{"login":"micropython","id":6298560,"avatar_url":"https://avatars.githubusercontent.com/u/6298560?v=3","gravatar_id":"","url":"https://api.github.com/users/micropython","html_url":"https://github.com/micropython","followers_url":"https://api.github.com/users/micropython/followers","following_url":"https://api.github.com/users/micropython/following{/other_user}","gists_url":"https://api.github.com/users/micropython/gists{/gist_id}","starred_url":"https://api.github.com/users/micropython/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/micropython/subscriptions","organizations_url":"https://api.github.com/users/micropython/orgs","repos_url":"https://api.github.com/users/micropython/repos","events_url":"https://api.github.com/users/micropython/events{/privacy}","received_events_url":"https://api.github.com/users/micropython/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/micropython/micropython","description":"The Micro Python project","fork":false,"url":"https://api.github.com/repos/micropython/micropython","forks_url":"https://api.github.com/repos/micropython/micropython/forks","keys_url":"https://api.github.com/repos/micropython/micropython/keys{/key_id}","collaborators_url":"https://api.github.com/repos/micropython/micropython/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/micropython/micropython/teams","hooks_url":"https://api.github.com/repos/micropython/micropython/hooks","issue_events_url":"https://api.github.com/repos/micropython/micropython/issues/events{/number}","events_url":"https://api.github.com/repos/micropython/micropython/events","assignees_url":"https://api.github.com/repos/micropython/micropython/assignees{/user}","branches_url":"https://api.github.com/repos/micropython/micropython/branches{/branch}","tags_url":"https://api.github.com/repos/micropython/micropython/tags","blobs_url":"https://api.github.com/repos/micropython/micropython/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/micropython/micropython/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/micropython/micropython/git/refs{/sha}","trees_url":"https://api.github.com/repos/micropython/micropython/git/trees{/sha}","statuses_url":"https://api.github.com/repos/micropython/micropython/statuses/{sha}","languages_url":"https://api.github.com/repos/micropython/micropython/languages","stargazers_url":"https://api.github.com/repos/micropython/micropython/stargazers","contributors_url":"https://api.github.com/repos/micropython/micropython/contributors","subscribers_url":"https://api.github.com/repos/micropython/micropython/subscribers","subscription_url":"https://api.github.com/repos/micropython/micropython/subscription","commits_url":"https://api.github.com/repos/micropython/micropython/commits{/sha}","git_commits_url":"https://api.github.com/repos/micropython/micropython/git/commits{/sha}","comments_url":"https://api.github.com/repos/micropython/micropython/comments{/number}","issue_comment_url":"https://api.github.com/repos/micropython/micropython/issues/comments{/number}","contents_url":"https://api.github.com/repos/micropython/micropython/contents/{+path}","compare_url":"https://api.github.com/repos/micropython/micropython/compare/{base}...{head}","merges_url":"https://api.github.com/repos/micropython/micropython/merges","archive_url":"https://api.github.com/repos/micropython/micropython/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/micropython/micropython/downloads","issues_url":"https://api.github.com/repos/micropython/micropython/issues{/number}","pulls_url":"https://api.github.com/repos/micropython/micropython/pulls{/number}","milestones_url":"https://api.github.com/repos/micropython/micropython/milestones{/number}","notifications_url":"https://api.github.com/repos/micropython/micropython/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/micropython/micropython/labels{/name}","releases_url":"https://api.github.com/repos/micropython/micropython/releases{/id}","created_at":"2013-12-20T11:47:07Z","updated_at":"2015-06-04T08:16:26Z","pushed_at":"2015-06-04T12:11:34Z","git_url":"git://github.com/micropython/micropython.git","ssh_url":"git@github.com:micropython/micropython.git","clone_url":"https://github.com/micropython/micropython.git","svn_url":"https://github.com/micropython/micropython","homepage":null,"size":47031,"stargazers_count":1907,"watchers_count":1907,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":359,"mirror_url":null,"open_issues_count":131,"forks":359,"open_issues":131,"watchers":1907,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/micropython/micropython/pulls/1292"},"html":{"href":"https://github.com/micropython/micropython/pull/1292"},"issue":{"href":"https://api.github.com/repos/micropython/micropython/issues/1292"},"comments":{"href":"https://api.github.com/repos/micropython/micropython/issues/1292/comments"},"review_comments":{"href":"https://api.github.com/repos/micropython/micropython/pulls/1292/comments"},"review_comment":{"href":"https://api.github.com/repos/micropython/micropython/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/micropython/micropython/pulls/1292/commits"},"statuses":{"href":"https://api.github.com/repos/micropython/micropython/statuses/51fcfe4bba2112165f97f655f7c18ee6e33ee1e5"}}}}