0

I am trying to dump the output of curses window into a file without displaying it on stdout. Currently I am using addstr() function to print on stdout and at the end I am calling instr() function to dump the entire screen into a file. In certain cases, the ncurses does not work properly on xterm and hence I need to redirect the output to a file without actually printing it on stdout. I thought of using logger module but I lose color coding which addstr() provides. What is the best method to achieve this?

For example:

If I run the following command

$ python get_stats.py

it should display on stdout and when I run the command

$ python get_stats.py --dump-to-file

it should dump to a file without displaying on stdout. Does addstr() takes additional parameters to determine whether the output should go to a file or stdout?

rakesh
  • 135
  • 3
  • 15

1 Answers1

0

No, addstr does not take additional parameters. By default (using initscr, that is), curses writes to the standard output. You could simply do

python get_stats.py >myfile

But for making a program option, you would have to tell Python (or curses) to write to the given file. Python's binding for ncurses does not include newterm, which is the usual way of controlling the output stream. But in Python, you can redirect the standard output as discussed in these questions:

Because curses saves a copy of the output stream locally, you must do the redirection before calling initscr. Also, keep in mind that curses may use the file descriptor rather than the buffered output stream.

Community
  • 1
  • 1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • no problem (if you get stuck, posting the script here may get results) – Thomas Dickey Mar 09 '15 at 01:26
  • now I am using generic print function which takes handle and the content to be displayed. If the handle is for file, it will be written to file else on ncurses window – rakesh Apr 02 '15 at 01:41