pub mod entities;
pub mod pane;
pub mod rich;
use std::cell::RefCell;
use std::collections::{HashMap, VecDeque};
use std::rc::Rc;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileError {
NotFound,
Denied,
Unsupported,
Failed,
}
pub mod file_type {
pub const FILE: u32 = 0x01;
pub const DIR: u32 = 0x02;
pub const CHAR: u32 = 0x04;
pub const BLOCK: u32 = 0x08;
pub const PIPE: u32 = 0x10;
pub const SOCKET: u32 = 0x20;
pub const LINK: u32 = 0x40;
}
pub mod file_attr {
pub const HIDDEN: u32 = 0x01;
pub const SYSTEM: u32 = 0x02;
pub const READ: u32 = 0x04;
pub const WRITE: u32 = 0x08;
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FileInfo {
pub kind: u32,
pub attrs: u32,
pub size: u64,
pub accessed: Option<i64>,
pub modified: Option<i64>,
pub created: Option<i64>,
pub link_target: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Sandbox {
Inside,
Itself,
Contains,
Outside,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Input {
Text(String),
End,
Pending,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileDialog {
Open,
Save,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileKind {
SavedGame,
Text,
Other,
}
impl FileKind {
pub fn from_code(code: i64) -> Self {
match code {
15 => FileKind::SavedGame,
2 | 7 => FileKind::Text,
_ => FileKind::Other,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FileChoice {
Chosen(String),
Cancelled,
Pending,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Time {
pub unix_millis: i64,
pub utc_offset_seconds: i32,
}
pub trait Host {
fn print(&mut self, text: &str);
fn rich_display(&self) -> Option<rich::RichDisplay> {
None
}
fn print_markup(&mut self, markup: &str) {
let _ = markup;
}
fn print_input(&mut self, text: &str) {
let _ = text;
}
fn random_bytes(&mut self, buffer: &mut [u8]) {
let _ = buffer;
}
fn read_file(&mut self, name: &str) -> Result<Vec<u8>, FileError> {
let _ = name;
Err(FileError::Unsupported)
}
fn write_file(&mut self, name: &str, data: &[u8]) -> Result<(), FileError> {
let _ = (name, data);
Err(FileError::Unsupported)
}
fn file_info(&mut self, name: &str, follow_links: bool) -> Option<FileInfo> {
let _ = (name, follow_links);
None
}
fn delete_file(&mut self, name: &str) -> Result<(), FileError> {
let _ = name;
Err(FileError::Unsupported)
}
fn rename_file(&mut self, from: &str, to: &str) -> Result<(), FileError> {
let _ = (from, to);
Err(FileError::Unsupported)
}
fn create_dir(&mut self, name: &str, parents: bool) -> Result<(), FileError> {
let _ = (name, parents);
Err(FileError::Unsupported)
}
fn remove_dir(&mut self, name: &str, contents: bool) -> Result<(), FileError> {
let _ = (name, contents);
Err(FileError::Unsupported)
}
fn list_dir(&mut self, name: &str) -> Option<Vec<String>> {
let _ = name;
None
}
fn absolute_path(&mut self, name: &str) -> Option<String> {
let _ = name;
None
}
fn root_dirs(&mut self) -> Vec<String> {
Vec::new()
}
fn sandbox(&mut self, name: &str) -> Sandbox {
let _ = name;
Sandbox::Inside
}
fn temp_file_name(&mut self) -> Option<String> {
None
}
fn special_file(&mut self, id: i32) -> Option<String> {
let _ = id;
None
}
fn append_file(&mut self, name: &str, data: &[u8]) -> Result<(), FileError> {
let _ = (name, data);
Err(FileError::Unsupported)
}
fn read_line(&mut self) -> Input {
Input::End
}
fn read_key(&mut self) -> Input {
Input::End
}
fn choose_file(
&mut self,
prompt: &str,
dialog: FileDialog,
kind: FileKind,
) -> Option<FileChoice> {
let _ = (prompt, dialog, kind);
None
}
fn clear_screen(&mut self) {}
fn console_width(&mut self) -> Option<usize> {
None
}
fn script_input(&mut self) {}
fn sleep(&mut self, milliseconds: u32) -> bool {
let _ = milliseconds;
true
}
fn status_line(&mut self, text: &str) {
let _ = text;
}
fn status_right(&mut self, text: &str) {
let _ = text;
}
fn time(&mut self) -> Time {
Time::default()
}
fn local_time_zone(&mut self) -> Option<String> {
None
}
fn system_name(&self) -> String {
std::env::consts::OS.to_owned()
}
fn has_panes(&self) -> bool {
false
}
fn pane_create(
&mut self,
parent: Option<u32>,
placement: pane::Placement,
spec: pane::PaneSpec,
) -> Option<u32> {
let _ = (parent, placement, spec);
None
}
fn pane_delete(&mut self, id: u32) {
let _ = id;
}
fn pane_clear(&mut self, id: u32) {
let _ = id;
}
fn pane_write(&mut self, id: u32, text: &str) {
let _ = (id, text);
}
fn pane_write_markup(&mut self, id: u32, markup: &str) {
let _ = (id, markup);
}
fn pane_flush(&mut self, id: u32) {
let _ = id;
}
fn pane_set_size(&mut self, id: u32, size: i32, units: i32, advisory: bool) {
let _ = (id, size, units, advisory);
}
fn pane_size_to_contents(&mut self, id: u32) {
let _ = id;
}
fn pane_go_to(&mut self, id: u32, row: usize, column: usize) {
let _ = (id, row, column);
}
fn pane_info(&mut self, id: u32) -> Option<pane::PaneInfo> {
let _ = id;
None
}
fn pane_set_screen_color(&mut self, id: u32, color: u32) {
let _ = (id, color);
}
}
#[derive(Debug, Default, Clone)]
pub struct BufferHost {
output: Rc<RefCell<String>>,
files: Rc<RefCell<HashMap<String, Vec<u8>>>>,
input: Rc<RefCell<VecDeque<String>>>,
temp_counter: Rc<RefCell<u32>>,
interactive: Rc<RefCell<bool>>,
screen: Rc<RefCell<Option<pane::Screen>>>,
}
impl BufferHost {
pub fn new() -> Self {
Self::default()
}
pub fn output(&self) -> String {
self.output.borrow().clone()
}
pub fn push_input(&self, line: &str) {
self.input.borrow_mut().push_back(line.to_owned());
}
pub fn set_interactive(&self, interactive: bool) {
*self.interactive.borrow_mut() = interactive;
}
fn next_input(&self) -> Input {
match self.input.borrow_mut().pop_front() {
Some(line) => Input::Text(line),
None if *self.interactive.borrow() => Input::Pending,
None => Input::End,
}
}
pub fn file(&self, name: &str) -> Option<Vec<u8>> {
self.files.borrow().get(name).cloned()
}
pub fn enable_panes(&self, rows: usize, columns: usize) {
*self.screen.borrow_mut() = Some(pane::Screen::new(rows, columns, true));
}
pub fn screen(&self) -> std::cell::Ref<'_, Option<pane::Screen>> {
self.screen.borrow()
}
fn with_screen<T>(&self, f: impl FnOnce(&mut pane::Screen) -> T) -> Option<T> {
self.screen.borrow_mut().as_mut().map(f)
}
pub fn set_file(&self, name: &str, data: &[u8]) {
self.files
.borrow_mut()
.insert(name.to_owned(), data.to_vec());
}
}
impl Host for BufferHost {
fn print(&mut self, text: &str) {
self.output.borrow_mut().push_str(text);
}
fn read_file(&mut self, name: &str) -> Result<Vec<u8>, FileError> {
self.files
.borrow()
.get(name)
.cloned()
.ok_or(FileError::NotFound)
}
fn write_file(&mut self, name: &str, data: &[u8]) -> Result<(), FileError> {
self.files
.borrow_mut()
.insert(name.to_owned(), data.to_vec());
Ok(())
}
fn append_file(&mut self, name: &str, data: &[u8]) -> Result<(), FileError> {
self.files
.borrow_mut()
.entry(name.to_owned())
.or_default()
.extend_from_slice(data);
Ok(())
}
fn file_info(&mut self, name: &str, _follow_links: bool) -> Option<FileInfo> {
self.files.borrow().get(name).map(|data| FileInfo {
kind: file_type::FILE,
attrs: file_attr::READ | file_attr::WRITE,
size: data.len() as u64,
..FileInfo::default()
})
}
fn delete_file(&mut self, name: &str) -> Result<(), FileError> {
match self.files.borrow_mut().remove(name) {
Some(_) => Ok(()),
None => Err(FileError::NotFound),
}
}
fn rename_file(&mut self, from: &str, to: &str) -> Result<(), FileError> {
let mut files = self.files.borrow_mut();
if files.contains_key(to) {
return Err(FileError::Failed);
}
let data = files.remove(from).ok_or(FileError::NotFound)?;
files.insert(to.to_owned(), data);
Ok(())
}
fn temp_file_name(&mut self) -> Option<String> {
let mut counter = self.temp_counter.borrow_mut();
*counter += 1;
Some(format!("/tmp/temp{counter}"))
}
fn read_line(&mut self) -> Input {
self.next_input()
}
fn read_key(&mut self) -> Input {
self.next_input()
}
fn has_panes(&self) -> bool {
self.screen.borrow().is_some()
}
fn pane_create(
&mut self,
parent: Option<u32>,
placement: pane::Placement,
spec: pane::PaneSpec,
) -> Option<u32> {
self.with_screen(|screen| screen.create(parent, placement, spec))
.flatten()
}
fn pane_delete(&mut self, id: u32) {
self.with_screen(|screen| screen.delete(id));
}
fn pane_clear(&mut self, id: u32) {
self.with_screen(|screen| screen.clear(id));
}
fn pane_write(&mut self, id: u32, text: &str) {
self.with_screen(|screen| screen.write(id, text));
}
fn pane_set_size(&mut self, id: u32, size: i32, units: i32, advisory: bool) {
self.with_screen(|screen| screen.set_size(id, size, units, advisory));
}
fn pane_size_to_contents(&mut self, id: u32) {
self.with_screen(|screen| screen.size_to_contents(id));
}
fn pane_go_to(&mut self, id: u32, row: usize, column: usize) {
self.with_screen(|screen| screen.go_to(id, row, column));
}
fn pane_info(&mut self, id: u32) -> Option<pane::PaneInfo> {
self.with_screen(|screen| screen.info(id)).flatten()
}
fn pane_set_screen_color(&mut self, id: u32, color: u32) {
self.with_screen(|screen| screen.set_screen_color(id, color));
}
}