Permalink
Browse files

Update to new termion

  • Loading branch information...
jackpot51 committed Sep 19, 2017
1 parent d7bc422 commit 722de2c0ab3728d8bf7a9f778a37a0ae879d6662
Showing with 18 additions and 21 deletions.
  1. +6 −7 src/bin/less.rs
  2. +5 −6 src/bin/mdless.rs
  3. +3 −3 src/bin/resize.rs
  4. +4 −5 src/bin/watch.rs
View
@@ -6,10 +6,11 @@ extern crate termion;
use std::env::args;
use std::fs::File;
use std::io::{self, Write, Read, StdoutLock};
use std::io::{self, Write, Read};
use std::path::Path;
use extra::option::OptionalExt;
use termion::raw::IntoRawMode;
static LONG_HELP: &'static str = /* @MANSTART{less} */ r#"
NAME
@@ -51,30 +52,28 @@ COPYRIGHT
fn main() {
let mut args = args().skip(1).peekable();
let stdout = io::stdout();
let mut stdout = stdout.lock();
let stdin = io::stdin();
let mut stdin = stdin.lock();
let mut stderr = io::stderr();
if let Some(x) = args.peek() {
if x == "--help" || x == "-h" {
// Print help.
stdout.write(LONG_HELP.as_bytes()).try(&mut stderr);
io::stdout().write(LONG_HELP.as_bytes()).try(&mut stderr);
return;
}
} else {
let mut terminal = termion::get_tty().try(&mut stderr);
run("-", &mut stdin, &mut terminal, &mut stdout).try(&mut stderr);
run("-", &mut stdin, &mut terminal, io::stdout()).try(&mut stderr);
};
while let Some(filename) = args.next() {
let mut file = File::open(Path::new(filename.as_str())).try(&mut stderr);
run(filename.as_str(), &mut file, &mut stdin, &mut stdout).try(&mut stderr);
run(filename.as_str(), &mut file, &mut stdin, io::stdout()).try(&mut stderr);
}
}
fn run(path: &str, file: &mut Read, controls: &mut Read, stdout: &mut StdoutLock) -> std::io::Result<()> {
fn run<W: IntoRawMode>(path: &str, file: &mut Read, controls: &mut Read, stdout: W) -> std::io::Result<()> {
let mut string = String::new();
file.read_to_string(&mut string)?;
View
@@ -7,7 +7,7 @@ extern crate termion;
use std::env::args;
use std::fs::File;
use std::io::{self, Write, Read, StdoutLock};
use std::io::{self, Write, Read};
use std::path::{Path, PathBuf};
use std::str::Chars;
@@ -69,8 +69,7 @@ fn terminal_path() -> String {
fn main() {
let mut args = args().skip(1).peekable();
let stdout = io::stdout();
let mut stdout = stdout.lock();
let mut stdout = io::stdout();
let stdin = io::stdin();
let mut stdin = stdin.lock();
let mut stderr = io::stderr();
@@ -189,7 +188,7 @@ impl Block {
blocks
}
fn draw(&self, to: &mut RawTerminal<&mut StdoutLock>, path: &PathBuf, next: &mut Vec<PathBuf>, next_i: usize) -> std::io::Result<usize> {
fn draw<W: IntoRawMode>(&self, to: &mut RawTerminal<W>, path: &PathBuf, next: &mut Vec<PathBuf>, next_i: usize) -> std::io::Result<usize> {
let mut count = 0;
match *self {
@@ -290,7 +289,7 @@ impl Buffer {
Ok(res)
}
fn draw(&self, to: &mut RawTerminal<&mut StdoutLock>, path: &PathBuf, next: &mut Vec<PathBuf>, next_i: usize, w: u16, h: u16) -> std::io::Result<usize> {
fn draw<W: IntoRawMode>(&self, to: &mut RawTerminal<W>, path: &PathBuf, next: &mut Vec<PathBuf>, next_i: usize, w: u16, h: u16) -> std::io::Result<usize> {
write!(to, "{}{}{}", clear::All, style::Reset, cursor::Goto(1, 1))?;
let mut y = 0;
@@ -336,7 +335,7 @@ impl Buffer {
}
}
fn run(mut path: PathBuf, file: &mut Read, controls: &mut Read, stdout: &mut StdoutLock) -> std::io::Result<()> {
fn run<W: IntoRawMode>(mut path: PathBuf, file: &mut Read, controls: &mut Read, stdout: &mut W) -> std::io::Result<()> {
let mut stdout = stdout.into_raw_mode()?;
let (w, h) = {
View
@@ -9,8 +9,8 @@ use termion::raw::IntoRawMode;
fn main() {
let mut stderr = io::stderr();
let mut terminal = termion::get_tty().try(&mut stderr)
.into_raw_mode().try(&mut stderr);
let mut terminal = termion::get_tty().try(&mut stderr);
let mut terminal = terminal.into_raw_mode().try(&mut stderr);
write!(terminal, "{}", cursor::Save).unwrap();
terminal.flush().unwrap();
@@ -22,7 +22,7 @@ fn main() {
write!(terminal, "{}", cursor::Restore).unwrap();
terminal.flush().unwrap();
drop(terminal);
println!("export COLUMNS={};", w);
View
@@ -7,7 +7,7 @@ extern crate termion;
use std::{cmp, str, thread};
use std::env::args;
use std::io::{self, Write, Read, StdoutLock};
use std::io::{self, Write, Read};
use std::process::{self, Command, Stdio};
use std::time::Duration;
@@ -57,8 +57,7 @@ COPYRIGHT
fn main() {
let mut args = args().skip(1);
let stdout = io::stdout();
let mut stdout = stdout.lock();
let mut stdout = io::stdout();
let mut stderr = io::stderr();
let mut command = String::new();
@@ -98,10 +97,10 @@ fn main() {
process::exit(1);
}
run(command, interval, &mut stdout).try(&mut stderr);
run(command, interval, stdout).try(&mut stderr);
}
fn run(command: String, interval: u64, stdout: &mut StdoutLock) -> std::io::Result<()> {
fn run<W: IntoRawMode>(command: String, interval: u64, mut stdout: W) -> std::io::Result<()> {
let title = format!("Every {}s: {}", interval, command);
let mut stdout = stdout.into_raw_mode()?;

0 comments on commit 722de2c

Please sign in to comment.