From d8de879f8850b5ea82463f9897d5b45923eeac10 Mon Sep 17 00:00:00 2001 From: SooHoon Choi Date: Sat, 8 Jun 2024 02:41:13 -0700 Subject: [PATCH] some changes --- .vscode/settings.json | 2 +- README.md | 2 +- assets/fs/user/soohoon/about | 19 --- assets/fs/user/soohoon/credits | 5 - assets/fs/user/soohoon/help | 33 ---- assets/fs/user/soohoon/license | 5 - assets/fs/user/soohoon/source | 5 - assets/fs/user/soohoon/timeline | 84 ---------- rust-project.json | 10 +- src/app/layout.tsx | 2 +- src/app/providers.tsx | 24 +-- src/components/terminal-content.tsx | 9 +- src/components/terminal-input.tsx | 13 +- src/lib/fs/fs.tsx | 231 +++++++++++++++++++++++++++ src/lib/fs/index.ts | 28 +++- src/lib/utils.ts | 2 +- src/styles/globals.css | 2 +- src/types/shell.d.ts | 2 +- src/wasm/src/filesystem/create.rs | 67 -------- src/wasm/src/filesystem/directory.rs | 8 +- src/wasm/src/filesystem/mod.rs | 1 - src/wasm/src/filesystem/node.rs | 22 +-- src/wasm/src/lib.rs | 22 ++- src/wasm/src/shell/exec.rs | 18 ++- 24 files changed, 327 insertions(+), 289 deletions(-) delete mode 100644 assets/fs/user/soohoon/about delete mode 100644 assets/fs/user/soohoon/credits delete mode 100644 assets/fs/user/soohoon/help delete mode 100644 assets/fs/user/soohoon/license delete mode 100644 assets/fs/user/soohoon/source delete mode 100644 assets/fs/user/soohoon/timeline create mode 100644 src/lib/fs/fs.tsx delete mode 100644 src/wasm/src/filesystem/create.rs diff --git a/.vscode/settings.json b/.vscode/settings.json index bf48e1a..70eed10 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,4 +5,4 @@ "./src/wasm/Cargo.toml", "./src/wasm/Cargo.toml" ] -} \ No newline at end of file +} diff --git a/README.md b/README.md index 5c7fc54..0cc1cac 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,4 @@ this is my website -thanks for coming \ No newline at end of file +thanks for coming diff --git a/assets/fs/user/soohoon/about b/assets/fs/user/soohoon/about deleted file mode 100644 index cdd55e9..0000000 --- a/assets/fs/user/soohoon/about +++ /dev/null @@ -1,19 +0,0 @@ -

- - soohoonchoi - -
i am a cofounder over at{' '} - - onboard ai - - .
- recently graduated from{' '} - - gt - {' '} - with a degree in math and cs. -
i also like to{' '} - - art - -

diff --git a/assets/fs/user/soohoon/credits b/assets/fs/user/soohoon/credits deleted file mode 100644 index a9a8a77..0000000 --- a/assets/fs/user/soohoon/credits +++ /dev/null @@ -1,5 +0,0 @@ -

- - soohoonchoi - -

\ No newline at end of file diff --git a/assets/fs/user/soohoon/help b/assets/fs/user/soohoon/help deleted file mode 100644 index 8d3b038..0000000 --- a/assets/fs/user/soohoon/help +++ /dev/null @@ -1,33 +0,0 @@ - \ No newline at end of file diff --git a/assets/fs/user/soohoon/license b/assets/fs/user/soohoon/license deleted file mode 100644 index f7b7ce3..0000000 --- a/assets/fs/user/soohoon/license +++ /dev/null @@ -1,5 +0,0 @@ -

- - MIT - -

\ No newline at end of file diff --git a/assets/fs/user/soohoon/source b/assets/fs/user/soohoon/source deleted file mode 100644 index e84d59d..0000000 --- a/assets/fs/user/soohoon/source +++ /dev/null @@ -1,5 +0,0 @@ -

- - github - -

