Skip to content

Commit

Permalink
Extended NCApplication::runInTerminal for RSpec (boo#937026)
Browse files Browse the repository at this point in the history
Actually RSpec is just an example.
runInTerminal("#open_ncurses") paired with
runInTerminal("#close_ncurses") will do just the respective halves,
allowing non-ncurses code access stdout afterwards.
  • Loading branch information
mvidner committed Jul 3, 2015
1 parent 8f861f5 commit a6eb95a
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions src/NCApplication.cc
Expand Up @@ -169,12 +169,9 @@ NCApplication::setConsoleFont( const std::string & console_magic,
language );
}


int
NCApplication::runInTerminal( const std::string & cmd )
{
int ret;

static
void
close_ncurses() {
// Save tty modes and end ncurses mode temporarily
::def_prog_mode();
::endwin();
Expand All @@ -183,22 +180,44 @@ NCApplication::runInTerminal( const std::string & cmd )
// via system() can use them and draw something to the terminal
dup2( YNCursesUI::ui()->stdout_save, 1 );
dup2( YNCursesUI::ui()->stderr_save, 2 );
}

// Call external program
ret = system( cmd.c_str() );

if ( ret != 0 )
{
yuiError() << cmd << " returned:" << ret << std::endl;
}

static
void
open_ncurses() {
// Redirect stdout and stderr to y2log again
YNCursesUI::ui()->RedirectToLog();

// Resume tty modes and refresh the screen
::reset_prog_mode();

::refresh();
}

int
NCApplication::runInTerminal( const std::string & cmd )
{
int ret = 0;

if (cmd == "#open_ncurses") {
open_ncurses();
}
else if (cmd == "#close_ncurses") {
close_ncurses();
}
else {
close_ncurses();

// Call external program
ret = system( cmd.c_str() );

if ( ret != 0 )
{
yuiError() << cmd << " returned:" << ret << std::endl;
}

open_ncurses();
}

return ret;
}
Expand Down

1 comment on commit a6eb95a

@shundhammer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Please sign in to comment.