]> git.lizzy.rs Git - nothing.git/commitdiff
(#358) implement console shortcuts for copy, cut, paste, clear
authorisidentical <batuhanosmantaskaya@gmail.com>
Mon, 16 Dec 2019 14:10:22 +0000 (17:10 +0300)
committerisidentical <batuhanosmantaskaya@gmail.com>
Mon, 16 Dec 2019 14:36:12 +0000 (17:36 +0300)
README.md
src/ui/console.c
src/ui/console_log.c
src/ui/console_log.h
src/ui/edit_field.c
src/ui/edit_field.h

index a2d9bf1103d93c460cbe0870e24954e8f3713b2d..69d95d0682d3fff09b011795b4aa09ad8a40da71 100644 (file)
--- a/README.md
+++ b/README.md
@@ -155,6 +155,10 @@ $ ./nothing
 | `ESC`     | Exit console             |
 | `Enter`   | Evaluate the expression  |
 | `Up/Down` | Traverse console history |
+| `CTRL+L`  | Clear                    |
+| `CTRL+W`  | Cut                      |
+| `META+W`  | Copy                     |
+| `CTRL+Y`  | Paste                    |
 
 ### Level Editor
 
index d1958433651ca91394e1b267187c3bb900137063..8338936aa164217a6b9cbc3eb6f7796f6e1b4898 100644 (file)
@@ -93,7 +93,6 @@ struct Console
 };
 
 /* TODO(#356): Console does not support autocompletion */
-/* TODO(#358): Console does not support copy, cut, paste operations */
 
 Console *create_console(Game *game)
 {
@@ -207,6 +206,31 @@ int console_handle_event(Console *console,
             }
         } break;
 
+       case SDLK_l: {
+            if (event->key.keysym.mod & KMOD_CTRL) {
+                console_log_clear(console->console_log);
+                return 0;
+            }
+        } break;
+
+        case SDLK_w: {
+            if (event->key.keysym.mod & (KMOD_CTRL | KMOD_ALT)) {
+                SDL_SetClipboardText(edit_field_as_text(console->edit_field));
+                if (event->key.keysym.mod & KMOD_CTRL) {
+                    edit_field_clean(console->edit_field);
+                }
+                return 0;
+            }
+        } break;
+
+       case SDLK_y: {
+            if (event->key.keysym.mod & KMOD_CTRL) {
+                char *text = SDL_GetClipboardText();
+                edit_field_append(console->edit_field, text);
+                return 0;
+            }
+        } break;
+
         case SDLK_DOWN:
             edit_field_replace(
                 console->edit_field,
index e5ee8efc1ccc6b86aa16a94a0c246b7606f778f7..87d872bc882bf655696bf0b8a6b9e4622cf9edc7 100644 (file)
@@ -107,3 +107,14 @@ int console_log_push_line(Console_Log *console_log,
 
     return 0;
 }
+
+void console_log_clear(Console_Log *console_log)
+{
+    trace_assert(console_log);
+    console_log->cursor = 0;
+    for (size_t i = 0; i < console_log->capacity; ++i) {
+        if (console_log->buffer[i]) {
+            console_log->buffer[i] = 0;
+        }
+    }
+}
index 705e93c45749e5041fe7f6c32058de493e2a6726..0c14cdb713d301203d44116a11c99c93aaf462fe 100644 (file)
@@ -19,4 +19,6 @@ int console_log_push_line(Console_Log *console_log,
                           const char *line_end,
                           Color color);
 
+void console_log_clear(Console_Log *console_log);
+
 #endif  // CONSOLE_LOG_H_
index ff193b3151db5256164ce67b9a49c73921ddb187..c055283e4a523272ab8fb3f1f3da3ef36af60fc2 100644 (file)
@@ -449,6 +449,13 @@ void edit_field_replace(Edit_field *edit_field, const char *text)
     }
 }
 
+void edit_field_append(Edit_field *edit_field, const char *text)
+{
+    size_t n = strlen(text);
+    edit_field->buffer = strcat(edit_field->buffer, text);
+    edit_field->cursor += n;
+}
+
 void edit_field_clean(Edit_field *edit_field)
 {
     trace_assert(edit_field);
index a896876c020085a7687fa6ff49aaeff8b74e8fea..59cb5079a2ed25ac211b743519ceea54f190fc0a 100644 (file)
@@ -26,6 +26,7 @@ int edit_field_event(Edit_field *edit_field, const SDL_Event *event);
 const char *edit_field_as_text(const Edit_field *edit_field);
 
 void edit_field_replace(Edit_field *edit_field, const char *text);
+void edit_field_append(Edit_field *edit_field, const char *text);
 void edit_field_clean(Edit_field *edit_field);
 void edit_field_restyle(Edit_field *edit_field,
                         Vec2f font_size,