2011-07-04 35 views
4

使用n/curses打印到終端窗口的右側和/或底側的標準方式是什麼?使用(n)詛咒打印到終端的右側或底側

這裏有一個小素描:用C或Python

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













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

解決方案的罰款。

謝謝!

回答

1

我會去用:

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

這有點即興,這是我做了我的大部分詛咒的編程。

1

有2種方式,我知道,但唯一一個我敢肯定的:

之一:從ncurses庫 move(int row, int col)。但是,如果您要在此聲明後執行一些I/O操作,則可以使用相應的「mv」函數進行更改。例如,

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

可以通過

mvaddch(y, x, ch); 

注意更換:我只有這個頭,但還沒有測試它自己。

二:

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

我敢肯定,這一個工程。

祝你好運!

+2

你的第二個不是_using_ ncurses,它是_bypassing_ ncurses。 – ninjalj

+0

@ninjalj它可能有點微妙,但這就是它在#2上市的原因。 – Ram

+1

你會如何計算x和y? –

0

我寫了這一點代碼來自動解決這個問題。調用這個而不是scr.addstr(),它會照顧正確的addstr/insstr命令來使其工作。

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)