Add in custom welcome message

This commit is contained in:
able 2023-09-15 22:16:34 -05:00
parent 6a8c46a6c4
commit f03b9e41cc
6 changed files with 58 additions and 136 deletions

86
Cargo.lock generated
View file

@ -4,13 +4,11 @@ version = 3
[[package]] [[package]]
name = "adit" name = "adit"
version = "0.1.1" version = "0.2.0"
dependencies = [ dependencies = [
"dirs",
"rhai", "rhai",
"serde", "serde",
"termion", "termion",
"toml",
"unicode-segmentation", "unicode-segmentation",
] ]
@ -33,12 +31,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "2.4.0" version = "2.4.0"
@ -79,26 +71,6 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]]
name = "dirs"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03d86534ed367a67548dc68113a0f5db55432fdfbb6e6f9d77704397d95d5780"
dependencies = [
"libc",
"redox_users",
"winapi",
]
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.10" version = "0.2.10"
@ -176,32 +148,13 @@ version = "0.1.56"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
[[package]]
name = "redox_syscall"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff"
dependencies = [
"bitflags 1.3.2",
]
[[package]] [[package]]
name = "redox_termios" name = "redox_termios"
version = "0.1.1" version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" checksum = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
dependencies = [ dependencies = [
"redox_syscall 0.1.56", "redox_syscall",
]
[[package]]
name = "redox_users"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64"
dependencies = [
"getrandom",
"redox_syscall 0.2.10",
] ]
[[package]] [[package]]
@ -211,7 +164,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "637a4f79f65571b1fd1a0ebbae05bbbf58a01faf612abbc3eea15cda34f0b87a" checksum = "637a4f79f65571b1fd1a0ebbae05bbbf58a01faf612abbc3eea15cda34f0b87a"
dependencies = [ dependencies = [
"ahash", "ahash",
"bitflags 2.4.0", "bitflags",
"instant", "instant",
"num-traits", "num-traits",
"once_cell", "once_cell",
@ -304,7 +257,7 @@ checksum = "6a8fb22f7cde82c8220e5aeacb3258ed7ce996142c77cba193f203515e26c330"
dependencies = [ dependencies = [
"libc", "libc",
"numtoa", "numtoa",
"redox_syscall 0.1.56", "redox_syscall",
"redox_termios", "redox_termios",
] ]
@ -317,15 +270,6 @@ dependencies = [
"crunchy", "crunchy",
] ]
[[package]]
name = "toml"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.12" version = "1.0.12"
@ -355,25 +299,3 @@ name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View file

@ -1,6 +1,6 @@
[package] [package]
name = "adit" name = "adit"
version = "0.1.1" version = "0.2.0"
authors = ["able"] authors = ["able"]
edition = "2018" edition = "2018"
@ -9,8 +9,6 @@ edition = "2018"
[dependencies] [dependencies]
termion = "1" termion = "1"
unicode-segmentation = "1" unicode-segmentation = "1"
toml = "0.5.8"
dirs = "*"
rhai = "1.16.1" rhai = "1.16.1"
[dependencies.serde] [dependencies.serde]

View file

@ -4,3 +4,26 @@ fn status_bar() {
return sb; return sb;
} }
fn welcome_message(){
let wm = `Adit | version ${adit_version}`;
return wm;
}
fn on_start(){
}
fn filetype(file){
switch file {
"rs" => {
let name = "Rust";
return (name);
}
_ => {
let name = "unknown";
return (name);
}
}
}

View file

@ -1,9 +1,6 @@
use std::path::PathBuf; use core::str::FromStr;
use std::str::FromStr;
use std::{fs::File, io::Read};
use crate::Editor;
use serde::Deserialize; use serde::Deserialize;
use std::path::PathBuf;
use termion::color::Rgb; use termion::color::Rgb;
use rhai::Dynamic; use rhai::Dynamic;
@ -35,9 +32,13 @@ impl Config<'static> {
} }
pub fn call(&mut self, fn_name: &str) -> String { pub fn call(&mut self, fn_name: &str) -> String {
self.eng match self
.eng
.call_fn::<String>(&mut self.scope, &self.ast, fn_name, ()) .call_fn::<String>(&mut self.scope, &self.ast, fn_name, ())
.unwrap() {
Ok(string) => string,
Err(err) => panic!("{}", err),
}
} }
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@ -62,35 +63,4 @@ impl Theme {
status_bg_color: RGB(239, 239, 239), status_bg_color: RGB(239, 239, 239),
} }
} }
pub fn read_config() -> ThemeReturn {
match dirs::home_dir() {
Some(pathbuf) => {
let mut file = File::open(pathbuf);
match file {
Ok(mut file_handle) => {
let mut contents = String::new();
match file_handle.read_to_string(&mut contents) {
Ok(ma) => {
let config: Theme = toml::from_str(&contents).unwrap();
return ThemeReturn::Theme(config);
}
Err(_) => todo!(),
}
}
Err(_) => todo!(),
}
}
None => ThemeReturn::NoHomeDir,
}
}
}
pub enum ThemeReturn {
Theme(Theme),
NoHomeDir,
GenericError,
} }

View file

