(m *TuiModel, str string)
| 254 | } |
| 255 | |
| 256 | func HandleFormatInput(m *TuiModel, str string) bool { |
| 257 | switch str { |
| 258 | case "tab": |
| 259 | InsertCharacter(m, "\t") |
| 260 | return true |
| 261 | case "enter": |
| 262 | InsertCharacter(m, "\n") |
| 263 | return true |
| 264 | case "backspace": |
| 265 | cursor := m.Format.CursorX + FormatModeOffset |
| 266 | input := m.Format.EditSlices[m.Format.CursorY] |
| 267 | inputLen := len(*input) |
| 268 | runes := []rune(*input) |
| 269 | if m.Format.CursorX > 0 { // cursor in middle of line |
| 270 | if cursor == inputLen && inputLen > 0 { |
| 271 | *input = (*input)[0 : inputLen-1] |
| 272 | } else if cursor > 0 { |
| 273 | min := Max(cursor, 0) |
| 274 | min = Min(min, inputLen-1) |
| 275 | first := runes[:min-1] |
| 276 | last := runes[min:] |
| 277 | *input = string(first) + string(last) |
| 278 | } |
| 279 | |
| 280 | return false |
| 281 | } else if m.Format.CursorY > 0 && m.Format.CursorX == 0 { // beginning of line |
| 282 | yOffset := Max(m.Viewport.YOffset, 0) |
| 283 | cursor := m.Format.RunningOffsets[m.Format.CursorY+yOffset] + m.Format.CursorX |
| 284 | runes := []rune(m.Data().EditTextBuffer) |
| 285 | min := Max(cursor, 0) |
| 286 | min = Min(min, len(m.Data().EditTextBuffer)-1) |
| 287 | first := runes[:min-1] |
| 288 | last := runes[min:] |
| 289 | m.Data().EditTextBuffer = string(first) + string(last) |
| 290 | if yOffset+m.Viewport.Height == len(m.Format.Text) && yOffset > 0 { |
| 291 | m.Viewport.YOffset-- |
| 292 | } else { |
| 293 | m.Format.CursorY-- |
| 294 | } |
| 295 | m.Format.Text = GetFormattedTextBuffer(m) |
| 296 | m.SetViewSlices() |
| 297 | } |
| 298 | |
| 299 | return true |
| 300 | } |
| 301 | |
| 302 | return false |
| 303 | } |
| 304 | |
| 305 | func HandleFormatMode(m *TuiModel, str string) { |
| 306 | var ( |
no test coverage detected