// File: /workspace/github_c_makefile_star2_O2/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 $0x168, %rsp            # imm = 0x168
movq %rdx, %r14
movq %rdi, %rbx
movq %rsi, 0x18(%rsp)
movq %rsi, %rdi
callq 0x20e0
movq %rax, %r15
movq %r14, 0x8(%rsp)
movq %r14, %rdi
callq 0x20e0
movq %rax, %r13
callq 0x2c91
movb $0x1, 0x115(%rbx)
addq $0x4, %r15
movq %r15, 0x10(%rsp)
movq %r13, (%rsp)
cmpb $0x0, (%rbx)
jne 0x5c79
movq %r13, %r12
xorl %edi, %edi
callq 0x30ac
cmpb $0x1, 0x115(%rbx)
jne 0x5a15
movq %rbx, %rdi
callq 0x49fc
callq 0x305c
pushq $0x1e
popq %r15
movl %r15d, %edi
pushq $0x2f
popq %r13
movl %r13d, %esi
callq 0x2ff2
movl 0x110(%rbx), %esi
addl $-0x2, %esi
pushq $0x1
popq %r14
movl %r14d, %edi
callq 0x3078
leaq 0x1f01(%rip), %rdi      # 0x794a
movq 0x18(%rsp), %rsi
movq 0x8(%rsp), %rbp
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2d2c
callq 0x306a
leaq (%r12,%rbp), %rax
movq %rbp, %rdi
movq %rax, %rbp
movq %rax, %rsi
callq 0x368d
movq 0x10(%rsp), %rcx
leal (%rax,%rcx), %edi
movl 0x110(%rbx), %esi
addl $-0x2, %esi
callq 0x3078
movl %r15d, %edi
movl %r13d, %esi
callq 0x2ff2
movl %r14d, %edi
callq 0x30ac
callq 0x2cbb
movl 0x108(%rbx), %edi
leaq 0x20(%rsp), %rsi
pushq $0x40
popq %rdx
callq 0x278c
movl %eax, %r15d
leal -0x1(%r15), %eax
cmpl $0x13, %eax
jbe 0x5b0c
leal -0x111000(%r15), %eax
cmpl $0x7, %eax
ja 0x5b28
leaq 0x1be4(%rip), %rcx      # 0x76b8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq (%rsp), %r13
jmpq *%rax
movq (%rsp), %rdx
movq %r12, %r13
subq %r12, %rdx
jbe 0x59f1
leaq 0x1(%rbp), %rsi
incq %rdx
movq %rbp, %rdi
callq 0x2210
decq (%rsp)
movq %r12, %r13
jmp 0x59f1
leaq 0x1b55(%rip), %rcx      # 0x7668
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq (%rsp), %r13
jmpq *%rax
xorl %r13d, %r13d
jmp 0x59f1
cmpl $0x7f, %r15d
je 0x5b9d
cmpl $0x110002, %r15d        # imm = 0x110002
jne 0x5bdf
xorl %edi, %edi
callq 0x30ac
callq 0x305c
callq 0x30ca
jmp 0x5c62
movl $0x0, %r13d
testq %r12, %r12
je 0x59f1
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3550
movq %rax, %r13
subq %r14, %r13
jmp 0x59f1
movq %r12, %r13
cmpq %r12, (%rsp)
jbe 0x59f1
movq %rbp, %rdi
callq 0x3592
movq %rax, %r13
subq 0x8(%rsp), %r13
jmp 0x59f1
movl $0x0, %r13d
testq %r12, %r12
je 0x59f1
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x3550
movq %rbp, %r15
subq %rax, %r15
movq (%rsp), %r14
movq %r14, %rdx
subq %r12, %rdx
addq %r15, %rdx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2210
subq %r15, %r12
subq %r15, %r14
jmp 0x5c29
leal -0x20(%r15), %eax
movq %r12, %r13
cmpl $0x5e, %eax
ja 0x59f1
movq (%rsp), %rax
addq $0x2, %rax
movq %r12, %r13
cmpq $0x100, %rax            # imm = 0x100
ja 0x59f1
leaq 0x1(%rbp), %rdi
movq (%rsp), %r14
movq %r14, %rdx
subq %r12, %rdx
incq %rdx
movq %rbp, %rsi
callq 0x2210
incq %r12
movb %r15b, (%rbp)
incq %r14
movq %r14, (%rsp)
movq %r12, %r13
jmp 0x59f1
cmpl $0x1, 0x104(%rbx)
movq %r12, %r13
jne 0x59f1
xorl %edi, %edi
callq 0x30ac
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
leaq 0x60(%rsp), %rsi
callq 0x36bc
testl %eax, %eax
jns 0x5cb9
pushq $0x1
popq %rdi
callq 0x30ac
movb $0x1, 0x115(%rbx)
movq %r12, %r13
jmp 0x59f1
andl $0x0, 0x104(%rbx)
callq 0x2c91
xorl %eax, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
andl $0x0, 0x104(%rbx)
xorl %edi, %edi
callq 0x30ac
callq 0x2c91
xorl %eax, %eax
cmpl $0xd, %r15d
setne %al
negl %eax
jmp 0x5c87
leaq 0x60(%rsp), %r15
movq %r15, %rdi
callq 0x20e0
leaq -0x100(%rax), %rcx
cmpq $-0x101, %rcx           # imm = 0xFEFF
movl $0xff, %r12d
cmovaeq %rax, %r12
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x21b0
movb $0x0, (%r14,%r12)
andl $0x0, 0x104(%rbx)
jmp 0x5c85
nop