From 209f631226185e6a463b6d37045f5651d23834a3 Mon Sep 17 00:00:00 2001 From: Sachith Date: Thu, 1 Feb 2024 22:12:13 +0530 Subject: [PATCH] Fixes and refactoring --- src/base.rs | 31 +++++++++++++++++++++++++++++++ src/{ => base}/prompt.rs | 13 +++++++++---- src/lib.rs | 8 ++++---- src/main.rs | 32 +++----------------------------- src/texts.rs | 15 ++++++++++++--- styles/styles.css | 6 ++++-- 6 files changed, 63 insertions(+), 42 deletions(-) create mode 100644 src/base.rs rename src/{ => base}/prompt.rs (93%) diff --git a/src/base.rs b/src/base.rs new file mode 100644 index 0000000..92111d0 --- /dev/null +++ b/src/base.rs @@ -0,0 +1,31 @@ +use leptos::{component, create_signal, view, For, IntoView, SignalGet}; +use std::collections::VecDeque; + +mod prompt; +use prompt::Prompt; + +#[component] +pub fn Base() -> impl IntoView { + // Signals for number of prompts and history vector + let (prompts, set_prompts) = create_signal(1); + let (history, set_history) = create_signal(VecDeque::new()); + + let prompt_list = move || (0..prompts.get()).collect::>(); + + view! { +
+ + } + } + /> +
+ } +} diff --git a/src/prompt.rs b/src/base/prompt.rs similarity index 93% rename from src/prompt.rs rename to src/base/prompt.rs index 767b392..1bf17cc 100644 --- a/src/prompt.rs +++ b/src/base/prompt.rs @@ -17,12 +17,15 @@ pub fn Prompt( updater: WriteSignal>, history: ReadSignal>, ) -> impl IntoView { + //Output and history index signals let (out, set_out) = create_signal(String::new()); let (history_index, set_history_index) = create_signal(0); - let input_element: NodeRef = create_node_ref(); + //Form and input elements let form_element: NodeRef
= create_node_ref(); + let input_element: NodeRef = create_node_ref(); + //Themes let UseColorModeReturn { mode, set_mode, .. } = use_color_mode_with_options( UseColorModeOptions::default() .custom_modes(vec!["catppuccin".into(), "nord".into(), "classic".into()]) @@ -39,14 +42,15 @@ pub fn Prompt( UseCycleListOptions::default().initial_value(Some((mode, set_mode).into())), ); + //On submit let on_submit = move |ev: SubmitEvent| { ev.prevent_default(); let value = input_element().unwrap().value(); let next = next.clone(); spawn_local(async move { - let val = value.trim(); - let val = val.split_once(' ').unwrap_or((val, " ")); + let value = value.trim().replace("<", "‹").replace(">", "›"); + let val = value.split_once(' ').unwrap_or((&value, " ")); match val.0 { "clear" => { @@ -76,7 +80,6 @@ pub fn Prompt( updater.update(|hist| { if value != "" && hist.front() != Some(&value) { - let value = value.replace("<", "‹").replace(">", "›"); hist.push_front(value); if hist.len() > 20 { hist.pop_back(); @@ -93,6 +96,7 @@ pub fn Prompt( input_element().unwrap().set_inert(true); }; + // Focus on the new prompt on mount create_effect(move |_| { if let Some(ref_input) = input_element.get() { let _ = ref_input.on_mount(|input| { @@ -101,6 +105,7 @@ pub fn Prompt( } }); + // Event listener for Up and Down arrow keys, Tab and Ctrl/Command + L let _ = use_event_listener(input_element, keydown, move |ev: KeyboardEvent| { let index = history_index.get_untracked(); let hist = history.get_untracked(); diff --git a/src/lib.rs b/src/lib.rs index e17f6d6..51ca6c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,7 +56,7 @@ pub enum Bash { You, Echo(String), Nothing, - Invalid, + Invalid(String), } impl Bash { @@ -75,7 +75,7 @@ impl Bash { "whoami" => Self::You, "echo" => Self::Echo(String::from(inp1)), "" => Self::Nothing, - _ => Self::Invalid, + _ => Self::Invalid(String::from(inp0)), } } @@ -92,9 +92,9 @@ impl Bash { Self::Edit => String::from("Nothing to change."), Self::Power => String::from("With great power comes great responsibility."), Self::You => String::from("Despite everything, it's still you."), - Self::Echo(s) => String::from(s).replace("<", "‹").replace(">", "›"), + Self::Echo(s) => String::from(s), Self::Nothing => String::new(), - _ => String::from("Command not found..."), + Self::Invalid(s) => format!("{s}: command not found",), } } } diff --git a/src/main.rs b/src/main.rs index 4d1d1a3..ff3e6e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,34 +1,8 @@ -use leptos::{component, create_signal, view, For, IntoView, SignalGet}; -use std::collections::VecDeque; +use leptos::view; -mod prompt; -use prompt::Prompt; +mod base; +use base::Base; fn main() { leptos::mount_to_body(|| view! { }); } - -#[component] -fn Base() -> impl IntoView { - let (prompts, set_prompts) = create_signal(1); - let (history, set_history) = create_signal(VecDeque::new()); - - let prompt_list = move || (0..prompts.get()).collect::>(); - - view! { -
- - } - } - /> -
- } -} diff --git a/src/texts.rs b/src/texts.rs index 5750f7c..abe520a 100644 --- a/src/texts.rs +++ b/src/texts.rs @@ -31,9 +31,18 @@ Made by Sachith C Shetty Repo: github.com/shettysach/termfolio APIs used - -* Github API -* Pinned repos - Ysn4Irix/gh-pinned-repos-api -* Total stars and forks - idealclover/GitHub-Star-Counter"#; +* Github REST API +* Pinned repos - Ysn4Irix/gh-pinned-repos-api +* Total stars and forks - idealclover/GitHub-Star-Counter"#; pub const READ_JSON_ERROR: &str = r#"Error reading config.json"#; pub const FETCH_GITHUB_ERROR: &str = diff --git a/styles/styles.css b/styles/styles.css index c73b53a..68f4087 100644 --- a/styles/styles.css +++ b/styles/styles.css @@ -61,6 +61,7 @@ form { .inline { display: inline; + font-weight: 500; color: var(--green); } @@ -119,7 +120,8 @@ form { font-size: 35px; } +/* Links */ + a { - text-decoration-thickness: 1px; - font-weight: 500; + text-decoration-thickness: 2px; }