pub use sys::position;
#[cfg(windows)]
use crate::Result;
use crate::{impl_display, Ansi, Command};
use std::fmt;
mod ansi;
pub(crate) mod sys;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MoveTo(pub u16, pub u16);
impl fmt::Display for Ansi<MoveTo> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
ansi::move_to_csi_sequence(f, (self.0).0, (self.0).1)
}
}
impl Command for MoveTo {
type AnsiType = Ansi<Self>;
#[inline]
fn ansi_code(&self) -> Self::AnsiType {
Ansi(*self)
}
#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
sys::move_to(self.0, self.1)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MoveToNextLine(pub u16);
impl fmt::Display for Ansi<MoveToNextLine> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
ansi::move_to_next_line_csi_sequence(f, (self.0).0)
}
}
impl Command for MoveToNextLine {
type AnsiType = Ansi<Self>;
#[inline]
fn ansi_code(&self) -> Self::AnsiType {
Ansi(*self)
}
#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
sys::move_to_next_line(self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MoveToPreviousLine(pub u16);
impl fmt::Display for Ansi<MoveToPreviousLine> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
ansi::move_to_previous_line_csi_sequence(f, (self.0).0)
}
}
impl Command for MoveToPreviousLine {
type AnsiType = Ansi<Self>;
#[inline]
fn ansi_code(&self) -> Self::AnsiType {
Ansi(*self)
}
#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
sys::move_to_previous_line(self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MoveToColumn(pub u16);
impl fmt::Display for Ansi<MoveToColumn> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
ansi::move_to_column_csi_sequence(f, (self.0).0)
}
}
impl Command for MoveToColumn {
type AnsiType = Ansi<Self>;
#[inline]
fn ansi_code(&self) -> Self::AnsiType {
Ansi(*self)
}
#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
sys::move_to_column(self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MoveUp(pub u16);
impl fmt::Display for Ansi<MoveUp> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
ansi::move_up_csi_sequence(f, (self.0).0)
}
}
impl Command for MoveUp {
type AnsiType = Ansi<Self>;
#[inline]
fn ansi_code(&self) -> Self::AnsiType {
Ansi(*self)
}
#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
sys::move_up(self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MoveRight(pub u16);
impl fmt::Display for Ansi<MoveRight> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
ansi::move_right_csi_sequence(f, (self.0).0)
}
}
impl Command for MoveRight {
type AnsiType = Ansi<Self>;
#[inline]
fn ansi_code(&self) -> Self::AnsiType {
Ansi(*self)
}
#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
sys::move_right(self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MoveDown(pub u16);
impl fmt::Display for Ansi<MoveDown> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
ansi::move_down_csi_sequence(f, (self.0).0)
}
}
impl Command for MoveDown {
type AnsiType = Ansi<Self>;
#[inline]
fn ansi_code(&self) -> Self::AnsiType {
Ansi(*self)
}
#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
sys::move_down(self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MoveLeft(pub u16);
impl fmt::Display for Ansi<MoveLeft> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
ansi::move_left_csi_sequence(f, (self.0).0)
}
}
impl Command for MoveLeft {
type AnsiType = Ansi<Self>;
#[inline]
fn ansi_code(&self) -> Self::AnsiType {
Ansi(*self)
}
#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
sys::move_left(self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SavePosition;
impl Command for SavePosition {
type AnsiType = &'static str;
#[inline]
fn ansi_code(&self) -> Self::AnsiType {
ansi::SAVE_POSITION_CSI_SEQUENCE
}
#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
sys::save_position()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RestorePosition;
impl Command for RestorePosition {
type AnsiType = &'static str;
#[inline]
fn ansi_code(&self) -> Self::AnsiType {
ansi::RESTORE_POSITION_CSI_SEQUENCE
}
#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
sys::restore_position()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Hide;
impl Command for Hide {
type AnsiType = &'static str;
#[inline]
fn ansi_code(&self) -> Self::AnsiType {
ansi::HIDE_CSI_SEQUENCE
}
#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
sys::show_cursor(false)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Show;
impl Command for Show {
type AnsiType = &'static str;
#[inline]
fn ansi_code(&self) -> Self::AnsiType {
ansi::SHOW_CSI_SEQUENCE
}
#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
sys::show_cursor(true)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EnableBlinking;
impl Command for EnableBlinking {
type AnsiType = &'static str;
#[inline]
fn ansi_code(&self) -> Self::AnsiType {
ansi::ENABLE_BLINKING_CSI_SEQUENCE
}
#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DisableBlinking;
impl Command for DisableBlinking {
type AnsiType = &'static str;
#[inline]
fn ansi_code(&self) -> Self::AnsiType {
ansi::DISABLE_BLINKING_CSI_SEQUENCE
}
#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
Ok(())
}
}
impl_display!(for MoveTo);
impl_display!(for MoveToColumn);
impl_display!(for MoveToNextLine);
impl_display!(for MoveToPreviousLine);
impl_display!(for MoveUp);
impl_display!(for MoveDown);
impl_display!(for MoveLeft);
impl_display!(for MoveRight);
impl_display!(for SavePosition);
impl_display!(for RestorePosition);
impl_display!(for Hide);
impl_display!(for Show);
impl_display!(for EnableBlinking);
impl_display!(for DisableBlinking);
#[cfg(test)]
mod tests {
use std::io::{self, stdout, Write};
use crate::execute;
use super::{
position, MoveDown, MoveLeft, MoveRight, MoveTo, MoveUp, RestorePosition, SavePosition,
};
#[test]
#[ignore]
fn test_move_to() {
let (saved_x, saved_y) = position().unwrap();
execute!(stdout(), MoveTo(saved_x + 1, saved_y + 1)).unwrap();
assert_eq!(position().unwrap(), (saved_x + 1, saved_y + 1));
execute!(stdout(), MoveTo(saved_x, saved_y)).unwrap();
assert_eq!(position().unwrap(), (saved_x, saved_y));
}
#[test]
#[ignore]
fn test_move_right() {
let (saved_x, saved_y) = position().unwrap();
execute!(io::stdout(), MoveRight(1)).unwrap();
assert_eq!(position().unwrap(), (saved_x + 1, saved_y));
}
#[test]
#[ignore]
fn test_move_left() {
execute!(stdout(), MoveTo(2, 0), MoveLeft(2)).unwrap();
assert_eq!(position().unwrap(), (0, 0));
}
#[test]
#[ignore]
fn test_move_up() {
execute!(stdout(), MoveTo(0, 2), MoveUp(2)).unwrap();
assert_eq!(position().unwrap(), (0, 0));
}
#[test]
#[ignore]
fn test_move_down() {
execute!(stdout(), MoveTo(0, 0), MoveDown(2)).unwrap();
assert_eq!(position().unwrap(), (0, 2));
}
#[test]
#[ignore]
fn test_save_restore_position() {
let (saved_x, saved_y) = position().unwrap();
execute!(
stdout(),
SavePosition,
MoveTo(saved_x + 1, saved_y + 1),
RestorePosition
)
.unwrap();
let (x, y) = position().unwrap();
assert_eq!(x, saved_x);
assert_eq!(y, saved_y);
}
}