Golang : Command line ticker to show work in progress
Problem:
Your command line application is crunching data and you want your program to show the user that it is working. It is not a progress bar with percentage completion, but just a simple ticker to distract the user's attention. How to do that?
Solution:
Create an array of -
,\
,/
' and -
. Randomly selects one or iterates over the items in the array and display it.
Here you go!
package main
/*
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
char getch(){
char ch = 0;
struct termios old = {0};
fflush(stdout);
if( tcgetattr(0, &old) < 0 ) perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if( tcsetattr(0, TCSANOW, &old) < 0 ) perror("tcsetattr ICANON");
if( read(0, &ch,1) < 0 ) perror("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if(tcsetattr(0, TCSADRAIN, &old) < 0) perror("tcsetattr ~ICANON");
return ch;
}
*/
import "C"
import (
"fmt"
"math/rand"
"os"
"time"
)
func main() {
go func() {
key := C.getch() // get single key hit without pressing Enter button
fmt.Println()
fmt.Println("Exiting ...")
if key == 27 {
os.Exit(0)
}
}()
// recording in progress ticker. From good old DOS days.
ticker := []string{
"-",
"\\", //<--- need escape
"/",
"|",
}
rand.Seed(time.Now().UnixNano())
fmt.Println("Press ESC key to quit...")
for {
fmt.Printf("\rProcessing data now! [%v]", ticker[rand.Intn(len(ticker)-1)])
}
}
Run the code and you should see the ticker in action.
It is use in https://socketloop.com/tutorials/golang-save-webcamera-frames-to-video-file as well.
Happy coding!
References:
https://www.socketloop.com/tutorials/golang-overwrite-previous-output-with-count-down-timer
https://socketloop.com/tutorials/golang-randomly-pick-an-item-from-a-slice-array-example
http://stackoverflow.com/questions/14094190/golang-function-similar-to-getchar
See also : Golang : Overwrite previous output with count down timer
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+18.7k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+6.2k Golang : Gorrila mux.Vars() function example
+5k Golang : Calculate US Dollar Index (DXY)
+37.8k Golang : Remove dashes(or any character) from string
+11.1k Golang : Add ASCII art to command line application launching process
+12.6k Golang : Gin framework accept query string by post request example
+7.7k Golang : Accept any number of function arguments with three dots(...)
+14.4k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+10.7k Golang : Detect user location with HTML5 geo-location
+21.5k Golang : Get ASCII code from a key press(cross-platform) example
+34.2k Golang : Validate IP address
+10.1k Use systeminfo to find out installed Windows Hotfix(s) or updates