// Copyright (c) 2026 Charles KWON OhJun (charleskwonohjun@gmail.com) // All rights reserved. // Cross-platform ANSI key-sequence decoder. Each termios_.go // collects bytes from the terminal in raw mode, then hands them here // for classification. The decoder output (ASCII or 0xE0-0xFC pseudo- // code) is the same on every OS so debugtui.go can stay platform- // neutral. package hbrt // decodeDebugKey translates a raw byte buffer captured in TTY/console // raw mode into a single logical key. Returns 0 when nothing was read. // Pseudo-codes 0xE0-0xE3 cover arrow keys; 0xF5-0xFC cover F5-F12. func decodeDebugKey(buf []byte, n int) int { if n == 0 { return 0 } if buf[0] != 0x1B { return int(buf[0]) } if n == 1 { return 0x1B // bare ESC } if n >= 3 && buf[1] == '[' { // Arrow keys: ESC [ A/B/C/D switch buf[2] { case 'A': return 0xE0 // Up case 'B': return 0xE1 // Down case 'C': return 0xE2 // Right case 'D': return 0xE3 // Left } // F5-F12: ESC [ 1 5 ~ through ESC [ 2 4 ~ if n >= 4 && buf[n-1] == '~' { switch string(buf[2 : n-1]) { case "15": return 0xF5 case "17": return 0xF6 case "18": return 0xF7 case "19": return 0xF8 case "20": return 0xF9 case "21": return 0xFA case "23": return 0xFB case "24": return 0xFC } } } return 0 // ignore unknown ESC sequences — never quit on them }