summaryrefslogtreecommitdiffstats
path: root/src/actions/cursor.rs
blob: 8b052a2ba3842b1cfdcdb0cb3f241c88732217b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
extern crate atty;

use cursorfile;
use utils::fileutil;
use KhResult;

pub fn do_cursor(_args: &[&str]) -> KhResult<()> {
  if atty::isnt(atty::Stream::Stdin) {
    write_stdin_to_cursorfile();
  } else {
    //println!("stdin is tty")
  }

  if atty::isnt(atty::Stream::Stdout) || atty::is(atty::Stream::Stdin) {
    write_cursorfile_to_stdout();
  }

  Ok(())
}

fn write_stdin_to_cursorfile() {
  let lines = match fileutil::read_lines_from_stdin() {
    Ok(input) => input,
    Err(error) => {
      error!("Error reading from stdin: {}", error);
      return
    }
  };

  if lines.len() > 1 {
    error!("Too many lines on stdin");
    return
  };

  cursorfile::write_cursorfile(&lines[0]).unwrap();
}

fn write_cursorfile_to_stdout() {
  if let Ok(cursor) = cursorfile::read_cursorfile() {
    println!("{}", cursor);
  }
}