V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
wohenyingyu01
V2EX  ›  问与答

写一个命令行工具怎样让输出的东西更新?

  •  
  •   wohenyingyu01 · 2016-02-29 11:24:09 +08:00 · 1823 次点击
    这是一个创建于 2977 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请教大侠:

    比如用 c 写的 print hello world ,怎样让已经 print 出来的 hello world 更新成成 123456 ,而不是重新输出一行?比如 curl 下载的时候会更新百分比,或者更高级一点的 vim ,在更新的同时还能互动?这种功能需要特定语言实现么?

    谢谢

    10 条回复    2016-02-29 20:34:08 +08:00
    haython
        1
    haython  
       2016-02-29 11:38:25 +08:00   ❤️ 1
    输出\r,回到行首,重新覆盖
    Sunyanzi
        2
    Sunyanzi  
       2016-02-29 11:43:32 +08:00   ❤️ 1
    for i in {1..50}; do echo -ne "\r"$i; sleep 0.3; done

    是这个意思 ..?
    wohenyingyu01
        3
    wohenyingyu01  
    OP
       2016-02-29 15:36:51 +08:00
    @Sunyanzi
    @haython
    回头试试,不知道 java 和 swift 行不行
    des
        4
    des  
       2016-02-29 17:33:48 +08:00 via Android
    @haython 换行了怎么办的?
    mx3y
        5
    mx3y  
       2016-02-29 17:43:59 +08:00
    复制当前的文本内容,然后替换需要更新的,然后清空,再输出。
    manfay
        6
    manfay  
       2016-02-29 18:04:50 +08:00
    Ncurses
    wohenyingyu01
        7
    wohenyingyu01  
    OP
       2016-02-29 19:20:13 +08:00
    @mx3y 怎么清空?
    zhjits
        8
    zhjits  
       2016-02-29 19:30:36 +08:00
    方法 1 :根据终端窗口的长宽来填空字符或者换行
    方法 2 : libreadline
    方法 3 : ncurses
    crystom
        9
    crystom  
       2016-02-29 20:19:06 +08:00
    tracyone
        10
    tracyone  
       2016-02-29 20:34:08 +08:00
    linux 下时 getopt 和 getoptlong 函数,自行百度
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3701 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 04:25 · PVG 12:25 · LAX 21:25 · JFK 00:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.