0

I am trying to make a software that determines my PS1 (my prompt) depending on the content of what's written in the terminal (limited to what I can see), to avoid cluttering my work space. For instance, if the terminal seems cluttered or are missing, reprint some info (path, user, host)

I am using GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu), though I don't know if it will change anything

Let's say my terminal looks like this right now :

terminal buffer

I would like to get a variable with this content as a single string:

$ echo 1
1
$ echo 2
2
$ LANG= git status
fatal: not a git repository (or any of the parent directories): .git
$ echo 3
3
$ time

real    0m0,000s
user    0m0,000s
sys 0m0,000s
$ LANG= date
Fri Jan  6 13:17:37 CET 2023
$ 

Which I would forward to my program.

This question states it is impossible, but it seems to use the curses lib, so I don't know if it is related.

Read screen character from terminal

Bonus questions:

Pierre-Antoine Guillaume
  • 1,047
  • 1
  • 12
  • 28
  • its technically possible but you may have to implement your own Shell and/or Terminal Emulator. – pynexj Jan 06 '23 at 15:05
  • 1
    I often see references to "terminal buffer" in postings. Is it possible to specify a "terminal buffer" which is like a DMA drive which is then directly accessible for queries like the requestor, but is in fact a duplicate/mirror of the display, not the display directly ? Or is that just a plain wacko concept? – Eric Marceau Jan 06 '23 at 21:17
  • consider `printf '1\t2\r\t3\n'` - curses output can be even more mangled. Run a text editor (`vi`, `nano`, etc) under `script` and then look at the `typescript` log in `less`. – jhnc Jan 06 '23 at 21:22
  • You could run your shell under `tmux` and then use something like [tmux-logging](https://github.com/tmux-plugins/tmux-logging) to capture a screenshot. The output may still contain control sequences (eg. I don't know how colour information is saved by that plugin) – jhnc Jan 06 '23 at 21:26
  • Looks like https://unix.stackexchange.com/questions/145050/what-exactly-is-scrollback-and-scrollback-buffer implies it is not possible as well – Pierre-Antoine Guillaume Jan 09 '23 at 09:03

1 Answers1

1

Sometimes, you need to stop chasing dragon. Stop what you are doing, step back and think what you really want to do. There maybe another approach which could be easier and better. The fact that you have to read from the terminal tells me you are poking a sleeping dragon. You can try, but expect undesirable outcome.

If you want clear indication where your prompt is, try something like

Calvin Kim
  • 359
  • 2
  • 6