Ticket #37429

Vote
Score: 0
No votes
0.0% (0/0)
0.0% (0/0)

Readback Height & Width

Open Date: 2017-08-08 14:01 Last Update: 2017-08-09 16:53

Reporter: jacotton Owner: (None)
Type: Feature Requests Status: Open
Component: (None) MileStone: (None)
Priority: 5 - Medium Severity: 5 - Medium
Resolution: None

Details

I need to read the terminal height and width (as displayed in the setup menu) from the computer on the serial line.

Attachment File List

No attachments

Ticket History (3/3 Histories)

2017-08-08 14:01 Updated by: jacotton
  • New Ticket "Readback Height & Width" created
2017-08-08 15:29 Updated by: doda
Comment

It can do with the combination of standard control sequences as follows.

  • CUP: move cursor.
  • CPR: get cursor position.
  • DECSC: save cursor position.
  • DECRC: restore cursor position.

cf. https://ttssh2.osdn.jp/manual/en/about/ctrlseq.html

here is the sample ruby script.

  1. #!/usr/bin/ruby
  2. #encoding: ascii-8bit
  3. require 'io/console'
  4. resp = ""
  5. STDIN.raw do |stdin|
  6. STDERR.print "\e7" # save cursor position
  7. STDERR.print "\e[999;999H" # move cursor to 999, 999
  8. STDERR.print "\e[6n" # query cursor position
  9. STDERR.print "\e8" # restore cursor position
  10. resp = ""
  11. while (c = stdin.getc)
  12. resp << c.chr
  13. break unless /[\[\x1b\x9c;0-9]/ =~ c
  14. end
  15. end
  16. if /(?:\x9c|\x1b\[)(\d+);(\d+)R/ =~ resp # check response of cursor position query
  17. rows = $1.to_i
  18. cols = $2.to_i
  19. puts "COLUMNS=#{cols}"
  20. puts "ROWS=#{rows}"
  21. else
  22. STDERR.puts "Invalid response"
  23. end

2017-08-09 16:53 Updated by: doda
Comment

Another way, using the dtterm's window manipulation sequence.

Here is the sample bash script.

  1. #!/bin/bash
  2. die() {
  3. echo "$@"
  4. exit 1
  5. }
  6. printf "\e[18t"
  7. read -r -t 1 -n 3 resp
  8. [ $? -ne 0 ] && die "No response"
  9. CSI=$(printf "\x9b")
  10. ESC=$(printf "\e")
  11. case $resp in
  12. ${CSI}8\;) : ;;
  13. ${ESC}\[8)
  14. read -r -t 1 -n 1 resp
  15. [ $? -ne 0 -o "$resp" != ";" ] && die "Invalid Response";;
  16. *) die "Invalid Response";;
  17. esac
  18. state=0
  19. cols=0
  20. rows=0
  21. while [ $state -lt 3 ]; do
  22. read -r -t 1 -n 1 resp
  23. [ $? -ne 0 ] && die "Invalid Response"
  24. case $resp in
  25. [0-9])
  26. if [ $state -eq 0 ]; then
  27. rows=$((rows * 10 + resp))
  28. elif [ $state -eq 1 ]; then
  29. cols=$((cols * 10 + resp))
  30. fi;;
  31. \;) [ $state -lt 2 ] && state=$((++state));;
  32. t) state=3;;
  33. *) die "Invalid response";;
  34. esac
  35. done
  36. echo "COLUMNS=$cols"
  37. echo "ROWS=$rows"

Edit

You are not logged in. I you are not logged in, your comment will be treated as an anonymous post. » Login