Skip to content

Commit

Permalink
Refactoring prompts.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
shettysach committed Feb 21, 2024
1 parent f60a011 commit 3fb5bd5
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 103 deletions.
2 changes: 1 addition & 1 deletion src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use leptos::{component, create_signal, view, For, IntoView, SignalGet};
use std::collections::VecDeque;

mod banner;
mod prompt;
use banner::Banner;
mod prompt;
use prompt::Prompt;

#[component]
Expand Down
127 changes: 27 additions & 100 deletions src/base/prompt.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
use leptos::ev::{keydown, KeyboardEvent, SubmitEvent};
use leptos::ev::SubmitEvent;
use leptos::html::{Form, Input};
use leptos::{
component, create_effect, create_node_ref, create_signal, spawn_local, view, IntoView, NodeRef,
ReadSignal, SignalGetUntracked, SignalUpdate, WriteSignal,
ReadSignal, WriteSignal,
};
use leptos_use::{
use_color_mode_with_options, use_cycle_list_with_options, use_event_listener, ColorMode,
UseColorModeOptions, UseColorModeReturn, UseCycleListOptions, UseCycleListReturn,
use_color_mode_with_options, use_cycle_list_with_options, ColorMode, UseColorModeOptions,
UseColorModeReturn, UseCycleListOptions, UseCycleListReturn,
};
use std::collections::VecDeque;

mod general;
use general::general_commands;
mod keyboard;
use keyboard::keyboard_commands;

#[component]
pub fn Prompt(
submitter: WriteSignal<u8>,
Expand All @@ -18,12 +23,21 @@ pub fn Prompt(
) -> 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 (history_index, set_history_index): (ReadSignal<u8>, WriteSignal<u8>) = create_signal(0);

//Form and input elements
let form_element: NodeRef<Form> = create_node_ref();
let input_element: NodeRef<Input> = create_node_ref();

// Focus on the new prompt on mount
create_effect(move |_| {
if let Some(ref_input) = input_element.get() {
let _ = ref_input.on_mount(|input| {
let _ = input.focus();
});
}
});

//Themes
let UseColorModeReturn { mode, set_mode, .. } = use_color_mode_with_options(
UseColorModeOptions::default()
Expand All @@ -48,108 +62,21 @@ pub fn Prompt(
let next = next.clone();

spawn_local(async move {
let value = value.trim().replace("<", "‹").replace(">", "›");
let val = value.split_once(' ').unwrap_or((&value, ""));

match val.0 {
"clear" => {
submitter.update(|prompts| {
*prompts = 0;
});
}
"history" => {
let hist: Vec<String> = history.get_untracked().into();
let hist: Vec<String> = hist
.iter()
.rev()
.enumerate()
.map(|(i, c)| format!("{} {}", i + 1, c))
.collect();
set_out(hist.join("\n"));
}
"theme" | "t" | "wal" => {
next();
let new_theme = state.get_untracked();
set_out(format!(
r#"Theme changed to: <b class="grn">{new_theme}</b>"#
));
}
_ => set_out(termfolio::Command::process(val.0, val.1).await),
}

updater.update(|hist| {
if !value.is_empty() && hist.front() != Some(&value) {
hist.push_front(value);
if hist.len() > 20 {
hist.pop_back();
}
}
});

submitter.update(|prompts| {
if *prompts < u8::MAX {
*prompts += 1;
}
});
general_commands(value, state, next, set_out, submitter, updater, history).await
});

form_element().unwrap().set_inert(true);
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| {
let _ = input.focus();
});
}
});

// 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();
let inp = input_element.get_untracked().unwrap();

match &ev.key()[..] {
//Previous command in history
"ArrowUp" => {
ev.prevent_default();
if index < hist.len() {
inp.set_value(&hist[index]);
set_history_index.update(|history_index| *history_index += 1);
}
}
//Next command in history
"ArrowDown" => {
if index > 1 {
inp.set_value(&hist[index - 2]);
set_history_index.update(|history_index| *history_index -= 1);
} else if index != 0 {
inp.set_value("");
set_history_index.update(|history_index| *history_index -= 1);
}
}
//Autocomplete
"Tab" => {
ev.prevent_default();
inp.set_value(termfolio::autocomplete(&inp.value()));
}
_ => {}
}

//Clear
if (ev.ctrl_key() || ev.meta_key()) && (ev.key() == "l" || ev.key() == "L") {
ev.prevent_default();
submitter.update(|prompts| {
*prompts = 0;
});
submitter.update(|prompts| {
*prompts += 1;
});
}
});
keyboard_commands(
input_element,
history,
history_index,
set_history_index,
submitter,
);