@ -16,6 +16,7 @@ pub struct Document {
impl Document { impl Document {
pub fn open(filename: &str) -> Result<Self, std::io::Error> { pub fn open(filename: &str) -> Result<Self, std::io::Error> {
let contents = fs::read_to_string(filename)?; let contents = fs::read_to_string(filename)?;
// TODO: Call filetype in config.rhai
let file_type = FileType::from(filename); let file_type = FileType::from(filename);
let mut rows = Vec::new(); let mut rows = Vec::new();
for value in contents.lines() { for value in contents.lines() {

View file

@ -3,8 +3,8 @@ use crate::config::Theme;
use crate::Document; use crate::Document;
use crate::Row; use crate::Row;
use crate::Terminal; use crate::Terminal;
use core::time::Duration;
use std::env; use std::env;
use std::time::Duration;
use std::time::Instant; use std::time::Instant;
use termion::event::Key; use termion::event::Key;
@ -12,12 +12,14 @@ const VERSION: &str = env!("CARGO_PKG_VERSION");
const QUIT_TIMES: u8 = 2; const QUIT_TIMES: u8 = 2;
#[derive(PartialEq, Copy, Clone)] #[derive(PartialEq, Copy, Clone)]
#[non_exhaustive]
pub enum SearchDirection { pub enum SearchDirection {
Forward, Forward,
Backward, Backward,
} }
#[derive(Default, Clone)] #[derive(Default, Clone)]
#[non_exhaustive]
pub struct Position { pub struct Position {
pub x: usize, pub x: usize,
pub y: usize, pub y: usize,
@ -99,6 +101,8 @@ impl Editor<'static> {
.scope .scope
.set_or_push("line_count", self.document.len() as u64); .set_or_push("line_count", self.document.len() as u64);
self.config.scope.set_or_push("adit_version", VERSION);
self.config self.config
.scope .scope
.set_or_push("file_type", self.document.file_type()); .set_or_push("file_type", self.document.file_type());
@ -109,6 +113,10 @@ impl Editor<'static> {
self.config self.config
.scope .scope
.set_or_push("y", self.cursor_position.y as u64); .set_or_push("y", self.cursor_position.y as u64);
self.config
.scope
.set_or_push("quit_times", self.quit_times as u64);
} }
fn refresh_screen(&mut self) -> Result<(), std::io::Error> { fn refresh_screen(&mut self) -> Result<(), std::io::Error> {
@ -326,26 +334,26 @@ impl Editor<'static> {
self.cursor_position = Position { x, y } self.cursor_position = Position { x, y }
} }
fn draw_welcome_message(&self) { fn draw_welcome_message(&mut self) {
let mut welcome_message = format!("Able editor -- version {}", VERSION); let mut welcome_message = self.config.call("welcome_message");
let width = self.terminal.size().width as usize; let width = self.terminal.size().width as usize;
let len = welcome_message.len(); let len = welcome_message.len();
#[allow(clippy::integer_arithmetic, clippy::integer_division)] #[allow(clippy::arithmetic_side_effects, clippy::integer_division)]
let padding = width.saturating_sub(len) / 2; let padding = width.saturating_sub(len) / 2;
let spaces = " ".repeat(padding.saturating_sub(1)); let spaces = " ".repeat(padding.saturating_sub(1));
welcome_message = format!("{}{}", spaces, welcome_message); welcome_message = format!("{spaces}{welcome_message}");
welcome_message.truncate(width); welcome_message.truncate(width);
println!("{}\r", welcome_message); println!("{welcome_message}\r");
} }
pub fn draw_row(&self, row: &Row) { pub fn draw_row(&self, row: &Row) {
let width = self.terminal.size().width as usize; let width = self.terminal.size().width as usize;
let start = self.offset.x; let start = self.offset.x;
let end = self.offset.x.saturating_add(width); let end = self.offset.x.saturating_add(width);
let row = row.render(start, end); let row = row.render(start, end);
println!("{}\r", row) println!("{row}\r");
} }
#[allow(clippy::integer_division, clippy::integer_arithmetic)] #[allow(clippy::integer_division, clippy::arithmetic_side_effects)]
fn draw_rows(&self) { fn draw_rows(&mut self) {
let height = self.terminal.size().height; let height = self.terminal.size().height;
for terminal_row in 0..height { for terminal_row in 0..height {
Terminal::clear_current_line(); Terminal::clear_current_line();
@ -392,22 +400,22 @@ impl Editor<'static> {
self.document self.document
.file_name .file_name
.clone() .clone()
.unwrap_or("buffer".to_string()), .unwrap_or("buffer".to_owned()),
); );
status = self.config.call("status_bar"); status = self.config.call("status_bar");
#[allow(clippy::integer_arithmetic)] #[allow(clippy::arithmetic_side_effects)]
let len = status.len(); let len = status.len();
status.push_str(&" ".repeat(width.saturating_sub(len))); status.push_str(&" ".repeat(width.saturating_sub(len)));
status = format!(" {}", status); status = format!(" {status}");
status.truncate(width); status.truncate(width);
let theme = Theme::default(); let theme = Theme::default();
Terminal::set_bg_color(theme.status_bg_color.to_term_color()); Terminal::set_bg_color(theme.status_bg_color.to_term_color());
Terminal::set_fg_color(theme.status_fg_color.to_term_color()); Terminal::set_fg_color(theme.status_fg_color.to_term_color());
println!("{}\r", status); println!("{status}\r");
Terminal::reset_fg_color(); Terminal::reset_fg_color();
Terminal::reset_bg_color(); Terminal::reset_bg_color();
} }