Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Console bug
#5
Well I'll go ahead and post a snippet of how I worked around that. The shell script was gleaned from Stackoverflow. The results are copied to the clipboard and then accessed by you program. Of course the snippet was bracketed by a meta condition of it being Linux.

Code: (Select All)
    'In linux, there is no easy way to get output of the
    'cursor position query "ESC[6n". One has to resort to
    'dirty tricks to get the output.
    DIM AS STRING script, clipboardTemp
    clipboardTemp = _CLIPBOARD$
    'If shell script doesn't exist create it
    IF NOT _FILEEXISTS("rc.sh") THEN
      f = FREEFILE
      script = script + "#!/bin/sh" + CHR$(10)
      script = script + "if [ -t 0 ] && [ -t 1 ]; then" + CHR$(10)
      'Save the terminal setting into a temp variable
      script = script + "  old_settings=$(stty -g) || exit" + CHR$(10)
      'In case of problem, restore settings
      script = script + "  trap 'stty " + CHR$(34) + "$(old_settings)" + CHR$(34) + "' INT TERM QUIT ALRM" + CHR$(10)
      'Set the terminal settings to receive the output of the query
      script = script + "  stty -icanon -echo min 0 time 3 || exit" + CHR$(10)
      'Make the query
      script = script + "  printf '\033[6n'" + CHR$(10)
      'Perform black magic to get the results of query
      script = script + "  pos=$(dd count=1 2> /dev/null)" + CHR$(10)
      script = script + "  pos=${pos%R*}" + CHR$(10)
      script = script + "  pos=${pos##*\[}" + CHR$(10)
      script = script + "  x=${pos##*;} y=${pos%%;*}" + CHR$(10)
      'Restore terminal state
      script = script + "  stty " + CHR$(34) + "$old_settings" + CHR$(34) + CHR$(10)
      script = script + "  trap - INT TERM QUIT ALRM" + CHR$(10)
      script = script + "fi" + CHR$(10)
      'Spit results to clipboard
      $IF MAC THEN
      script = script + "echo " + CHR$(34) + "$x:$y" + CHR$(34) + " | pbcopy" + CHR$(10)
      $ELSE
        script = script + "echo " + CHR$(34) + "$x:$y" + CHR$(34) + " | xclip -selection clipboard" + CHR$(10)
      $END IF
      OPEN "rc.sh" FOR OUTPUT AS f
      PRINT #f, script;
      CLOSE f
      'Make the script executable
      SHELL _HIDE "chmod +x rc.sh"
    END IF

    SHELL "./rc.sh"
    c$ = _CLIPBOARD$
    _CLIPBOARD$ = clipboardTemp
    y$ = LEFT$(c$, INSTR(c$, ":"))
    x$ = MID$(c$, INSTR(c$, ":") + 1)
    y = VAL(y$): x = VAL(x$)

There is also a way to do it in C. This is not my code, but I tested it and it does work.


Code: (Select All)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>

int getCursorPosition(int *x, int *y) {
    struct termios old_termios;
    char response[32];
    int i = 0;
    char c;
   
    // Save current terminal settings
    tcgetattr(STDIN_FILENO, &old_termios);
   
    // Set terminal to non-canonical mode
    struct termios new_termios = old_termios;
    new_termios.c_lflag &= ~(ICANON | ECHO);
    new_termios.c_cc[VMIN] = 0;
    new_termios.c_cc[VTIME] = 0;
    tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);
   
    // Send cursor position request
    printf("\033[6n");
    fflush(stdout);
   
    // Read the response
    while (i < sizeof(response) - 1) {
        if (read(STDIN_FILENO, &c, 1) > 0) {
            response[i++] = c;
            if (c == 'R') break;
        }
    }
   
    response[i] = '\0';
   
    // Restore terminal settings
    tcsetattr(STDIN_FILENO, TCSANOW, &old_termios);
   
    // Parse response: ESC[<row>;<col>R
    if (i > 3 && response[0] == '\033' && response[1] == '[') {
        char *ptr = response + 2;
        char *end;
       
        *y = strtol(ptr, &end, 10);
        if (*end == ';') {
            *x = strtol(end + 1, NULL, 10);
            return 1;
        }
    }
   
    return 0;
}

int main() {
    int x, y;
   
    printf("Current cursor position:\n");
   
    if (getCursorPosition(&x, &y)) {
        printf("X = %d, Y = %d\n", x, y);
    } else {
        printf("Could not determine cursor position\n");
    }
   
    return 0;
}
Reply


Messages In This Thread
Console bug - by justsomeguy - 02-21-2026, 05:34 PM
RE: Console bug - by SMcNeill - 02-21-2026, 09:06 PM
RE: Console bug - by justsomeguy - 02-21-2026, 09:45 PM
RE: Console bug - by SMcNeill - 02-22-2026, 02:53 AM
RE: Console bug - by justsomeguy - 02-22-2026, 03:58 PM
RE: Console bug - by a740g - 02-23-2026, 06:59 AM
RE: Console bug - by grymmjack - 03-01-2026, 04:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Setting Focus between console and terminal cage 4 1,406 09-01-2024, 02:44 AM
Last Post: cage
  Possible Bug SpriggsySpriggs 1 732 04-06-2024, 12:33 AM
Last Post: a740g
Bug D notation bug bplus 13 2,817 08-26-2023, 05:12 PM
Last Post: Jack

Forum Jump:


Users browsing this thread: 1 Guest(s)