203 lines
7.5 KiB
Python
203 lines
7.5 KiB
Python
import streamlit as st
|
|
from streamlit_arborist import tree_view
|
|
from pathlib import Path
|
|
from backend.managers.file_manager import FileManager
|
|
|
|
fm = FileManager()
|
|
|
|
SUFFIX_MAP = {
|
|
".py": "🐍", # Python
|
|
".js": "🟨", # JavaScript (Gelbes Quadrat/Logo)
|
|
".html": "🌐", # HTML (Web)
|
|
".css": "🎨", # CSS (Styling)
|
|
".json": "📦", # JSON (Datenpaket)
|
|
".yaml": "⚙️", # YAML (Konfiguration)
|
|
".yml": "⚙️", # YAML
|
|
".sh": "🐚", # Bash/Shell (Shell-Icon)
|
|
".md": "📝", # Markdown
|
|
".txt": "📄", # Text
|
|
".tex": "📑", # LaTeX
|
|
".c": "🔵", # C (Blaues Icon)
|
|
".cpp": "🔷", # C++
|
|
".java": "☕", # Java
|
|
"folder": "📁", # Ordner
|
|
"default": "📄" # Unbekannt
|
|
}
|
|
|
|
|
|
# ── Modals ────────────────────────────────────────────────────────────────────
|
|
|
|
@st.dialog("Delete Folder")
|
|
def _delete_folder_dialog(folder_rel: str, folder_name: str):
|
|
st.warning(f"Delete **{folder_name}** and all its contents? This cannot be undone.")
|
|
col1, col2 = st.columns(2)
|
|
with col1:
|
|
if st.button("Delete", type="primary", use_container_width=True):
|
|
if fm.delete_folder(folder_rel):
|
|
st.session_state.selected_folder = None
|
|
st.session_state.selected_folder_rel = None
|
|
st.rerun()
|
|
else:
|
|
st.error("Delete failed.")
|
|
with col2:
|
|
if st.button("Cancel", use_container_width=True):
|
|
st.rerun()
|
|
|
|
|
|
@st.dialog("Add File")
|
|
def _add_file_dialog(parent_path: str = ""):
|
|
name = st.text_input("File name:", placeholder="e.g. script.py")
|
|
col1, col2 = st.columns(2)
|
|
with col1:
|
|
if st.button("Create", type="primary", use_container_width=True):
|
|
if not name.strip():
|
|
st.warning("Please enter a file name.")
|
|
elif "/" in name or "\\" in name:
|
|
st.warning("Name must not contain slashes.")
|
|
else:
|
|
if fm.create_file(parent_path, name.strip()):
|
|
st.rerun()
|
|
with col2:
|
|
if st.button("Cancel", use_container_width=True):
|
|
st.rerun()
|
|
|
|
|
|
@st.dialog("Add Folder")
|
|
def _add_folder_dialog(parent_path: str = ""):
|
|
name = st.text_input("Folder name:", placeholder="e.g. utils")
|
|
col1, col2 = st.columns(2)
|
|
with col1:
|
|
if st.button("Create", type="primary", use_container_width=True):
|
|
if not name.strip():
|
|
st.warning("Please enter a folder name.")
|
|
elif "/" in name or "\\" in name:
|
|
st.warning("Name must not contain slashes.")
|
|
else:
|
|
if fm.create_folder(parent_path, name.strip()):
|
|
st.rerun()
|
|
with col2:
|
|
if st.button("Cancel", use_container_width=True):
|
|
st.rerun()
|
|
|
|
|
|
# ── File tree ─────────────────────────────────────────────────────────────────
|
|
|
|
def build_arborist_tree(tree, parent_path=Path()):
|
|
nodes = []
|
|
|
|
for name, content in sorted(tree.items()):
|
|
full_path = parent_path / name
|
|
node_id = str(full_path.as_posix())
|
|
|
|
if isinstance(content, dict):
|
|
nodes.append({
|
|
"id": node_id,
|
|
"name": f"{name}",
|
|
"children": build_arborist_tree(content, full_path)
|
|
})
|
|
else:
|
|
suffix = Path(name).suffix
|
|
icon = SUFFIX_MAP.get(suffix, SUFFIX_MAP["default"])
|
|
|
|
nodes.append({
|
|
"id": node_id,
|
|
"name": f"{icon} {name}",
|
|
"key": f"key_{node_id}"
|
|
})
|
|
|
|
return nodes
|
|
|
|
|
|
def render_filetree_arborist(tree):
|
|
data = build_arborist_tree(tree)
|
|
|
|
selected = tree_view(
|
|
data=data,
|
|
icons={"open": "📂", "closed": "📁"},
|
|
height=200,
|
|
selection=None,
|
|
select_internal_nodes=True,
|
|
open_by_default=False
|
|
)
|
|
|
|
return selected
|
|
|
|
|
|
# ── Sidebar ───────────────────────────────────────────────────────────────────
|
|
|
|
def render_sidebar():
|
|
st.sidebar.title("Navigation")
|
|
|
|
navigation_section = st.sidebar.container()
|
|
file_explorer_section = st.sidebar.container()
|
|
|
|
with navigation_section:
|
|
st.radio(
|
|
"Options",
|
|
["Chat with AI Assistant", "Code Editor"],
|
|
key="radio_interface_options")
|
|
|
|
st.markdown("---")
|
|
|
|
with file_explorer_section:
|
|
st.subheader("File Explorer")
|
|
tree = fm.get_file_tree()
|
|
workspace = st.container()
|
|
add_more = st.container()
|
|
|
|
with workspace:
|
|
with st.container(border=False):
|
|
if not tree:
|
|
st.info("Workspace is empty.")
|
|
else:
|
|
selected = render_filetree_arborist(tree)
|
|
|
|
if selected:
|
|
selected_path = selected.get("id")
|
|
|
|
if st.session_state.last_selected != selected_path:
|
|
st.session_state.last_selected = selected_path
|
|
abs_path = fm.base_path / selected_path
|
|
|
|
if abs_path.is_file():
|
|
st.session_state.selected_folder = None
|
|
st.session_state.selected_folder_rel = None
|
|
file_str = str(abs_path)
|
|
if file_str not in st.session_state.open_files:
|
|
st.session_state.open_files.append(file_str)
|
|
st.session_state.active_file = file_str
|
|
st.rerun()
|
|
|
|
elif abs_path.is_dir():
|
|
st.session_state.selected_folder = str(abs_path)
|
|
st.session_state.selected_folder_rel = selected_path
|
|
st.rerun()
|
|
|
|
# Folder actions — rendered outside the selection block so they persist across reruns
|
|
if st.session_state.get("selected_folder"):
|
|
folder_name = Path(st.session_state.selected_folder).name
|
|
folder_rel = st.session_state.selected_folder_rel
|
|
|
|
with st.container(border=True):
|
|
st.write(f"**Folder actions:** 📁 {folder_name}")
|
|
if st.button("Add File", key="btn_add_file_in_folder", use_container_width=True):
|
|
_add_file_dialog(folder_rel)
|
|
if st.button("Add Folder", key="btn_add_folder_in_folder", use_container_width=True):
|
|
_add_folder_dialog(folder_rel)
|
|
if st.button("Delete Folder", key="btn_delete_folder", use_container_width=True):
|
|
_delete_folder_dialog(folder_rel, folder_name)
|
|
|
|
with add_more:
|
|
with st.popover("⚙️ Explorer Options", key="popover_options", use_container_width=True):
|
|
if st.button("Add File", key="btn_add_file", use_container_width=True):
|
|
_add_file_dialog("")
|
|
|
|
if st.button("Add Folder", key="btn_add_folder", use_container_width=True):
|
|
_add_folder_dialog("")
|
|
|
|
return
|
|
|
|
|
|
if __name__ == "__main__":
|
|
render_sidebar()
|