view! {
<form
Expand Down
59 changes: 59 additions & 0 deletions src/base/prompt/general.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use leptos::{ReadSignal, Signal, SignalGetUntracked, SignalUpdate, WriteSignal};
use leptos_use::ColorMode;
use std::collections::VecDeque;

pub async fn general_commands<F>(
value: String,
state: Signal<ColorMode>,
next: F,
set_out: WriteSignal<String>,
submitter: WriteSignal<u8>,
updater: WriteSignal<VecDeque<String>>,
history: ReadSignal<VecDeque<String>>,
) where
F: Fn(),
{
let value = value.trim().replace("<", "‹").replace(">", "›");
let val = value.split_once(' ').unwrap_or((&value, ""));

match val.0 {
"clear" => {
submitter.update(|prompts| {
*prompts = 0;
});
}
"history" => {
let hist: Vec<String> = history.get_untracked().into();
let hist: Vec<String> = hist
.iter()
.rev()
.enumerate()
.map(|(i, c)| format!("{} {}", i + 1, c))
.collect();
set_out(hist.join("\n"));
}
"theme" | "t" | "wal" => {
next();
let new_theme = state.get_untracked();
set_out(format!(
r#"Theme changed to: <b class="grn">{new_theme}</b>"#
));
}
_ => set_out(termfolio::Command::process(val.0, val.1).await),
}

updater.update(|hist| {
if !value.is_empty() && hist.front() != Some(&value) {
hist.push_front(value);
if hist.len() > 20 {
hist.pop_back();
}
}
});

submitter.update(|prompts| {
if *prompts < u8::MAX {
*prompts += 1;
}
});
}
57 changes: 57 additions & 0 deletions src/base/prompt/keyboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use leptos::ev::{keydown, KeyboardEvent};
use leptos::html::Input;
use leptos::{NodeRef, ReadSignal, SignalGetUntracked, SignalUpdate, WriteSignal};
use leptos_use::use_event_listener;
use std::collections::VecDeque;

pub fn keyboard_commands(
input_element: NodeRef<Input>,
history: ReadSignal<VecDeque<String>>,
history_index: ReadSignal<u8>,
set_history_index: WriteSignal<u8>,
submitter: WriteSignal<u8>,
) {
let _ = use_event_listener(input_element, keydown, move |ev: KeyboardEvent| {
let index = history_index.get_untracked().into();
let hist = history.get_untracked();
let inp = input_element.get_untracked().unwrap();

match &ev.key()[..] {
//Previous command in history
"ArrowUp" => {
ev.prevent_default();
if index < hist.len() {
inp.set_value(&hist[index]);
set_history_index.update(|history_index| *history_index += 1);
}
}
//Next command in history
"ArrowDown" => {
if index > 1 {
inp.set_value(&hist[index - 2]);
set_history_index.update(|history_index| *history_index -= 1);
} else if index == 1 {
inp.set_value("");
set_history_index.update(|history_index| *history_index -= 1);
}
}
//Autocomplete
"Tab" => {
ev.prevent_default();
inp.set_value(termfolio::autocomplete(&inp.value()));
}
_ => {}
}

//Clear
if (ev.ctrl_key() || ev.meta_key()) && (ev.key() == "l" || ev.key() == "L") {
ev.prevent_default();
submitter.update(|prompts| {
*prompts = 0;
});
submitter.update(|prompts| {
*prompts += 1;
});
}
});
}
3 changes: 2 additions & 1 deletion src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ mod formats;
use crate::texts::{FETCH_GITHUB_ERROR, READ_JSON_ERROR};
use formats::*;

// Config
// Config JSON

const JSON: &str = include_str!("../configs/config.json");

// Once statics
Expand Down
6 changes: 5 additions & 1 deletion src/texts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,25 @@ pub const CREDITS: &str = r#"<span class="grn"> _____ ______________ _________
| | | |___| |\ \| | | || | \ \_/ / |_____| |_\ \_/ /
\_/ \____/\_| \_\_| |_/\_| \___/\_____/\___/ \___/
</span>
Terminal style portfolio website, made using Rust.
Terminal style portfolio website, made in Leptos, Rust.
Made by <span class="rd semibold">Sachith C Shetty</span>
<a href="https://github.com/shettysach" target="_blank" class="blu semibold">Github</a>: github.com/shettysach
<a href="https://github.com/shettysach/termfolio" target="_blank" class="blu semibold">Repo</a>: github.com/shettysach/termfolio
<span class="rd semibold">APIs used -</span>
* <a
href="https://docs.github.com/en/rest/about-the-rest-api"
target="_blank"
class="blu semibold">Github REST API</a>
* <a
href="https://github.com/Ysn4Irix/gh-pinned-repos-api"
target="_blank"
class="blu semibold">Pinned repos</a> - Ysn4Irix/gh-pinned-repos-api
* <a
href="https://github.com/idealclover/GitHub-Star-Counter"
target="_blank"
Expand Down

0 comments on commit 3fb5bd5

Please sign in to comment.