\ No newline at end of file diff --git a/assets/fs/user/soohoon/timeline b/assets/fs/user/soohoon/timeline deleted file mode 100644 index af9429e..0000000 --- a/assets/fs/user/soohoon/timeline +++ /dev/null @@ -1,84 +0,0 @@ - \ No newline at end of file diff --git a/rust-project.json b/rust-project.json index 00263ff..1bc5bdd 100644 --- a/rust-project.json +++ b/rust-project.json @@ -1,11 +1,9 @@ { "roots": [ - { - "crateRoots": [ - "./src/wasm/src/lib.rs" - ], - "isWorkspaceMember": true - } + { + "crateRoots": ["./src/wasm/src/lib.rs"], + "isWorkspaceMember": true + } ], "source": "discover" } diff --git a/src/app/layout.tsx b/src/app/layout.tsx index a001c54..01a09ea 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -7,7 +7,7 @@ const inter = Inter({ subsets: ['latin'] }); export const metadata: Metadata = { title: 'soohoonchoi', - description: "soohoonchoi", + description: 'soohoonchoi', }; export default function RootLayout({ children }: { children: React.ReactNode }) { diff --git a/src/app/providers.tsx b/src/app/providers.tsx index a9e584d..c2b2455 100644 --- a/src/app/providers.tsx +++ b/src/app/providers.tsx @@ -14,8 +14,7 @@ type ShellContext = { prompt: string; }; -const initialShell = { -} as Shell; +const initialShell = {} as Shell; const initialShellContext = { execute: (command: string) => null, @@ -33,7 +32,7 @@ export function useShell() { } export function ShellProvider({ children }: { children: React.ReactNode }) { - console.log('Welcome to my website!') + console.log('Welcome to my website!'); const [shell, setShell] = React.useState(initialShell); const [prompt, setPrompt] = React.useState(''); const execute = (command: string) => { @@ -42,13 +41,14 @@ export function ShellProvider({ children }: { children: React.ReactNode }) { return; } const result = JSON.parse(shell.run(command)); + console.log('execute', result); setPrompt(`${result.user}@${result.host} ${result.path} $`); - return parse(result.result) as React.ReactNode | null; - } + return parse(result.result) as React.ReactNode | null; + }; React.useEffect(() => { async function init() { const shell = await getShell(); - const result = JSON.parse(shell.run('hello')) + const result = JSON.parse(shell.run('hello')); console.log(result.result); setPrompt(`${result.user}@${result.host} ${result.path} $`); setShell(shell); @@ -57,10 +57,14 @@ export function ShellProvider({ children }: { children: React.ReactNode }) { }, []); return ( - {children} + + {children} + ); } diff --git a/src/components/terminal-content.tsx b/src/components/terminal-content.tsx index cf04c54..6524b97 100644 --- a/src/components/terminal-content.tsx +++ b/src/components/terminal-content.tsx @@ -17,7 +17,7 @@ export const TerminalContent = () => { hour12: false, }); return `${formattedDate}, ${formattedTime}`; - } + }; const [currentTime, setCurrentTime] = useState(getTime()); useEffect(() => { @@ -35,7 +35,10 @@ export const TerminalContent = () => { {showWelcome && ( <>

- soohoonchoi ({currentTime})
+ + soohoonchoi + {' '} + ({currentTime})
Type "help", "credits" or "license" for more.

@@ -48,4 +51,4 @@ export const TerminalContent = () => { ))} ); -}; \ No newline at end of file +}; diff --git a/src/components/terminal-input.tsx b/src/components/terminal-input.tsx index 06fd31a..12770d1 100644 --- a/src/components/terminal-input.tsx +++ b/src/components/terminal-input.tsx @@ -3,15 +3,8 @@ import React from 'react'; import { useTerminalState, useShell } from '@/app/providers'; export const TerminalInput = React.forwardRef(({}, ref) => { - const { - setShowWelcome, - inputs, - setInputs, - outputs, - setOutputs, - inputIndex, - setInputIndex, - } = useTerminalState(); + const { setShowWelcome, inputs, setInputs, outputs, setOutputs, inputIndex, setInputIndex } = + useTerminalState(); const [input, setInput] = React.useState(''); const inputRef = React.useRef(null); const { execute, prompt } = useShell(); @@ -70,7 +63,7 @@ export const TerminalInput = React.forwardRef(({}, ref) => { return (
- {prompt ? ({prompt}) : null} + {prompt ? {prompt} : null} + + soohoonchoi + +
i am a cofounder over at{' '} + + onboard ai + + .
+ recently graduated from{' '} + + gt + {' '} + with a degree in math and cs. +
i also like to{' '} + + art + +

+ ), + }, + credits: { + type: 'file', + content: ( +

+ + soohoonchoi + +

+ ), + }, + help: { + type: 'file', + content: ( +
    +
  • [command]: [description]
  • +
  • + about + {': '} + about me +
  • +
  • + resume + {': '} + view my resume +
  • +
  • + timeline + {': '} + some of my notable moments +
  • +
  • + source + {': '} + view the source code +
  • +
  • + clear + {': '} + clear the terminal +
  • +
  • + (+ more) + {': '} + try stuff, in the works +
  • +
+ ), + }, + license: { + type: 'file', + content: ( +

+ + MIT + +

+ ), + }, + projects: { + type: 'directory', + children: {}, + }, + resume: { + type: 'file', + content: ( +

+ + github + +

+ ), + }, + source: { + type: 'file', + content: ( +

+ + github + +

+ ), + }, + timeline: { + type: 'file', + content: ( + + ), + }, + }, + }, + }, + }, +}; diff --git a/src/lib/fs/index.ts b/src/lib/fs/index.ts index cfc0a9a..1ffe1c0 100644 --- a/src/lib/fs/index.ts +++ b/src/lib/fs/index.ts @@ -1,10 +1,34 @@ import init, { Shell as WASMShell } from '@/wasm/pkg'; +import { fs } from './fs'; + +async function create( + shell: WASMShell, + fs: { [key: string]: { type: string; children?: any; content?: any }}, + currentPath = '' +) { + for (const [name, value] of Object.entries(fs)) { + const fullPath = `${currentPath}/${name}`; + if (value.type === 'directory') { + shell.run(`mkdir ${fullPath}`); + if (value.children) { + await create(shell, value.children, fullPath); + } + } else if (value.type === 'file') { + console.log(`touch ${fullPath}`); + // shell.run(`touch ${fullPath}`); + // if (value.content) { + // shell.run(`write ${fullPath} "${value.content}"`); + // } + } + } +} export async function getShell() { await init(); // https://github.com/rustwasm/wasm-bindgen/issues/166 - const shell = new WASMShell("guest", "soohoonchoi.com"); - return shell + const shell = new WASMShell('guest', 'soohoonchoi.com'); + await create(shell, fs); + return shell; } export type Shell = WASMShell; diff --git a/src/lib/utils.ts b/src/lib/utils.ts index d34cf7e..9ad0df4 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -3,4 +3,4 @@ import { twMerge } from 'tailwind-merge'; export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)); -} \ No newline at end of file +} diff --git a/src/styles/globals.css b/src/styles/globals.css index 5a7a3d2..6d40966 100644 --- a/src/styles/globals.css +++ b/src/styles/globals.css @@ -70,7 +70,7 @@ } .link { - color:#10b981 + color: #10b981; } } diff --git a/src/types/shell.d.ts b/src/types/shell.d.ts index d759fa7..234b305 100644 --- a/src/types/shell.d.ts +++ b/src/types/shell.d.ts @@ -1 +1 @@ -type Message = React.ReactNode; \ No newline at end of file +type Message = React.ReactNode; diff --git a/src/wasm/src/filesystem/create.rs b/src/wasm/src/filesystem/create.rs deleted file mode 100644 index f8548d2..0000000 --- a/src/wasm/src/filesystem/create.rs +++ /dev/null @@ -1,67 +0,0 @@ - - -// use std::cell::RefCell; -// use std::rc::Rc; -use std::path::Path; -use once_cell::sync::Lazy; -use std::sync::{Arc, Mutex}; - -use crate::filesystem::node::Node; -use crate::filesystem::node::NodeType; - -use web_sys::console; - -static ROOT_NODE: Lazy>> = Lazy::new(|| { - return create_fs(&Path::new("../../assets/fs")); -}); - -pub fn get_root_node() -> Arc> { - ROOT_NODE.clone() -} - -fn traverse(path: &Path, node: Arc>) { - - console::log_1(&path.display().to_string().into()); - - let children = match path.read_dir() { - Ok(children) => { - console::log_1(&"children".into()); - children - }, - Err(_) => { - console::log_1(&"error".into()); - return; - - }, - }; - - for child in children.filter_map(|e| e.ok()) { - let child_path = child.path(); - let child_name = child_path.file_name().unwrap().to_str().unwrap().to_string(); - let child_node; - if child_path.is_dir() { - console::log_1(&"directory".into()); - child_node = Arc::new(Mutex::new(Node::new(child_name, NodeType::Directory, Some(node.clone())))); - traverse(&child_path, Arc::clone(&child_node)); - } else if child_path.is_file() { - console::log_1(&"file".into()); - child_node = Arc::new(Mutex::new(Node::new(child_name, NodeType::File, Some(node.clone())))); - } else { - continue; - } - - match node.lock().unwrap().get_node_type() { - NodeType::File => continue, - NodeType::Directory => { - node.lock().unwrap().add_child(Arc::clone(&child_node)); - } - } - } -} - -fn create_fs (root_path: &Path) -> Arc> { - let root = Arc::new(Mutex::new(Node::new("root".to_string(), NodeType::Directory, None))); - // read files in root_path and create a filesystem structure, traverse directories - traverse(root_path, Arc::clone(&root)); - root -} \ No newline at end of file diff --git a/src/wasm/src/filesystem/directory.rs b/src/wasm/src/filesystem/directory.rs index f0194fb..3ae7055 100644 --- a/src/wasm/src/filesystem/directory.rs +++ b/src/wasm/src/filesystem/directory.rs @@ -1,11 +1,11 @@ -// use std::cell::RefCell; -// use std::rc::Rc; -use std::sync::{Arc, Mutex}; +use std::cell::RefCell; +use std::rc::Rc; +// use std::sync::{Arc, Mutex}; use crate::filesystem::node::Node; pub struct Directory { name: String, - node: Arc>, + node: Rc>, } impl Directory { diff --git a/src/wasm/src/filesystem/mod.rs b/src/wasm/src/filesystem/mod.rs index f3fcac5..41bd1c8 100644 --- a/src/wasm/src/filesystem/mod.rs +++ b/src/wasm/src/filesystem/mod.rs @@ -2,4 +2,3 @@ pub mod directory; pub mod file; pub mod node; pub mod pipe; -pub mod create; \ No newline at end of file diff --git a/src/wasm/src/filesystem/node.rs b/src/wasm/src/filesystem/node.rs index ec058d9..e82877f 100644 --- a/src/wasm/src/filesystem/node.rs +++ b/src/wasm/src/filesystem/node.rs @@ -1,6 +1,6 @@ -// use std::rc::Rc; -// use std::cell::RefCell; -use std::sync::{Arc, Mutex}; +use std::rc::Rc; +use std::cell::RefCell; +// use std::sync::{Arc, Mutex}; #[derive(Clone)] pub enum NodeType { @@ -12,17 +12,17 @@ pub enum NodeType { pub struct Node { name: String, node_type: NodeType, - parent: Option>>, - children: Vec>>, + parent: Option>>, + children: Vec>>, } impl Node { - pub fn new(name: String, node_type: NodeType, parent: Option>>) -> Node { + pub fn new(name: String, node_type: NodeType, parent: Option>>) -> Node { Node { name, node_type, parent, - children: Vec::>>::new(), + children: Vec::>>::new(), } } @@ -34,18 +34,18 @@ impl Node { self.name.clone() } - pub fn get_parent(&self) -> Option>> { + pub fn get_parent(&self) -> Option>> { match &self.parent { - Some(parent) => Some(Arc::clone(parent)), + Some(parent) => Some(Rc::clone(parent)), None => None, } } - pub fn add_child(&mut self, child: Arc>) { + pub fn add_child(&mut self, child: Rc>) { self.children.push(child); } - pub fn get_children(&self) -> Vec>> { + pub fn get_children(&self) -> Vec>> { self.children.clone() } } \ No newline at end of file diff --git a/src/wasm/src/lib.rs b/src/wasm/src/lib.rs index 6cab92e..b82cd4c 100644 --- a/src/wasm/src/lib.rs +++ b/src/wasm/src/lib.rs @@ -1,6 +1,5 @@ use std::rc::Rc; -// use std::cell::RefCell; -use std::sync::{Arc, Mutex}; +use std::cell::RefCell; use wasm_bindgen::prelude::*; use web_sys::console; @@ -12,7 +11,6 @@ mod filesystem; use shell::lexer::Lexer; use shell::exec::Exec; use filesystem::node::Node; -use filesystem::create::get_root_node; // use filesystem::pipe::Pipe; @@ -20,8 +18,8 @@ use filesystem::create::get_root_node; pub struct Shell { lexer: Lexer, exec: Exec, - root: Arc>, - current: Arc>, + root: Rc>, + current: Rc>, // pipe: Pipe, user: String, hostname: String, @@ -31,15 +29,15 @@ pub struct Shell { impl Shell { #[wasm_bindgen(constructor)] pub fn new(user: String, hostname: String) -> Shell { - // hardcoded for now - console::log_1(&"Creating filesystem".into()); - let root = get_root_node(); - // let current = root.borrow().get("/user/guest").unwrap(); + let root = Rc::new(RefCell::new( + Node::new("/".to_string(), filesystem::node::NodeType::Directory, None) + )); + Shell { lexer: Lexer::new(), - exec: Exec::new(Arc::clone(&root), Arc::clone(&root)), + exec: Exec::new(Rc::clone(&root), Rc::clone(&root)), // current, - current: Arc::clone(&root), + current: Rc::clone(&root), root, // pipe: Pipe::new(""), user, @@ -56,7 +54,7 @@ impl Shell { "result": result, "user": self.user, "host": self.hostname, - "path": self.current.lock().unwrap().get_name(), + "path": self.current.borrow().get_name(), }); let output_str = serde_json::to_string(&output).unwrap(); diff --git a/src/wasm/src/shell/exec.rs b/src/wasm/src/shell/exec.rs index 0f35220..294c290 100644 --- a/src/wasm/src/shell/exec.rs +++ b/src/wasm/src/shell/exec.rs @@ -1,7 +1,6 @@ -// use std::rc::Rc; -// use std::cell::RefCell; -use std::sync::{Arc, Mutex}; +use std::rc::Rc; +use std::cell::RefCell; use wasm_bindgen::prelude::*; use web_sys::console; @@ -10,12 +9,12 @@ use crate::filesystem::node::Node; use crate::shell::lexer::Statement; pub struct Exec { - root: Arc>, - current: Arc>, + root: Rc>, + current: Rc>, } impl Exec { - pub fn new(root: Arc>, current: Arc>) -> Exec { + pub fn new(root: Rc>, current: Rc>) -> Exec { Exec { root, current, @@ -111,6 +110,13 @@ impl Exec { "cd command".to_string() } + fn mkdir(&mut self, new_dir: String) -> String { + // let new_node = Rc::new(RefCell::new(Node::new(new_dir, NodeType::Directory, Some(Rc::clone(&self.current)))); + // self.current.borrow_mut().add_child(Rc::clone(&new_node)); + // Ok(JsValue::from_str("Success")) + "mkdir command".to_string() + } + pub fn execute(&mut self, input: Statement) -> String { match &input.command[..] { "" => " ".to_owned(),