package input import "../picker" import "../theme" import "../terminal" import "core:fmt" import "core:strings" import "core:unicode/utf8" LOADING_FRAMES :: []string { "▱▱▱▱▱", "▰▱▱▱▱", "▰▰▱▱▱", "▰▰▰▱▱", "▰▰▰▰▱", "▰▰▰▰▰", "▰▰▰▰▱", "▰▰▰▱▱", "▰▰▱▱▱", "▰▱▱▱▱", "▱▱▱▱▱", } // row_end describes one displayed row. A full row leaves a following empty row, // and a newline consumes its byte but also starts a new row. row_end :: proc(input: ^State, start, content_width: int) -> (end, next, width: int) { end = start for end < input.len { character, size := utf8.decode_rune(input.bytes[end:input.len]) if character == '\n' { return end, end + size, width } character_width := rune_width(character) if width > 0 && width + character_width > content_width { return end, end, width } width += character_width end += size if width >= content_width { return end, end, width } } return end, end, width } trailing_empty_row :: proc(input: ^State, end, width, content_width: int) -> bool { return (end < input.len && input.bytes[end] == '\n') || width >= content_width } QUEUED_LABEL :: "Queued: " // Width of the one-row queue banner as it was painted at the old terminal width. queued_painted_width :: proc(queued: string, width: int) -> int { if queued == "" do return 0 used := min(len(QUEUED_LABEL), width) for index := 0; index < len(queued); { r, size := utf8.decode_rune(queued[index:]) if r == '\n' || r == '\t' || r == '\r' do r = ' ' w := rune_width(r) if used + w > width do break if r >= ' ' do used += w index += size } return used } // width is the drawable width (the application reserves the terminal's final // column); two columns are reserved for the marker and its space. row_count :: proc(input: ^State) -> int { content_width := max(1, int(input.width) - 2) rows := 1 index := 0 for index < input.len { end, next, width := row_end(input, index, content_width) if next == input.len { if trailing_empty_row(input, end, width, content_width) { rows += 1 } break } rows += 1 index = next } return rows } // Count the physical rows above the painted input cursor after the terminal // reflows each old input row. The status and completion rows are below it. reflow_cursor_climb :: proc( input: ^State, cursor_row, cursor_column, terminal_columns: int, ) -> int { columns := max(1, terminal_columns) content_width := max(1, int(input.width) - 2) climb := 0 index := 0 for _ in 0 ..< cursor_row { _, next, width := row_end(input, index, content_width) climb += max(1, (width + 2 + columns - 1) / columns) index = next } return climb + cursor_column / columns } draw_input_status :: proc(sb: ^strings.Builder, input: ^State, status: string) { terminal.clear_line(sb) width := int(input.width) fmt.sbprint(sb, theme.DEFAULT.status) status_width := 0 if len(status) <= width { status_width = len(status) } loading_width := 0 status_gap := status_width > 0 ? 1 : 0 if input.loading && len("loading ") + 1 + status_gap + status_width <= width { loading_width = len("loading ") + 1 } if loading_width > 0 { frames := LOADING_FRAMES fmt.sbprint(sb, frames[input.loading_frame]) } for _ in loading_width ..< width - status_width { fmt.sbprint(sb, " ") } if status_width > 0 { fmt.sbprint(sb, status) } fmt.sbprint(sb, theme.DEFAULT.reset) } draw :: proc( sb: ^strings.Builder, input: ^State, previous_rows: int, previous_below_rows: int, show_interrupt: bool, prefix_mode: bool, status: string, queued: string = "", ) { content_width := max(1, int(input.width) - 2) rows := row_count(input) queued_rows := queued != "" ? 1 : 0 if queued_rows > 0 { terminal.clear_line(sb) fmt.sbprint(sb, theme.DEFAULT.text) label := QUEUED_LABEL label_width := min(len(label), int(input.width)) fmt.sbprint(sb, label[:label_width]) used := 0 // Keep the label and preview on a single physical row, even for multiline prompts. for index := 0; index < len(queued); { r, size := utf8.decode_rune(queued[index:]) if r == '\n' || r == '\t' || r == '\r' { r = ' ' } w := rune_width(r) if used + label_width + w > int(input.width) do break if r >= ' ' { if r == ' ' { fmt.sbprint(sb, " ") } else { fmt.sbprint(sb, queued[index:index + size]) } used += w } index += size } fmt.sbprint(sb, theme.DEFAULT.reset + "\n") } input_index := 0 input.cursor = 0 input.cursor_row = 0 for row in 0 ..< rows { terminal.clear_line(sb) marker_style := prefix_mode ? theme.DEFAULT.marker_prefix : theme.DEFAULT.marker fmt.sbprint(sb, marker_style) fmt.sbprint(sb, "┃ ") fmt.sbprint(sb, theme.DEFAULT.text) row_width := 0 if input_index == input.position { input.cursor = 0 input.cursor_row = row } end, next, _ := row_end(input, input_index, content_width) for input_index < end { character, size := utf8.decode_rune(input.bytes[input_index:input.len]) character_width := rune_width(character) if character == '\t' { for _ in 0 ..< character_width do fmt.sbprint(sb, " ") } else { fmt.sbprint(sb, string(input.bytes[input_index:input_index + size])) } input_index += size row_width += character_width if input_index == input.position { input.cursor = uint(row_width) input.cursor_row = row } } input_index = next fmt.sbprint(sb, "\n") } draw_input_status(sb, input, status) if rows + queued_rows < previous_rows { // Remove stale input rows below the new status row. fmt.sbprintf(sb, "\x1b[1B\r\x1b[%dM\x1b[1A\r", previous_rows - rows - queued_rows) } if picker_mode_is_generic(input.picker_mode) && input.picker.active { picker.draw(sb, &input.picker, picker_query(input), int(input.width)) } else if input.command_autocomplete { for index in 0 ..< autocomplete_rows(input) { command := command_match(input, index) fmt.sbprint(sb, "\r\n\x1b[2K") if index == input.selected do fmt.sbprint(sb, theme.DEFAULT.selection) fmt.sbprint(sb, command) if index == input.selected do fmt.sbprint(sb, theme.DEFAULT.reset) } } else if input.picker_mode == .Files { start, _ := file_query_start(input) rows := autocomplete_rows(input) visible := min(FILE_PICKER_ROWS, rows) view_start := clamp(input.selected - visible / 2, 0, max(0, rows - visible)) matching_index := 0 for file in input.files { if !matches_query(input, start, file.name) { continue } if matching_index < view_start { matching_index += 1 continue } if matching_index >= view_start + visible { break } fmt.sbprint(sb, "\r\n\x1b[2K") if matching_index == input.selected { fmt.sbprint(sb, theme.DEFAULT.selection) } terminal.write_safe_text(sb, file.name, int(input.width)) if matching_index == input.selected { fmt.sbprint(sb, theme.DEFAULT.reset) } matching_index += 1 } } if input.notice != "" { fmt.sbprint(sb, "\r\n") terminal.clear_line(sb) fmt.sbprint(sb, theme.DEFAULT.notice) terminal.write_safe_text(sb, input.notice, int(input.width)) fmt.sbprint(sb, theme.DEFAULT.reset) } if show_interrupt { fmt.sbprint( sb, "\r\n\x1b[2K" + theme.DEFAULT.notice + "press Esc to interrupt" + theme.DEFAULT.reset, ) } below_rows := below_rows(input, show_interrupt) if below_rows < previous_below_rows { fmt.sbprintf(sb, "\x1b[1B\r\x1b[%dM\x1b[1A\r", previous_below_rows - below_rows) } // The queued row is above both the status and the input cursor; it cancels // out of the distance back from the status to the editable input. terminal.move_up(sb, below_rows + rows - input.cursor_row) terminal.set_column(sb, int(input.cursor) + 3) }