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
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! # Scrawl 
//! A library for opening a file for editing in a text editor and capturing the result as a String
#![deny(missing_docs,
        missing_debug_implementations, missing_copy_implementations,
        trivial_casts, trivial_numeric_casts,
        unstable_features, unsafe_code,
        unused_import_braces, unused_qualifications)]

use std::{
    fs,
    env::{temp_dir, var},
    path::{Path, PathBuf},
    sync::atomic::{AtomicUsize, Ordering},
    process::Command
};

pub mod error;
use error::ScrawlError as ScrawlError;

/// New opens an empty text buffer in an editor and returns a Result<String> with the contents.
///
/// # Example
/// ```no_run
/// fn main() {
///     let output = match scrawl::new() {
///          Ok(s) => s,
///          Err(e) => e.to_string()
///    };
///    println!("{}", output);
/// }
/// ```
pub fn new() -> Result<String, ScrawlError> {
    let temp_file = create_temp_file()?;
    open_editor(&temp_file).and_then(|output| {
        let _ = fs::remove_file(temp_file);
        Ok(output)
    })
}

/// New opens an text buffer with the contents of the provided String in an editor. Returns a Result<String> with the edited contents.
///
/// # Example
/// ```no_run
/// fn main() {
///     let output = match scrawl::with("Hello World!") {
///          Ok(s) => s,
///          Err(e) => e.to_string()
///    };
///    println!("{}", output);
/// }
/// ```
pub fn with(content: &str) -> Result<String, ScrawlError> {
    let temp_file = create_temp_file()?;

    fs::write(&temp_file, content).map_err(|_| {
        ScrawlError::FailedToCreateTempfile
    })?;

    open_editor(&temp_file).and_then(|output| {
        let _ = fs::remove_file(temp_file);
        Ok(output)
    })
}

/// Open opens a text buffer in an editor with the contents of the file specified. This does _not_ edit the contents of the file. Returns a Result<String> with the contents of the buffer.
///
/// # Example
/// ```no_run
/// use std::path::Path;
/// 
/// fn main() {
///     let path = Path::new("hello.txt");
///     let output = match scrawl::open(path) {
///          Ok(s) => s,
///          Err(e) => e.to_string()
///    };
///    println!("{}", output);
/// }
/// ```
pub fn open(p: &Path) -> Result<String, ScrawlError> {
    let temp_file = create_temp_file()?;

    /* Copy the contents of the file to the temp file */
    fs::copy(p, &temp_file).map_err(|_| {
        let p = p.to_str().unwrap_or("<unknown>");
        ScrawlError::FailedToCopyToTempFile(String::from(p))
    })?;
    
    open_editor(&temp_file).and_then(|output| {
        let _ = fs::remove_file(temp_file);
        Ok(output)
    })
}

/// Edit opens a text buffer in an editor with the contents of the file specified. This _does_ edit the contents of the file. Returns a Result<String> with the contents of the buffer.
///
/// # Example
/// ```no_run
/// use std::path::Path;
/// 
/// fn main() {
///     let path = Path::new("hello.txt");
///     let output = match scrawl::edit(path) {
///          Ok(s) => s,
///          Err(e) => e.to_string()
///    };
///    println!("{}", output);
/// }
/// ```
pub fn edit(p: &Path) -> Result<String, ScrawlError> {
    open_editor(p)
}

/* Attempts to determine which text editor to open the text buffer with. */
fn get_editor_name() -> Result<String, ScrawlError> {
    match var("EDITOR") {
        Ok(s) => Ok(s),
        _ => Err(ScrawlError::EditorNotFound)
    }
}

/* Creates the temporary file */
const PREFIX: &str = "xvrqt_scrawl";
static TEMP_FILE_COUNT: AtomicUsize = AtomicUsize::new(0);
fn create_temp_file() -> Result<PathBuf, ScrawlError> {
    /* Generate unique path to a temporary file buffer */
    let i = TEMP_FILE_COUNT.fetch_add(1, Ordering::SeqCst);
    let process_id = std::process::id();
    let temp_file = format!("{}_{}_{}", PREFIX, process_id, i);

    let mut temp_dir = temp_dir();
    temp_dir.push(temp_file);

    match fs::File::create(&temp_dir) {
        Err(_) => Err(ScrawlError::FailedToCreateTempfile),
        _ => Ok(temp_dir)
    }
}

/* Opens the file in the user's preferred text editor, and returns the contents
   as a String 
*/
fn open_editor(path: &Path) -> Result<String, ScrawlError> {
    let editor_name = get_editor_name()?;
    match Command::new(&editor_name)
        .arg(&path)
        .status() { 
            Ok(status) if status.success() => {
                fs::read_to_string(path).map_err(|_| {
                    ScrawlError::FailedToReadIntoString
                })
            },
            _ => Err(ScrawlError::FailedToOpenEditor(editor_name))
    }
}