currently, when detach at htop/vim screen, will got a broken terminal.
//although leave htop run in detach state maybe not a good idea
demo
Maybe we can save the altscreen stat,
here is a demo
(with a patched dtach, which can restore from vim,htop which in altscreen)
(also a wrapper dtach.sh for convenience)

principle
the key point is save term mode when detach, and restore it when attach.
https://terminalguide.namepad.de/mode/
especially ?1049 which indicate whether in altscreen
https://terminalguide.namepad.de/mode/p1049/
I didn't implement a full terminal sequence parser
(I think that's too heavy)
I just queried and saved from the terminal emulator
(Maybe it is not compatible with all terminal emulators and usage scenarios, but it is relatively lightweight)