add a scope guard sort of thing to allow restoring terminal state