„tput cols“ gibt immer den Standardwert 80 zurückJava

Java-Forum
Guest
 „tput cols“ gibt immer den Standardwert 80 zurück

Post by Guest »

Ich habe vor Jahren eine Bibliothek zum Rendern im Terminal erstellt.
Beim erneuten Ausführen im Jahr 2025 ist sie teilweise kaputt.
Hier ist ein kleines Beispiel, das das Problem eingrenzt :

Code: Select all

TerminalCols.java

Code: Select all

package p5_terminal_graphics_examples;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class TerminalCols {

public static void main(String[] args) {
while (true) {
int cols = cols();
System.out.println("Terminal columns: " + cols);

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

static public int cols() {
return Integer.parseInt(cmd("tput cols"));
}

static public String cmd(String args) {
return exec("sh", "-c", args);
}

static public String exec(String... cmd) {

try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();

Process p;

p = Runtime.getRuntime().exec(cmd);

int c;
InputStream in = p.getInputStream();

while ((c = in.read()) != -1) {
bout.write(c);
}

in = p.getErrorStream();

while ((c = in.read()) != -1) {
bout.write(c);
}

p.waitFor();

String result = new String(bout.toByteArray());
return result.trim();
}
catch (IOException | InterruptedException e) {
return null;
}

}
}
Der obige Code sollte die Anzahl der Spalten ausdrucken, die das Terminal jede Sekunde hat.
Es wird jedoch immer der Standardwert 80 gedruckt.
Wenn ich direkt tput cols in das Terminal eingebe, wird immer die richtige Antwort zurückgegeben.
Mit der Zeit ist also etwas kaputt gegangen und ich weiß nicht, wie ich weiter herausfinden soll, wie um das zu beheben. Jede Hilfe wäre willkommen.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post