![]() ![]() As in the p example above, you can see that we have a string object returned. ![]() The p command is more for debugging your code, as it will help you know what value it is that is being printed. The p command is similar to puts in that it will return a newline after it prints, making it so each result is on its own line. some_string = "This is a string."Īs you can see, the puts command will return a newline after it prints, making it so each result is on its own line.Īnd finally, let’s take a look at what the p command will do. Let’s take a look at what the puts command will do. some_string = "This is a string."Īnother_string = "This is another string."Īs you can see, the print command will print each string on the same line next to each other. Let’s first look at printing to the console using the print command, with a very simple example. We can do this by using either the print, puts, or p commands. They’re not supposed to be fool-proof, only to illustrate various approaches.)įor similar screen control in PHP scripts, you could look at the PECL ncurses extension.There are 3 main ways we can use Ruby to print to the console. (Note that a lot of the specifics of the examples above assume that all your output is on the same line. Man terminfo will tell you what “capability name” to use with tput. Will move the cursor to the left-most column ( hpa 0) and clear to the end of the line. Will move the cursor to the left three times ( cub 3) and clear to the end of the line ( el), using whatever character sequence is appropriate for the current terminal printf "89%% hello" sleep 1 tput hpa 0 tput el printf "90%%\n" Tput provides a terminal- and printf-agnostic way of accessing these sequences: printf "89%%" sleep 1 tput cub 3 tput el printf "90%%\n" ![]() Uses ␛[0E to move to the beginning of the current line, and ␛[K to clear to the end of the line (assuming your terminal supports those sequences). Uses ␛[3D to move three characters to the left, and writes over them (assuming your printf supports \e) printf "89%% hello" sleep 1 printf "\e[0E\e[K90%%\n" For example printf "89%%" sleep 1 printf "\e[3D90%%\n" There are terminal escapes which will allow you to move around and clear parts of the screen, the CSI sequences, but they are terminal-dependent (although in practice VT100 escapes are supported everywhere now). printf "-%%\n" 1 gives a leading space). Simple options are adding spaces to the end, or making the output fixed-width (e.g. Note that this doesn’t clear the line, so you need to take care of that if necessary. The typical way of doing this is not to erase a single character, but to go back to the start of the line using a carriage return ( \r): printf "89%%" sleep 1 printf "\r90%%\n" Otherwise I look for an approach to do the same in other ways always using bash and php scripts (please include actual working examples at least with a debian9 console and php7) I would get the new string printed at the exact position of the previous one without have the screen full of lines "\033[01 31m") that could be echoed with bash or php echo and tells the console "remove the last previous printed character."ĭoing so by using something like: (php example) echo str_repeat($neg_character, strlen($last_percentage_update_string)) What I would like is to learn if there's a special character combination (eg. What I hate is getting the screen full of percent updates. In my simple scripts I often have the need of print a percentage of what's being done with my commands. In bash you can cast a command named clear to clear all the screen commands.Īnd with echo you can print whatever you want onscreen. ![]()
0 Comments
Leave a Reply. |