Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server : erase previous stdout text for multi-row outputs #2406

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions examples/stream/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
#include <thread>
#include <vector>
#include <fstream>
#if defined(_WIN32)
# define WIN32_LEAN_AND_MEAN
# define VC_EXTRALEAN
# include <Windows.h>
#else
# include <sys/ioctl.h>
# include <unistd.h>
#endif


// command-line parameters
Expand Down Expand Up @@ -112,6 +120,18 @@ void whisper_print_usage(int /*argc*/, char ** argv, const whisper_params & para
fprintf(stderr, "\n");
}

static void get_terminal_width(int& width) {
#if defined(_WIN32)
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
width = (int)(csbi.srWindow.Right-csbi.srWindow.Left+1);
#else
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
width = (int)(w.ws_col);
#endif
}

int main(int argc, char ** argv) {
whisper_params params;

Expand Down Expand Up @@ -224,6 +244,8 @@ int main(int argc, char ** argv) {

auto t_last = std::chrono::high_resolution_clock::now();
const auto t_start = t_last;
int terminal_width = 80;
int current_text_length = 0;

// main audio loop
while (is_running) {
Expand Down Expand Up @@ -330,11 +352,14 @@ int main(int argc, char ** argv) {
{
if (!use_vad) {
printf("\33[2K\r");

// print long empty line to clear the previous line
printf("%s", std::string(100, ' ').c_str());

printf("\33[2K\r");
// also erase text that is longer than one row/terminal width
get_terminal_width(terminal_width);
for (int j = 1; j*terminal_width < current_text_length; j++) {
printf("\33[A\r");
printf("\33[2K\r");
}
fflush(stdout);
current_text_length = 0;
} else {
const int64_t t1 = (t_last - t_start).count()/1000000;
const int64_t t0 = std::max(0.0, t1 - pcmf32.size()*1000.0/WHISPER_SAMPLE_RATE);
Expand All @@ -351,6 +376,7 @@ int main(int argc, char ** argv) {
if (params.no_timestamps) {
printf("%s", text);
fflush(stdout);
current_text_length += strlen(text);

if (params.fname_out.length() > 0) {
fout << text;
Expand Down Expand Up @@ -390,6 +416,7 @@ int main(int argc, char ** argv) {

if (!use_vad && (n_iter % n_new_line) == 0) {
printf("\n");
current_text_length = 0;

// keep part of the audio for next iteration to try to mitigate word boundary issues
pcmf32_old = std::vector<float>(pcmf32.end() - n_samples_keep, pcmf32.end());
Expand Down