mirror of
https://github.com/dankamongmen/notcurses
synced 2025-03-10 09:39:03 -04:00
rust: refactor examples & palette_size method
- refactor Notcurses.palette_size() to return an NcResult. - refactor & rename example direct-text.rs to direct-capabilities.rs. - new example full-capabilities.rs
This commit is contained in:
parent
1e274287f6
commit
435b8cb6a7
22
rust/examples/direct-capabilities.rs
Normal file
22
rust/examples/direct-capabilities.rs
Normal file
@ -0,0 +1,22 @@
|
||||
use libnotcurses_sys::*;
|
||||
|
||||
fn main() -> NcResult<()> {
|
||||
let mut dm = DirectMode::new()?;
|
||||
|
||||
let (t_rows, t_cols) = dm.dim_yx();
|
||||
println!("Terminal rows={0}, cols={1}", t_rows, t_cols);
|
||||
|
||||
println!(
|
||||
"Can display UTF-8: {0}
|
||||
Can open images: {1}
|
||||
Supports Pixels: {2:?}
|
||||
Palette size: {3:?}
|
||||
",
|
||||
dm.canutf8(),
|
||||
dm.canopen_images(),
|
||||
dm.check_pixel_support(),
|
||||
dm.palette_size(),
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
@ -1,110 +0,0 @@
|
||||
#![allow(unused_imports)]
|
||||
|
||||
use libnotcurses_sys::*;
|
||||
|
||||
fn main() -> NcResult<()> {
|
||||
let mut dm = DirectMode::new()?;
|
||||
|
||||
// INFO
|
||||
|
||||
let t_rows = dm.dim_y();
|
||||
let t_cols = dm.dim_x();
|
||||
println!("Terminal rows={0}, cols={1}", t_rows, t_cols);
|
||||
|
||||
println!(
|
||||
"Can open images: {0}\nCan UTF-8: {1}\nSupports Pixels: {2:?}",
|
||||
dm.canopen_images(),
|
||||
dm.canutf8(),
|
||||
dm.check_pixel_support()?,
|
||||
);
|
||||
|
||||
println!("palette_size: {}", dm.palette_size()?);
|
||||
|
||||
// TEXT & STYLE
|
||||
|
||||
// let stylesv = vec![
|
||||
// ("[DIM]", Style::Dim),
|
||||
// ("[UNDERLINE]", Style::Underline),
|
||||
// ("[ITALIC]", Style::Italic),
|
||||
// ("[BOLD]", Style::Bold),
|
||||
// ("[STRUCK]", Style::Struck),
|
||||
// ("[REVERSE]", Style::Reverse),
|
||||
// ("[BLINK]", Style::Blink),
|
||||
// ("[INVIS]", Style::Invis),
|
||||
// ("[PROTECT]", Style::Protect),
|
||||
// ("[STANDOUT]", Style::Standout),
|
||||
// ];
|
||||
//
|
||||
// dm.print_colored(0, "\nSingle styles:\n")?;
|
||||
//
|
||||
// dm.print_colored(0, "[DEFAULT]")?;
|
||||
// for (label, style) in stylesv.iter() {
|
||||
// dm.styles_on(*style)?;
|
||||
// dm.print_colored(0, label)?;
|
||||
// dm.styles_off(*style)?;
|
||||
// }
|
||||
//
|
||||
// dm.print_colored(0, "\nJoint styles:\n")?;
|
||||
//
|
||||
// dm.print_colored(0, "[DEFAULT ")?;
|
||||
// for (label, style) in stylesv.iter() {
|
||||
// dm.styles_on(*style)?;
|
||||
// dm.print_colored(
|
||||
// 0,
|
||||
// &label
|
||||
// .chars()
|
||||
// .map(|c| match c {
|
||||
// '[' | ']' => ' ',
|
||||
// _ => c,
|
||||
// })
|
||||
// .collect::<String>(),
|
||||
// )?;
|
||||
// if let Style::Blink = style {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// dm.styles_off_all()?;
|
||||
// dm.print_colored(0, "]")?;
|
||||
//
|
||||
// // TEXT mixing Rust's print!() & println!() and notcurses' print_colored() & print()
|
||||
// //
|
||||
// dm.print_colored(0, "\n\n1")?;
|
||||
// println!("2 < instead of printing this concatenated AFTER, it appears BEFORE 1");
|
||||
//
|
||||
// dm.print_colored(0, "\n\n1 \n")?;
|
||||
// println!("2 < it does work (better) with a `\\n` after 1");
|
||||
//
|
||||
// // TODO: more tests with styles_set & bold+italic
|
||||
// //
|
||||
// //dm.styles_off(Style::Bold)?;
|
||||
// //dm.styles_on(Style::Italic)?;
|
||||
//
|
||||
// // COLORS & TEXT (WIP)
|
||||
//
|
||||
// dm.bg(0x00FF00 as u32)?; // FIXME: colors don't seem to work
|
||||
// dm.fg(0xFF0000 as u32)?;
|
||||
// println!("\nhello colors? (investigate)");
|
||||
// dm.print_colored(
|
||||
// sys::channels_combine(0xFF008800, 0xFFBB0099),
|
||||
// "hello colors 2",
|
||||
// )?;
|
||||
// dm.print_colored(0, "...")?;
|
||||
|
||||
// TODO: should be able to use print!() & println!()
|
||||
// dm.clear()?;
|
||||
// dm.print_aligned(0, Align::Center, "PRINTED")?;
|
||||
// dm.print_aligned(40, Align::Left, "PRINTED")?;
|
||||
// dm.print_aligned(5, Align::Right, "PRINTED")?;
|
||||
|
||||
// WIP----------------------- ↓
|
||||
|
||||
// CURSOR & TEXT
|
||||
|
||||
// println!("Cursor position: {:?}", dm.cursor_yx()?);
|
||||
// dm.cursor_move_yx(200,100)?;
|
||||
// dm.cursor_move_yx(yx.0, yx.1)?;
|
||||
// dm.cursor_disable()?;
|
||||
// dm.cursor_enable()?;
|
||||
|
||||
Ok(())
|
||||
}
|
30
rust/examples/full-capabilities.rs
Normal file
30
rust/examples/full-capabilities.rs
Normal file
@ -0,0 +1,30 @@
|
||||
use libnotcurses_sys::*;
|
||||
|
||||
fn main() -> NcResult<()> {
|
||||
let mut nc = FullMode::new()?;
|
||||
|
||||
let (t_rows, t_cols) = nc.term_dim_yx();
|
||||
println!("Terminal rows={0}, cols={1}", t_rows, t_cols);
|
||||
|
||||
println!(
|
||||
"Can display UTF-8: {0}
|
||||
Can display sextant characters: {1}
|
||||
Can open images: {2}
|
||||
Can open videos: {3}
|
||||
Supports Pixels: {4:?}
|
||||
Supports True Color: {5}
|
||||
Palette size: {6:?}
|
||||
",
|
||||
nc.canutf8(),
|
||||
nc.cansextant(),
|
||||
nc.canopen_images(),
|
||||
nc.canopen_videos(),
|
||||
nc.check_pixel_support(),
|
||||
nc.cantruecolor(),
|
||||
nc.palette_size(),
|
||||
);
|
||||
|
||||
println!("Done. Waiting for 10 seconds. . .");
|
||||
rsleep![&mut nc, 10];
|
||||
Ok(())
|
||||
}
|
@ -159,7 +159,8 @@ impl NcDirect {
|
||||
]
|
||||
}
|
||||
|
||||
/// Returns the number of simultaneous colors claimed to be supported.
|
||||
/// Returns the number of simultaneous colors claimed to be supported,
|
||||
/// if there is color support.
|
||||
///
|
||||
/// Note that several terminal emulators advertise more colors than they
|
||||
/// actually support, downsampling internally.
|
||||
|
@ -456,14 +456,21 @@ impl Notcurses {
|
||||
}
|
||||
|
||||
/// Returns the number of simultaneous colors claimed to be supported,
|
||||
/// or 1 if there is no color support.
|
||||
/// if there is color support.
|
||||
///
|
||||
/// Note that several terminal emulators advertise more colors than they
|
||||
/// actually support, downsampling internally.
|
||||
///
|
||||
/// *C style function: [notcurses_palette_size()][crate::notcurses_palette_size].*
|
||||
pub fn palette_size(&mut self) -> u32 {
|
||||
unsafe { crate::notcurses_palette_size(self) }
|
||||
pub fn palette_size(&mut self) -> NcResult<u32> {
|
||||
let res = unsafe { crate::notcurses_palette_size(self) };
|
||||
if res == 1 {
|
||||
return Err(NcError::with_msg(
|
||||
1,
|
||||
"No color support ← Notcurses.palette_size()",
|
||||
));
|
||||
}
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
/// Refreshes the physical screen to match what was last rendered (i.e.,
|
||||
|
Loading…
x
Reference in New Issue
Block a user