2019-03-12 20:35:23 +02:00
/*
2020-02-04 15:52:12 +02:00
* meli
2019-03-12 20:35:23 +02:00
*
* Copyright 2017 - 2018 Manos Pitsidianakis
*
* This file is part of meli .
*
* meli is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* meli is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with meli . If not , see < http ://www.gnu.org/licenses/>.
* /
2020-02-04 15:52:12 +02:00
2023-06-16 20:20:12 +03:00
//! Terminal grid cells, keys, colors, etc.
2023-08-11 11:00:59 +03:00
2023-06-16 20:20:12 +03:00
use serde ::{ de , de ::Visitor , Deserialize , Deserializer } ;
2019-06-18 21:13:58 +03:00
2023-08-11 11:00:59 +03:00
mod braille ;
2020-11-23 00:35:27 +02:00
mod color ;
2023-08-11 11:00:59 +03:00
mod screen ;
2023-06-16 20:20:12 +03:00
pub use color ::* ;
2019-03-12 20:35:23 +02:00
#[ macro_use ]
2023-07-20 00:19:42 +03:00
pub mod position ;
2019-03-12 20:35:23 +02:00
#[ macro_use ]
2023-07-20 00:19:42 +03:00
pub mod cells ;
2019-03-12 20:35:23 +02:00
#[ macro_use ]
2023-07-20 00:19:42 +03:00
pub mod keys ;
2019-11-05 08:32:27 +02:00
pub mod embed ;
2023-07-20 00:19:42 +03:00
pub mod text_editing ;
2023-04-30 19:39:41 +03:00
2023-09-09 12:24:07 +03:00
use std ::io ::{ BufRead , Write } ;
2023-08-11 11:00:59 +03:00
pub use braille ::BraillePixelIter ;
2023-10-23 13:56:13 +03:00
pub use screen ::{ Area , Screen , ScreenGeneration , StateStdout , Tty , Virtual } ;
2023-08-11 11:00:59 +03:00
2023-04-30 19:39:41 +03:00
pub use self ::{ cells ::* , keys ::* , position ::* , text_editing ::* } ;
2023-08-11 11:00:59 +03:00
2019-10-15 22:55:53 +03:00
/*
* CSI events we use
* /
2023-08-11 11:00:59 +03:00
pub const BRACKET_PASTE_START : & [ u8 ] = b " \x1B [200~ " ;
pub const BRACKET_PASTE_END : & [ u8 ] = b " \x1B [201~ " ;
2019-10-15 22:55:53 +03:00
// Some macros taken from termion:
/// Create a CSI-introduced sequence.
macro_rules ! csi {
( $( $l :expr ) , * ) = > { concat! ( " \x1b [ " , $( $l ) , * ) } ;
}
/// Derive a CSI sequence struct.
macro_rules ! derive_csi_sequence {
( $( #[ $outer:meta ] ) *
( $name :ident , $value :expr ) ) = > {
$( #[ $outer ] ) *
#[ derive(Copy, Clone) ]
pub struct $name ;
2023-08-11 13:16:47 +03:00
impl std ::fmt ::Display for $name {
fn fmt ( & self , f : & mut std ::fmt ::Formatter ) -> std ::fmt ::Result {
2019-10-15 22:55:53 +03:00
write! ( f , csi! ( $value ) )
}
}
impl AsRef < [ u8 ] > for $name {
fn as_ref ( & self ) -> & 'static [ u8 ] {
csi! ( $value ) . as_bytes ( )
}
}
impl AsRef < str > for $name {
fn as_ref ( & self ) -> & 'static str {
csi! ( $value )
}
}
} ;
}
derive_csi_sequence! (
2020-10-14 12:50:38 +03:00
///Ps = 1 0 0 2 ⇒ Don't use Cell Motion Mouse Tracking, xterm
( DisableMouse , " ?1002l " )
) ;
derive_csi_sequence! (
///Ps = 1 0 0 2 ⇒ Use Cell Motion Mouse Tracking, xterm
( EnableMouse , " ?1002h " )
2019-10-15 22:55:53 +03:00
) ;
2020-10-14 12:50:38 +03:00
derive_csi_sequence! (
///Ps = 1 0 0 6 Enable SGR Mouse Mode, xterm.
( EnableSGRMouse , " ?1006h " )
) ;
2019-10-15 22:55:53 +03:00
derive_csi_sequence! (
2020-10-14 12:50:38 +03:00
///Ps = 1 0 0 6 Disable SGR Mouse Mode, xterm.
( DisableSGRMouse , " ?1006l " )
2019-10-15 22:55:53 +03:00
) ;
derive_csi_sequence! (
2023-04-30 19:39:41 +03:00
#[ doc = " `CSI Ps ; Ps ; Ps t`, where `Ps = 2 2 ; 0` -> Save xterm icon and window title on \
stack . " ]
2019-10-15 22:55:53 +03:00
( SaveWindowTitleIconToStack , " 22;0t " )
) ;
derive_csi_sequence! (
2023-04-30 19:39:41 +03:00
#[ doc = " Restore window title and icon from terminal's title stack. `CSI Ps ; Ps ; Ps t`, \
where ` Ps = 2 3 ; 0 ` -> Restore xterm icon and window title from stack . " ]
2019-10-15 22:55:53 +03:00
( RestoreWindowTitleIconFromStack , " 23;0t " )
) ;
derive_csi_sequence! (
#[ doc = " Empty struct with a Display implementation that returns the byte sequence to start [Bracketed Paste Mode](http://www.xfree86.org/current/ctlseqs.html#Bracketed%20Paste%20Mode) " ]
( BracketModeStart , " ?2004h " )
) ;
derive_csi_sequence! (
#[ doc = " Empty struct with a Display implementation that returns the byte sequence to end [Bracketed Paste Mode](http://www.xfree86.org/current/ctlseqs.html#Bracketed%20Paste%20Mode) " ]
2019-11-19 23:41:12 +02:00
( BracketModeEnd , " ?2004l " )
2019-10-15 22:55:53 +03:00
) ;
2023-09-09 12:24:07 +03:00
pub struct Ask {
pub message : String ,
}
impl Ask {
pub fn run ( self ) -> bool {
let mut buffer = String ::new ( ) ;
let stdin = std ::io ::stdin ( ) ;
let mut handle = stdin . lock ( ) ;
print! ( " {} [Y/n] " , & self . message ) ;
let _ = std ::io ::stdout ( ) . flush ( ) ;
loop {
buffer . clear ( ) ;
handle
. read_line ( & mut buffer )
. expect ( " Could not read from stdin. " ) ;
match buffer . trim ( ) {
" " | " Y " | " y " | " yes " | " YES " | " Yes " = > {
return true ;
}
" n " | " N " | " no " | " No " | " NO " = > {
return false ;
}
_ = > {
print! ( " \n {} [Y/n] " , & self . message ) ;
let _ = std ::io ::stdout ( ) . flush ( ) ;
}
}
}
}
}