繁体   English   中英

如何在 Rust 中显示带有 ncurses 的字符?

[英]How do you display a character with ncurses in Rust?

我试图通过简单地在终端中添加一个字符

use ncurses::*;
fn main()
{
    initscr();
    addch('#');
    endwin();
}

但我收到以下错误:

error[E0308]: mismatched types
  --> src/main.rs:15:11
   |
15 |     addch('#');
   |           ^^^ expected `u32`, found `char`

我检查了文档,它说它需要一个chtype ,所以我认为这只是一个字符? 我不确定我应该如何将字符更改为u32 charchtype有什么区别?

通过查看定义为u64 (对于 64 位平台)和u32 (对于 32 位平台)的别名的源代码chtype

#[cfg(feature="wide_chtype")]
pub type chtype = u64;
#[cfg(not(feature="wide_chtype"))]
pub type chtype = u32;

为了解决这个错误,你可以输入 cast # to chtype

fn main()
{
    initscr();
    addch('#' as chtype);
    endwin();
}

chtype包含多个字符,如 ncurses手册页中所述:

      ncurses
           the "normal" library,  which  handles  8-bit  characters.   The
           normal   (8-bit)   library   stores  characters  combined  with
           attributes in chtype data.

           Attributes alone (no corresponding character) may be stored  in
           chtype or the equivalent attr_t data.  In either case, the data
           is stored in something like an integer.

有关更多信息, waddch手册页详细说明:

   Video attributes can be combined with a character  argument  passed  to
   addch  or  related  functions by logical-ORing them into the character.
   (Thus, text, including attributes, can be  copied  from  one  place  to
   another  using  inch(3x)  and  addch.)   See the curs_attr(3x) page for
   values of predefined video attribute constants  that  can  be  usefully
   OR'ed into characters.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM