// File: /workspace/github_c_makefile_star2_O3/moefh[P]hed/src/editor.c
// Function: prompt_get_text
static int prompt_get_text(struct hed_editor *editor, const char *prompt, char *str, size_t max_str_len)
{
  struct hed_screen *scr = &editor->screen;

  size_t prompt_len = strlen(prompt);
  size_t str_len = strlen(str);
  size_t cursor_pos = str_len;
  char key_err[64];

  clear_msg();
  scr->redraw_needed = true;
  while (! editor->quit) {
    show_cursor(false);
    if (scr->redraw_needed)
      draw_main_screen(editor);
    reset_color();
    set_color(FG_BLACK, BG_GRAY);
    move_cursor(1, scr->h - EDITOR_FOOTER_LINES + 1);
    out(" %s: %s", prompt, str);
    clear_eol();
    size_t scr_cursor_pos = utf8_len_upto(str, str + cursor_pos);
    move_cursor(4 + prompt_len + scr_cursor_pos, scr->h - EDITOR_FOOTER_LINES + 1);
    set_color(FG_BLACK, BG_GRAY);
    show_cursor(true);
    hed_scr_flush();
    
    int k = read_key(scr->term_fd, key_err, sizeof(key_err));
    switch (k) {
    case KEY_REDRAW:
      show_cursor(false);
      reset_color();
      clear_screen();
      show_cursor(true);
      scr->redraw_needed = true;
      break;
      
    case CTRL_KEY('c'):
    case '\r':
      editor->mode = HED_MODE_DEFAULT;
      show_cursor(false);
      clear_msg();
      return (k == '\r') ? 0 : -1;

    case CTRL_KEY('a'):
    case KEY_HOME:
      cursor_pos = 0;
      break;
      
    case CTRL_KEY('e'):
    case KEY_END:
      cursor_pos = str_len;
      break;
      
    case CTRL_KEY('b'):
    case KEY_ARROW_LEFT:
      if (cursor_pos > 0) {
        const char *pos = utf8_prev(str, str + cursor_pos);
        cursor_pos = pos - str;
      }
      break;
      
    case CTRL_KEY('f'):
    case KEY_ARROW_RIGHT:
      if (cursor_pos < str_len) {
        const char *pos = utf8_next(str + cursor_pos);
        cursor_pos = pos - str;
      }
      break;

    case CTRL_KEY('t'):
      if (editor->mode == HED_MODE_READ_FILENAME) {
        show_cursor(false);
        char filename[256];
        if (hed_select_file(editor, filename, sizeof(filename)) >= 0) {
          size_t filename_len = strlen(filename) + 1;
          if (filename_len > max_str_len) {
            filename_len = max_str_len;
          }
          memcpy(str, filename, filename_len-1);
          str[filename_len-1] = '\0';
          editor->mode = HED_MODE_DEFAULT;
          return 0;
        }
        show_cursor(true);
        scr->redraw_needed = true;
      }
      break;
      
    case 8:
    case 127:
      if (cursor_pos > 0) {
        //memmove(str + cursor_pos - 1, str + cursor_pos, str_len - cursor_pos + 1);
        const char *prev = utf8_prev(str, str + cursor_pos);
        size_t n_erase = str + cursor_pos - prev;
        memmove(str + cursor_pos - n_erase, str + cursor_pos, str_len - cursor_pos + n_erase);
        cursor_pos -= n_erase;
        str_len -= n_erase;
      }
      break;

    case KEY_DEL:
      if (cursor_pos < str_len) {
        memmove(str + cursor_pos, str + cursor_pos + 1, str_len - cursor_pos + 1);
        str_len--;
      }
      break;
      
    default:
      if (k >= 32 && k < 127 && (str_len + 2 <= max_str_len)) {
        memmove(str + cursor_pos + 1, str + cursor_pos, str_len - cursor_pos + 1);
        str[cursor_pos++] = k;
        str_len++;
      }
    }
  }
  
  editor->mode = HED_MODE_DEFAULT;
  clear_msg();
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %rdx, %r14
movq %rdi, %rbx
movq %rsi, 0x8(%rsp)
movq %rsi, %rdi
callq 0x20e0
movq %rax, (%rsp)
movq %r14, %rdi
callq 0x20e0
movq %rax, %r12
callq 0x2db2
movb $0x1, 0x115(%rbx)
cmpb $0x0, (%rbx)
jne 0x5faf
addq $0x4, (%rsp)
movq %r12, %r13
xorl %edi, %edi
callq 0x31e0
cmpb $0x1, 0x115(%rbx)
jne 0x5d9d
movq %rbx, %rdi
callq 0x4d9b
callq 0x318e
movl $0x1e, %edi
movl $0x2f, %esi
callq 0x3124
movl 0x110(%rbx), %esi
addl $-0x2, %esi
movl $0x1, %edi
callq 0x31aa
leaq 0x1b7f(%rip), %rdi      # 0x794a
movq 0x8(%rsp), %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2e58
callq 0x319c
leaq (%r14,%r13), %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x37f1
movq (%rsp), %rcx
leal (%rax,%rcx), %edi
movl 0x110(%rbx), %esi
addl $-0x2, %esi
callq 0x31aa
movl $0x1e, %edi
movl $0x2f, %esi
callq 0x3124
movl $0x1, %edi
callq 0x31e0
callq 0x2de3
movl 0x108(%rbx), %edi
movl $0x40, %edx
leaq 0x10(%rsp), %rsi
callq 0x2898
movl %eax, %r15d
cmpl $0x7e, %eax
jle 0x5e7d
leal -0x111000(%r15), %eax
cmpl $0x7, %eax
ja 0x5eb0
leaq 0x1867(%rip), %rcx      # 0x76b8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r12, %rdx
subq %r13, %rdx
jbe 0x5fa6
leaq 0x1(%rbp), %rsi
incq %rdx
movq %rbp, %rdi
callq 0x2210
decq %r12
jmp 0x5fa6
leal -0x1(%r15), %eax
cmpl $0x13, %eax
ja 0x5f71
leaq 0x17d7(%rip), %rcx      # 0x7668
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
testq %r13, %r13
je 0x5f2f
movq %r14, %rdi
movq %rbp, %rsi
callq 0x36b0
jmp 0x5eea
cmpl $0x7f, %r15d
je 0x5efd
cmpl $0x110002, %r15d        # imm = 0x110002
jne 0x5f71
xorl %edi, %edi
callq 0x31e0
callq 0x318e
callq 0x31fe
jmp 0x5f5e
cmpq %r13, %r12
jbe 0x5fa6
movq %rbp, %rdi
callq 0x36f2
movq %rax, %r13
subq %r14, %r13
jmp 0x5fa6
movq %r12, %r13
jmp 0x5fa6
testq %r13, %r13
je 0x5f2f
movq %r14, %rdi
movq %rbp, %rsi
callq 0x36b0
movq %rbp, %r15
subq %rax, %r15
movq %r12, %rdx
subq %r13, %rdx
addq %r15, %rdx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2210
subq %r15, %r13
subq %r15, %r12
jmp 0x5fa6
xorl %r13d, %r13d
jmp 0x5fa6
cmpl $0x1, 0x104(%rbx)
jne 0x5fa6
xorl %edi, %edi
callq 0x31e0
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
leaq 0x50(%rsp), %rsi
callq 0x3824
testl %eax, %eax
jns 0x5ff5
movl $0x1, %edi
callq 0x31e0
movb $0x1, 0x115(%rbx)
jmp 0x5fa6
leal -0x20(%r15), %eax
cmpl $0x5e, %eax
ja 0x5fa6
leaq 0x2(%r12), %rax
cmpq $0x100, %rax            # imm = 0x100
ja 0x5fa6
leaq 0x1(%rbp), %rdi
movq %r12, %rdx
subq %r13, %rdx
incq %rdx
movq %rbp, %rsi
callq 0x2210
incq %r13
movb %r15b, (%rbp)
incq %r12
cmpb $0x1, (%rbx)
jne 0x5d85
movl $0x0, 0x104(%rbx)
callq 0x2db2
xorl %eax, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, 0x104(%rbx)
xorl %edi, %edi
callq 0x31e0
callq 0x2db2
xorl %eax, %eax
cmpl $0xd, %r15d
setne %al
negl %eax
jmp 0x5fc0
leaq 0x50(%rsp), %r15
movq %r15, %rdi
callq 0x20e0
leaq -0x100(%rax), %rcx
cmpq $-0x101, %rcx           # imm = 0xFEFF
movl $0xff, %r12d
cmovaeq %rax, %r12
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x21b0
movb $0x0, (%r14,%r12)
movl $0x0, 0x104(%rbx)
jmp 0x5fbe
nopl (%rax)