Skip to content

Commit 123b950

Browse files
committed
🎨 implement advanced tab actions
1 parent a5f352d commit 123b950

File tree

2 files changed

+99
-4
lines changed

2 files changed

+99
-4
lines changed

src/components/Terminal.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ import {
1818
MobileSpan,
1919
Wrapper,
2020
} from "./styles/Terminal.styled";
21+
import { argTab } from "../utils/funcs";
2122

2223
type Command = {
2324
cmd: string;
24-
desc?: string;
25-
tab?: number;
25+
desc: string;
26+
tab: number;
2627
}[];
2728

2829
export const commands: Command = [
@@ -116,10 +117,23 @@ const Terminal = () => {
116117
hintsCmds = [...hintsCmds, cmd];
117118
}
118119
});
120+
121+
const returnedHints = argTab(inputVal, setInputVal, setHints, hintsCmds);
122+
hintsCmds = returnedHints ? [...hintsCmds, ...returnedHints] : hintsCmds;
123+
124+
// if there are many command to autocomplete
119125
if (hintsCmds.length > 1) {
120126
setHints(hintsCmds);
121-
} else if (hintsCmds.length === 1) {
122-
setInputVal(hintsCmds[0]);
127+
}
128+
// if only one command to autocomplete
129+
else if (hintsCmds.length === 1) {
130+
const currentCmd = _.split(inputVal, " ");
131+
setInputVal(
132+
currentCmd.length !== 1
133+
? `${currentCmd[0]} ${currentCmd[1]} ${hintsCmds[0]}`
134+
: hintsCmds[0]
135+
);
136+
123137
setHints([]);
124138
}
125139
}

src/utils/funcs.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import _ from "lodash";
2+
import theme from "../components/styles/themes";
23

34
/**
45
* Generates html tabs
@@ -68,3 +69,83 @@ export const checkThemeRedirect = (
6869
return false;
6970
}
7071
};
72+
73+
/**
74+
* Perform advanced tab actions
75+
* @param {string} inputVal - current input value
76+
* @param {(value: React.SetStateAction<string>) => void} setInputVal - setInputVal setState
77+
* @param {(value: React.SetStateAction<string[]>) => void} setHints - setHints setState
78+
* @param {hintsCmds} hintsCmds - hints command array
79+
* @returns {string[] | undefined} hints command or setState action(undefined)
80+
*/
81+
export const argTab = (
82+
inputVal: string,
83+
setInputVal: (value: React.SetStateAction<string>) => void,
84+
setHints: (value: React.SetStateAction<string[]>) => void,
85+
hintsCmds: string[]
86+
): string[] | undefined => {
87+
// 1) if input is 'themes '
88+
if (inputVal === "themes ") {
89+
setInputVal(`themes set`);
90+
return [];
91+
}
92+
93+
// 2) if input is 'themes s'
94+
else if (
95+
_.startsWith("themes", _.split(inputVal, " ")[0]) &&
96+
_.split(inputVal, " ")[1] !== "set" &&
97+
_.startsWith("set", _.split(inputVal, " ")[1])
98+
) {
99+
setInputVal(`themes set`);
100+
return [];
101+
}
102+
103+
// 3) if input is 'themes set '
104+
else if (inputVal === "themes set ") {
105+
setHints(_.keys(theme));
106+
return [];
107+
}
108+
109+
// 4) if input starts with 'themes set ' + theme
110+
else if (_.startsWith(inputVal, "themes set ")) {
111+
_.keys(theme).forEach((t) => {
112+
if (_.startsWith(t, _.split(inputVal, " ")[2])) {
113+
hintsCmds = [...hintsCmds, t];
114+
}
115+
});
116+
return hintsCmds;
117+
}
118+
119+
// 5) if input is 'projects' or 'socials'
120+
else if (inputVal === "projects " || inputVal === "socials ") {
121+
setInputVal(`${inputVal}go`);
122+
return [];
123+
}
124+
125+
// 6) if input is 'projects g' or 'socials g'
126+
else if (inputVal === "projects g" || inputVal === "socials g") {
127+
setInputVal(`${inputVal}o`);
128+
return [];
129+
}
130+
131+
// 7) if input is 'socials go '
132+
else if (_.startsWith(inputVal, "socials go ")) {
133+
["1.Github", "2.Dev.to", "3.Facebook", "4.Instagram"].forEach((t) => {
134+
hintsCmds = [...hintsCmds, t];
135+
});
136+
return hintsCmds;
137+
}
138+
139+
// 8) if input is 'projects go '
140+
else if (_.startsWith(inputVal, "projects go ")) {
141+
[
142+
"1.Sat Naing's Blog",
143+
"2.Haru Fashion",
144+
"3.Haru API",
145+
"4.Tip Calculator",
146+
].forEach((t) => {
147+
hintsCmds = [...hintsCmds, t];
148+
});
149+
return hintsCmds;
150+
}
151+
};

0 commit comments

Comments
 (0)