feat(nvim): display active file in status bar and use url in editor

This commit is contained in:
Pihkaal
2024-06-01 00:26:23 +02:00
parent 96207863a9
commit 5651a42195
6 changed files with 72 additions and 62 deletions

View File

@@ -1,3 +1,8 @@
export const NvimEditor = (props: { source: string | undefined }) => ( import { useState } from "react";
<div className="h-full">{props.source}</div>
); export const NvimEditor = (props: { source: string | undefined }) => {
const [data, setData] = useState<string>();
const [loading, setLoading] = useState(false);
return <div className="h-full">{props.source}</div>;
};

View File

@@ -1,17 +1,22 @@
import { ICONS, getIcon } from "~/utils/icons";
export const NvimStatusBar = (props: { export const NvimStatusBar = (props: {
label: string; label: string;
labelColor: string; labelColor: string;
fileIcon?: string;
fileName: string; fileName: string;
}) => ( }) => (
<div className="bg-[#29293c]"> <div className="bg-[#29293c]">
<span className="text-black" style={{ background: props.labelColor }}> <span className="text-[#272332]" style={{ background: props.labelColor }}>
{` ${props.label} `} {` ${props.label} `}
</span> </span>
<span className="text-[#474353]" style={{ background: props.labelColor }}> <span className="text-[#474353]" style={{ background: props.labelColor }}>
{"\ue0ba"} {"\ue0ba"}
</span> </span>
<span className="bg-[#474353] text-[#373040]">{"\ue0ba"}</span> <span className="bg-[#474353] text-[#373040]">{"\ue0ba"}</span>
<span className="bg-[#373040] text-white">{` ${props.fileName} `}</span> <span className="bg-[#373040]">{` ${getIcon(props.fileIcon).char}${
props.fileName
} `}</span>
<span className="bg-[#373040] text-[#29293c]">{"\ue0ba"}</span> <span className="bg-[#373040] text-[#29293c]">{"\ue0ba"}</span>
</div> </div>
); );

View File

@@ -1,4 +1,4 @@
import { ICONS } from "~/utils/icons"; import { getIcon } from "~/utils/icons";
import { File } from "~/utils/types"; import { File } from "~/utils/types";
export const NvimTreeFile = (props: { export const NvimTreeFile = (props: {
@@ -8,17 +8,7 @@ export const NvimTreeFile = (props: {
inDirectory: boolean | "last"; inDirectory: boolean | "last";
onSelect: (y: number) => void; onSelect: (y: number) => void;
onOpen: (file: File) => void; onOpen: (file: File) => void;
}) => { }) => (
let iconName = props.file.icon;
if (!iconName) {
const parts = props.file.name.split(".");
iconName = parts[parts.length - 1];
}
if (!ICONS[iconName]) iconName = "UNKNOWN";
const icon = ICONS[iconName];
return (
<li <li
style={{ background: props.selected ? "#504651" : "" }} style={{ background: props.selected ? "#504651" : "" }}
onMouseDown={() => props.onSelect(props.y)} onMouseDown={() => props.onSelect(props.y)}
@@ -30,7 +20,9 @@ export const NvimTreeFile = (props: {
{props.inDirectory === "last" ? "└ " : "│ "} {props.inDirectory === "last" ? "└ " : "│ "}
</span> </span>
)} )}
<span style={{ color: icon.color }}>{`${icon.char}`}</span> <span style={{ color: getIcon(props.file).color }}>{`${
getIcon(props.file).char
}`}</span>
{props.file.name === "README.md" ? ( {props.file.name === "README.md" ? (
<span className="font-bold text-[#d8c5a1]">README.md</span> <span className="font-bold text-[#d8c5a1]">README.md</span>
) : ( ) : (
@@ -38,4 +30,3 @@ export const NvimTreeFile = (props: {
)} )}
</li> </li>
); );
};

View File

@@ -159,20 +159,3 @@ export const NvimTree = (
</div> </div>
); );
}; };
type FileIcon = {
char: string;
color: string;
};
const FILE_ICONS: Record<string, FileIcon> = {
md: {
char: " ",
color: "#89bafa",
},
asc: {
char: "󰷖 ",
color: "#f9e2af",
},
UNKNOWN: { char: "󰈚 ", color: "#f599ae" },
};

View File

@@ -10,15 +10,21 @@ import { File } from "~/utils/types";
export const Nvim = (_props: {}) => { export const Nvim = (_props: {}) => {
const kitty = useKitty(); const kitty = useKitty();
const [activeFile, setActiveFile] = useState<string>(); const [activeFile, setActiveFile] = useState<{
name: string;
url: string;
icon?: string;
}>();
const handleOpenFile = (file: File) => { const handleOpenFile = (file: File) => {
if (file.type === "link") { if (file.type === "link") {
window.open(file.url, "_blank")?.focus(); window.open(file.url, "_blank")?.focus();
} else { } else {
setActiveFile( setActiveFile({
`https://raw.githubusercontent.com/pihkaal/${file.repo}/main/${file.fileName}`, name: file.repo === "pihkaal" ? file.fileName : file.repo,
); icon: file.icon,
url: `https://raw.githubusercontent.com/pihkaal/${file.repo}/main/${file.fileName}`,
});
} }
}; };
@@ -36,13 +42,14 @@ export const Nvim = (_props: {}) => {
<NvimTree {...kitty} onOpen={handleOpenFile} /> <NvimTree {...kitty} onOpen={handleOpenFile} />
</div> </div>
<div style={{ gridArea: "1 / 2 / 1 / 3", overflow: "scroll" }}> <div style={{ gridArea: "1 / 2 / 1 / 3", overflow: "scroll" }}>
<NvimEditor source={activeFile} /> <NvimEditor source={activeFile?.url} />
</div> </div>
<div style={{ gridArea: "2 / 1 / 2 / 3" }}> <div style={{ gridArea: "2 / 1 / 2 / 3" }}>
<NvimStatusBar <NvimStatusBar
label="INSERT" label=" NORMAL"
labelColor="#7ea7ca" labelColor="#7ea7ca"
fileName="README.md" fileIcon={activeFile?.icon}
fileName={activeFile?.name ?? `NvimTree_1`}
/> />
</div> </div>
<div style={{ gridArea: "3 / 1 / 3 / 3" }}> <div style={{ gridArea: "3 / 1 / 3 / 3" }}>

View File

@@ -1,4 +1,23 @@
import { Icon } from "./types"; import { File, Icon } from "./types";
export const getIcon = (file: File | string | undefined): Icon => {
if (file === undefined) return ICONS["UNKNOWN"];
let iconName;
if (typeof file === "string") {
const parts = file.split(".");
iconName = parts[parts.length - 1];
} else {
iconName = file.icon;
if (!iconName) {
const parts = file.name.split(".");
iconName = parts[parts.length - 1];
}
}
if (!ICONS[iconName]) iconName = "UNKNOWN";
return ICONS[iconName];
};
export const ICONS: Record<string, Icon> = { export const ICONS: Record<string, Icon> = {
md: { md: {