开发者

printing to right side or bottom side of terminal using (n)curses

开发者 https://www.devze.com 2023-03-17 09:39 出处:网络
What\'s the standard way to print to the right side and/or bottom side of the terminal window using n/curses?

What's the standard way to print to the right side and/or bottom side of the terminal window using n/curses?

Here's a little 开发者_高级运维sketch:

Terminal window:
================================================================================
                                                                         [ MSG ]













message number 2                                                   here is more
================================================================================

Solutions in C or Python are fine.

Thanks!


I'd go with:

mvprintw(COLS-length("msg"),1,"msg");
mvprintw(0,LINES-1,"message number 2");
mvprintw(COLS-length("here is more"),LINES-1,"here is more");

That's kinda off the cuff, which is how I do most of my curses programming.


There are 2 ways that I know of, but only one I'm sure of:

ONE: move(int row, int col) from the ncurses library. But, if you are going to perform some I/O after this statement, you are metter off using the corresponding 'mv' function. For example,

move(y, x);
addch(ch);

can be replaced by

mvaddch(y, x, ch);

NOTE: I've only head of this but haven't tested it myself.

TWO:

printf("\033[%d;%df", y, x);
fflush(stdout);
printf("Hello, I will be placed at (x,y)\n");

I'm sure this one works.

Good Luck!


I wrote this bit of code to work around the problem automatically. Call this instead of scr.addstr() and it will take care of doing the right addstr/insstr commands to make it work.

def cwrite(scr, row, col, str, attr=0):
    max = scr.getmaxyx()
    if row < max[0] - 1:
        scr.addstr(row, col, str, attr)
    elif row == max[0] - 1:
        if len(str) + col >= max[1]:
            offset = max[1] - col - 2
            scr.addstr(row, col, str[:offset])
            scr.addstr(row, max[1] - 2, str[offset + 1], attr)
            scr.insstr(row, max[1] - 1, str[offset], attr)
        else:
            scr.addstr(row, col, str, attr)
0

精彩评论

暂无评论...
验证码 换一张
取 消