From c45e5ce48772f5f98e8fa24d3d4bd02b7f2c9219 Mon Sep 17 00:00:00 2001 From: peshwar9 Date: Tue, 15 Sep 2020 18:52:09 +0530 Subject: [PATCH] added chapter 7 --- .gitignore | 1 + chapter5/.DS_Store | Bin 6148 -> 6148 bytes chapter6/rstat/src/.DS_Store | Bin 0 -> 6148 bytes chapter7/tui/.DS_Store | Bin 0 -> 6148 bytes chapter7/tui/.gitignore | 1 + chapter7/tui/Cargo.lock | 47 +++++++ chapter7/tui/Cargo.toml | 8 ++ chapter7/tui/src/.DS_Store | Bin 0 -> 6148 bytes chapter7/tui/src/bin/mouse-events.rs | 44 +++++++ chapter7/tui/src/bin/text-viewer1.rs | 118 +++++++++++++++++ chapter7/tui/src/bin/text-viewer2.rs | 188 +++++++++++++++++++++++++++ 11 files changed, 407 insertions(+) create mode 100644 .gitignore create mode 100644 chapter6/rstat/src/.DS_Store create mode 100644 chapter7/tui/.DS_Store create mode 100644 chapter7/tui/.gitignore create mode 100644 chapter7/tui/Cargo.lock create mode 100644 chapter7/tui/Cargo.toml create mode 100644 chapter7/tui/src/.DS_Store create mode 100644 chapter7/tui/src/bin/mouse-events.rs create mode 100644 chapter7/tui/src/bin/text-viewer1.rs create mode 100644 chapter7/tui/src/bin/text-viewer2.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/chapter5/.DS_Store b/chapter5/.DS_Store index 8b3d777a2fde9966273b1bde2d3995be3045e4ca..87109d7c5628af989b2ee75ef8742f8c31b34cdc 100644 GIT binary patch delta 64 zcmZoMXfc=|#>CJ*F;Q%yo+2a5!~knXmdQMf#hXQ$KCy0AU=m~8%+A5j0aUVCkm)<~ UWPTAtMuy2YJkpzEL{=~Z03QAjGynhq delta 199 zcmZoMXfc=|#>B`mF;Q%yo+6MAu%0Zy*Z^eo6xXvclrSVR6fvYTq%xG`q#Fh&=jRqM zfPiVqMIebHpPTREl9ZF51Qh2u*P)a*HUE$!rkWIjY6>!7_OJrAn+I%WWMpC6%+A5j b0dyvi^PPDzzlb3RBT%vdh&OwP>|q7~Z&)u* diff --git a/chapter6/rstat/src/.DS_Store b/chapter6/rstat/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0`8dt64B0O_o(mj^z+o9iAc~cIpok=GT^0d3F z>Lbz*ZQJdJ4!OERib?SW>BLifB43GgbaPADCXR9=qjd9M@M)d$xDG=nqtIAUJiebyb?G% zdbuP!8T-V^DJGO;r^BBuT~cwRB?DyOl!2Kq7ux@CpnuH&r$t break, + Event::Mouse(m) => match m { + MouseEvent::Press(_, a, b) | MouseEvent::Release(a, b) | MouseEvent::Hold(a, b) => { + write!(stdout, "{}", cursor::Goto(a, b)).unwrap(); + let (x, y) = stdout.cursor_pos().unwrap(); + write!( + stdout, + "{}{}Cursor is at: ({},{}){}", + cursor::Goto(5, 5), + termion::clear::UntilNewline, + x, + y, + cursor::Goto(a, b) + ) + .unwrap(); + } + }, + _ => {} + } + + stdout.flush().unwrap(); + } +} diff --git a/chapter7/tui/src/bin/text-viewer1.rs b/chapter7/tui/src/bin/text-viewer1.rs new file mode 100644 index 0000000..6133322 --- /dev/null +++ b/chapter7/tui/src/bin/text-viewer1.rs @@ -0,0 +1,118 @@ +use std::env::args; +use std::fs; +use std::io::{stdin, stdout, Write}; +use termion::event::Key; +use termion::input::TermRead; +use termion::raw::IntoRawMode; +use termion::{color, style}; +struct Doc { + lines: Vec, +} +#[derive(Debug)] +struct Coordinates { + pub x: usize, + pub y: usize, +} +struct TextViewer { + doc: Doc, + doc_length: usize, + cur_pos: Coordinates, + terminal_size: Coordinates, + file_name: String, +} + +impl TextViewer { + fn init(file_name: &str) -> Self { + let mut doc_file = Doc { lines: vec![] }; + let file_handle = fs::read_to_string(file_name).unwrap(); + for doc_line in file_handle.lines() { + doc_file.lines.push(doc_line.to_string()); + } + let mut doc_length = file_handle.lines().count(); + if doc_length == 0 { + doc_file.lines.push("".into()); + doc_length += 1; + } + let size = termion::terminal_size().unwrap(); + Self { + doc: doc_file, + cur_pos: Coordinates { + x: 1, + y: doc_length, + }, + doc_length: doc_length, + terminal_size: Coordinates { + x: size.0 as usize, + y: size.1 as usize, + }, + file_name: file_name.into(), + } + } + + fn show_document(&mut self) { + let pos = &self.cur_pos; + let (old_x, old_y) = (pos.x, pos.y); + print!("{}{}", termion::clear::All, termion::cursor::Goto(1, 1)); + println!( + "{}{}Welcome to Super text viewer\r{}", + color::Bg(color::Black), + color::Fg(color::White), + style::Reset + ); + for line in 0..self.doc_length { + println!("{}\r", self.doc.lines[line as usize]); + } + + println!( + "{}", + termion::cursor::Goto(0, (self.terminal_size.y - 2) as u16), + ); + println!( + "{}{} line-count={} Filename: {}{}", + color::Fg(color::Red), + style::Bold, + self.doc_length, + self.file_name, + style::Reset + ); + self.set_pos(old_x, old_y); + } + fn set_pos(&mut self, x: usize, y: usize) { + self.cur_pos.x = x; + self.cur_pos.y = y; + println!( + "{}", + termion::cursor::Goto(self.cur_pos.x as u16, (self.cur_pos.y) as u16) + ); + } + + fn run(&mut self) { + let mut stdout = stdout().into_raw_mode().unwrap(); + let stdin = stdin(); + for c in stdin.keys() { + match c.unwrap() { + Key::Ctrl('q') => { + break; + } + _ => {} + } + stdout.flush().unwrap(); + } + } +} + +fn main() { + //Get arguments from command line + let args: Vec = args().collect(); + //Check if file exists. If not, print error message and exit process + if !std::path::Path::new(&args[1]).exists() { + println!("File does not exist"); + std::process::exit(0); + } + // Open file & load into struct + println!("{}", termion::cursor::Show); + // Initialize viewer + let mut viewer = TextViewer::init(&args[1]); + viewer.show_document(); + viewer.run(); +} diff --git a/chapter7/tui/src/bin/text-viewer2.rs b/chapter7/tui/src/bin/text-viewer2.rs new file mode 100644 index 0000000..ca5e68b --- /dev/null +++ b/chapter7/tui/src/bin/text-viewer2.rs @@ -0,0 +1,188 @@ +use std::env::args; +use std::fs; +use std::io::{stdin, stdout, Write}; +use termion::event::Key; +use termion::input::TermRead; +use termion::raw::IntoRawMode; +use termion::{color, style}; +struct TextViewer { + doc: Doc, + doc_length: usize, + cur_pos: Coordinates, + terminal_size: Coordinates, + file_name: String, +} +impl TextViewer { + fn init(file_name: &str) -> Self { + let mut doc_file = Doc { lines: vec![] }; + let file_handle = fs::read_to_string(file_name).unwrap(); + for doc_line in file_handle.lines() { + doc_file.lines.push(doc_line.to_string()); + } + let mut doc_length = file_handle.lines().count(); + if doc_length == 0 { + doc_file.lines.push("".into()); + doc_length += 1; + } + let size = termion::terminal_size().unwrap(); + Self { + doc: doc_file, + cur_pos: Coordinates { + x: 1, + y: doc_length, + }, + doc_length: doc_length, + terminal_size: Coordinates { + x: size.0 as usize, + y: size.1 as usize, + }, + file_name: file_name.into(), + } + } + + fn show_document(&mut self) { + let pos = &self.cur_pos; + let (old_x, old_y) = (pos.x, pos.y); + print!("{}{}", termion::clear::All, termion::cursor::Goto(1, 1)); + println!( + "{}{}Welcome to Super text viewer\r{}", + color::Bg(color::Black), + color::Fg(color::White), + style::Reset + ); + if self.doc_length < self.terminal_size.y { + for line in 0..self.doc_length { + println!("{}\r", self.doc.lines[line as usize]); + } + } else { + if pos.y <= self.terminal_size.y { + for line in 0..self.terminal_size.y - 3 { + println!("{}\r", self.doc.lines[line as usize]); + } + } else { + for line in pos.y - (self.terminal_size.y - 3)..pos.y { + println!("{}\r", self.doc.lines[line as usize]); + } + } + } + println!( + "{}", + termion::cursor::Goto(0, (self.terminal_size.y - 2) as u16), + ); + println!( + "{}X={},Y={}, line-count={} Filename: {}{}", + color::Fg(color::Red), + old_x, + old_y, + self.doc_length, + self.file_name, + style::Reset + ); + self.set_pos(old_x, old_y); + } + fn set_pos(&mut self, x: usize, y: usize) { + self.cur_pos.x = x; + self.cur_pos.y = y; + println!( + "{}", + termion::cursor::Goto(self.cur_pos.x as u16, (self.cur_pos.y) as u16) + ); + } + fn inc_x(&mut self) { + if self.cur_pos.x < self.terminal_size.x { + self.cur_pos.x += 1; + } + println!( + "{}", + termion::cursor::Goto(self.cur_pos.x as u16, self.cur_pos.y as u16) + ); + } + fn dec_x(&mut self) { + if self.cur_pos.x > 1 { + self.cur_pos.x -= 1; + } + println!( + "{}", + termion::cursor::Goto(self.cur_pos.x as u16, self.cur_pos.y as u16) + ); + } + fn inc_y(&mut self) { + if self.cur_pos.y < self.doc_length { + self.cur_pos.y += 1; + } + + println!( + "{}", + termion::cursor::Goto(self.cur_pos.x as u16, self.cur_pos.y as u16) + ); + } + fn dec_y(&mut self) { + if self.cur_pos.y > 1 { + self.cur_pos.y -= 1; + } + println!( + "{}", + termion::cursor::Goto(self.cur_pos.x as u16, self.cur_pos.y as u16) + ); + } + + fn run(&mut self) { + let mut stdout = stdout().into_raw_mode().unwrap(); + let stdin = stdin(); + for c in stdin.keys() { + match c.unwrap() { + Key::Ctrl('q') => { + break; + } + Key::Left => { + self.dec_x(); + self.show_document(); + } + Key::Right => { + self.inc_x(); + self.show_document(); + } + Key::Up => { + self.dec_y(); + self.show_document(); + } + Key::Down => { + self.inc_y(); + self.show_document(); + } + Key::Backspace => { + self.dec_x(); + } + _ => {} + } + stdout.flush().unwrap(); + } + } +} +struct Doc { + lines: Vec, +} +#[derive(Debug)] +struct Coordinates { + pub x: usize, + pub y: usize, +} +fn main() { + //Get arguments from command line + let args: Vec = args().collect(); + //Check if file exists. If not, print error message and exit process + if !std::path::Path::new(&args[1]).exists() { + println!("File does not exist"); + std::process::exit(0); + } + // Clear full screen + println!("{}", termion::clear::All); + // Open file & load into struct + println!("{}", termion::cursor::Show); + println!("{}", termion::cursor::Goto(1, 1)); + // Initialize editor + let mut editor = TextViewer::init(&args[1]); + editor.show_document(); + editor.set_pos(1, 1); + editor.run(); +}