เขาทำ progress ใน terminal กันยังไงหว่า?
เคยเรียกคำสั่งอะไรใน terminal แล้วมันก็ขึ้นเลข 0%, 10%, 20%, … 100% ใน terminal ไหม (มันอัพเดทข้อความในบรรทัดเดิมได้เรื่อยๆ… มันทำยังไงหว่า?)
อ่ะนี่ ตัวอักษรสำคัญเลขคือ \r (carriage return)
#include<stdio.h> void delay() { for(int i=0;i<1000000;i++) for(int j=0;j<10;j++); } int main() { for(int p=0;p<=100;p++) { delay(); printf("\rPercent: %d ", p); fflush(stdout); } printf("\n"); return 0; }
งืมๆ… มาพ่นโค้ด แล้วก็ไป ฮ่าๆ
Wow! This trick is cool. Instead of having thousands of characters printed out, I can have just a few lines on my screen.
Note that this still works well if you redirect output from screen to a file!
[Reply]
อ๋อ.. \r นี่เอง
[Reply]