How to overwrite a printed line in the shell with Ruby?

后端 未结 3 1781
北荒
北荒 2020-12-13 17:23

How would I overwrite the previously printed line in a Unix shell with Ruby?

Say I\'d like to output the current time on a shell every second, but instead of stackin

3条回答
  •  自闭症患者
    2020-12-13 18:14

    Use the escape sequence \r at the end of the line - it is a carriage return without a line feed.

    On most unix terminals this will do what you want: the next line will overwrite the previous line.

    You may want to pad the end of your lines with spaces if they are shorter than the previous lines.

    Note that this is not Ruby-specific. This trick works in any language!

提交回复
热议问题