View renders the textinput in its current state.
()
| 690 | |
| 691 | // View renders the textinput in its current state. |
| 692 | func (m TextInputModel) View() string { |
| 693 | // Placeholder text |
| 694 | if len(m.value) == 0 && m.Placeholder != "" { |
| 695 | return m.placeholderView() |
| 696 | } |
| 697 | |
| 698 | styleText := m.TextStyle.Inline(true).Render |
| 699 | |
| 700 | value := m.value[m.Offset:m.OffsetRight] |
| 701 | pos := max(0, m.pos-m.Offset) |
| 702 | v := styleText(m.echoTransform(string(value[:pos]))) |
| 703 | |
| 704 | if pos < len(value) { |
| 705 | if Ascii { |
| 706 | v += "¦" |
| 707 | } |
| 708 | v += m.cursorView(m.echoTransform(string(value[pos]))) // cursor and text under it |
| 709 | v += styleText(m.echoTransform(string(value[pos+1:]))) // text after cursor |
| 710 | } else { |
| 711 | v += m.cursorView(" ") |
| 712 | } |
| 713 | |
| 714 | // If a max width and background color were set fill the empty spaces with |
| 715 | // the background color. |
| 716 | valWidth := rw.StringWidth(string(value)) |
| 717 | if m.Width > 0 && valWidth <= m.Width { |
| 718 | padding := max(0, m.Width-valWidth) |
| 719 | if valWidth+padding <= m.Width && pos < len(value) { |
| 720 | padding++ |
| 721 | } |
| 722 | v += styleText(strings.Repeat(" ", padding)) |
| 723 | } |
| 724 | |
| 725 | return m.PromptStyle.Render(m.Prompt) + v |
| 726 | } |
| 727 | |
| 728 | // placeholderView returns the prompt and placeholder view, if any. |
| 729 | func (m TextInputModel) placeholderView() string { |
nothing calls this directly
no test coverage detected