~garritfra/notiz

a0c64c519f67d428dcf47fec65dc091501f97189 — Garrit Franke 4 years ago
feat: basic notetaking
4 files changed, 92 insertions(+), 0 deletions(-)

A .gitignore
A .vscode/launch.json
A Cargo.toml
A src/main.rs
A  => .gitignore +2 -0
@@ 1,2 @@
/target/
Cargo.lock
\ No newline at end of file

A  => .vscode/launch.json +45 -0
@@ 1,45 @@
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug executable 'notiz'",
            "cargo": {
                "args": [
                    "build",
                    "--bin=notiz",
                    "--package=notiz"
                ],
                "filter": {
                    "name": "notiz",
                    "kind": "bin"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        },
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug unit tests in executable 'notiz'",
            "cargo": {
                "args": [
                    "test",
                    "--no-run",
                    "--bin=notiz",
                    "--package=notiz"
                ],
                "filter": {
                    "name": "notiz",
                    "kind": "bin"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}
\ No newline at end of file

A  => Cargo.toml +9 -0
@@ 1,9 @@
[package]
name = "notiz"
version = "0.1.0"
authors = ["Garrit Franke <garritfranke@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

A  => src/main.rs +36 -0
@@ 1,36 @@
use std::collections::hash_map::DefaultHasher;
use std::env;
use std::fs;
use std::hash::Hasher;
use std::io;

fn main() -> Result<(), io::Error> {
    let home_dir = env::var("HOME").expect("Home folder not found");
    let base_dir = format!("{}/.local/share/notiz", home_dir);
    std::fs::create_dir_all(&base_dir).unwrap();

    let args: Vec<String> = env::args().skip(1).collect();

    if args.is_empty() {
        let time = std::time::SystemTime::now();
        let mut s = DefaultHasher::new();
        s.write(format!("{:?}", time).as_bytes());

        let tmp_filename = format!("notiz-{}", s.finish());
        let tmp_filepath = format!("/tmp/{}", &tmp_filename);

        std::process::Command::new("vim")
            .arg(&tmp_filepath)
            .spawn()
            .expect("Error: Failed to run editor")
            .wait()
            .expect("Error: Editor returned a non-zero status");

        fs::copy(&tmp_filename, format!("{}/{}", base_dir, tmp_filename))
            .expect("Can't copy file to base directory");

        fs::remove_file(tmp_filename).expect("Could not remove temporary file");
    }

    Ok(())
}