From 9cdd608311b79e30f6d9fdb1462508c6765c579f Mon Sep 17 00:00:00 2001 From: FantasyPvP <80643031+FantasyPvP@users.noreply.github.com> Date: Thu, 24 Oct 2024 18:32:17 +0100 Subject: [PATCH] written initial prototype in rust --- .gitignore | 2 + final/main.c | 25 +++++++ prototype/Cargo.lock | 48 +++++++++++++ prototype/Cargo.toml | 7 ++ prototype/something.txt | 1 + prototype/src/main.rs | 149 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 232 insertions(+) create mode 100644 .gitignore create mode 100644 final/main.c create mode 100644 prototype/Cargo.lock create mode 100644 prototype/Cargo.toml create mode 100644 prototype/something.txt create mode 100644 prototype/src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7fc4f74 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*/target +*.out \ No newline at end of file diff --git a/final/main.c b/final/main.c new file mode 100644 index 0000000..688cf3c --- /dev/null +++ b/final/main.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include +#include + +typedef struct { + int file_line; + int file_col; + int buff_line; + int buff_col; + int editmode; +} EditorData; + +void help() { + printf("Usage:\n"); + printf(" cmd open // opens the specified file\n"); + printf(" cmd rm // deletes the specified file\n"); + printf(" cmd new // creates a new empty file at the specified path\n"); + printf(" cmd mv // moves the specified file to the new path\n"); + printf(" cmd cp // copies the specified file to the new path\n"); + printf(" cmd len // returns the length of the specified file\n"); + printf(" cmd log // prints a list of all changes made to the file\n"); +} diff --git a/prototype/Cargo.lock b/prototype/Cargo.lock new file mode 100644 index 0000000..ac89e8b --- /dev/null +++ b/prototype/Cargo.lock @@ -0,0 +1,48 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cc" +version = "1.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2e7962b54006dcfcc61cb72735f4d89bb97061dd6a7ed882ec6b8ee53714c6f" +dependencies = [ + "shlex", +] + +[[package]] +name = "cs_coursework_editor_draft" +version = "0.1.0" +dependencies = [ + "ncurses", +] + +[[package]] +name = "libc" +version = "0.2.161" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" + +[[package]] +name = "ncurses" +version = "6.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbd71afa95710e841d173521e8483e764004eb332bdf47bd01d00f568688027f" +dependencies = [ + "cc", + "libc", + "pkg-config", +] + +[[package]] +name = "pkg-config" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" diff --git a/prototype/Cargo.toml b/prototype/Cargo.toml new file mode 100644 index 0000000..2a99654 --- /dev/null +++ b/prototype/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "cs_coursework_editor_draft" +version = "0.1.0" +edition = "2021" + +[dependencies] +ncurses = "6.0.1" diff --git a/prototype/something.txt b/prototype/something.txt new file mode 100644 index 0000000..6c041dd --- /dev/null +++ b/prototype/something.txt @@ -0,0 +1 @@ +[package] name = "cs_coursework_editor_draft" version = "0.1.0" edition = "2021" [dependencies] ncurses = "6.0.1" i may have made a very scuffed version of vim. \ No newline at end of file diff --git a/prototype/src/main.rs b/prototype/src/main.rs new file mode 100644 index 0000000..986a372 --- /dev/null +++ b/prototype/src/main.rs @@ -0,0 +1,149 @@ +extern crate ncurses; + +use std::{env, fs::{self, File, OpenOptions}, io::Write}; + +struct EditorData { + file_line: i32, + file_col: i32, + buff_line: i32, + buff_col: i32, + editmode: bool, +} + +impl EditorData { + fn new() -> EditorData { + EditorData { + file_line: 0, + file_col: 0, + buff_line: 0, + buff_col: 0, + editmode: false, + } + } +} + +fn main() { + let args = env::args().collect::>(); + + if let Some(a) = env::args().nth(1) { + match a.as_str() { + "open" => open(&args[2]).unwrap(), + // "rm" => rm(&args[2]), + // "new" => new(&args[2]), + // "mv" => mv(&args[2], &args[3]), + // "cp" => cp(&args[2], &args[3]), + // "len" => len(&args[2]), + // "log" => log(), + _ => help(), + } + } else { + help(); + } + +} + +fn help() { + println!("Usage: + cmd open // opens the specified file + cmd rm // deletes the specified file + cmd new // creates a new empty file at the specified path + cmd mv // moves the specified file to the new path + cmd cp // copies the specified file to the new path + cmd len // returns the length of the specified file + cmd log // prints a list of all changes made to the file + ") +} + +fn open(filename: &str) -> Result<(), &'static str> { + let filestr = match fs::read_to_string(filename) { + Ok(s) => s, + Err(_) => return Err("file not found"), + }; + + let mut data = EditorData::new(); + + ncurses::initscr(); + ncurses::raw(); + ncurses::noecho(); + ncurses::keypad(ncurses::stdscr(), true); + let mut max_y = 0; + let mut max_x = 0; + ncurses::getmaxyx(ncurses::stdscr(), &mut max_y, &mut max_x); + + if let Err(_) = ncurses::addstr(&filestr) { + ncurses::endwin(); + return Err("error writing file to buffer"); + }; + + ncurses::mv(0, 0); + + + loop { + ncurses::refresh(); + let keystroke = ncurses::getch(); + + if data.editmode { + match keystroke { + 27 => data.editmode = false, + ncurses::KEY_BACKSPACE => { + mv_cursor(&mut data, 0, -1); + ncurses::delch(); + }, + ncurses::KEY_DC => { ncurses::delch(); }, + ncurses::KEY_LEFT => mv_cursor(&mut data, 0, -1), + ncurses::KEY_RIGHT => mv_cursor(&mut data, 0, 1), + ncurses::KEY_UP => mv_cursor(&mut data, -1, 0), + ncurses::KEY_DOWN => mv_cursor(&mut data, 1, 0), + _ => { ncurses::insch(keystroke as u32); mv_cursor(&mut data, 0, 1); }, + } + } else { + match keystroke { + 105 => { + data.editmode = true; + }, + 119 => { + let buff_size = max_y * max_x; + let mut buff = vec![0; buff_size as usize]; + + for i in 0..buff_size { + buff[i as usize] = (ncurses::mvwinch(ncurses::stdscr(), i / max_x, i % max_x) & ncurses::A_CHARTEXT) as u8; + } + ncurses::endwin(); + + OpenOptions::new() + .write(true) + .truncate(true) + .open(filename) + .unwrap() + .write_all(&buff) + .unwrap(); + } + 113 => { + ncurses::endwin(); + return Ok(()); + }, + _ => {}, + } + } + } +} + +fn mv_cursor(pos: &mut EditorData, dy: i32, dx: i32) { + if pos.file_col + dx >= 0 { + pos.file_col += dx; + } + + if pos.buff_col + dx >= 0 { + pos.buff_col += dx; + } + + if pos.file_line + dy >= 0 { + pos.file_line += dy; + } + + if pos.buff_line + dy >= 0 { + pos.buff_line += dy; + } + + ncurses::mv(pos.buff_line, pos.buff_col); +} \ No newline at end of file