feat: Add nano
This commit is contained in:
@@ -140,7 +140,6 @@ def is_valid_binary(path: str) -> bool:
|
||||
if is_valid_path("/".join(parts[:-1])):
|
||||
files = get_nodes_in_directory("/".join(parts[:-1]))
|
||||
for item in files:
|
||||
print(item)
|
||||
if item["name"] == parts[-1] and item["type"] == 2:
|
||||
return True
|
||||
return False
|
||||
@@ -236,7 +235,6 @@ def ls():
|
||||
return json_response(request, {"output": f"ls: cannot access '{path}': No such file or directory"}, 200)
|
||||
|
||||
files = get_nodes_in_directory(path)
|
||||
print(files)
|
||||
output = [file["name"] for file in files]
|
||||
if all_files:
|
||||
output.insert(0, ".")
|
||||
@@ -420,7 +418,7 @@ def nano():
|
||||
return json_response(request, {"output": f"nano: cannot write to '{path}': Permission denied"}, 200)
|
||||
|
||||
if update_file_content(path, content):
|
||||
output = f"File '{path}' updated successfully"
|
||||
output = ""
|
||||
else:
|
||||
output = f"nano: failed to update '{path}'"
|
||||
else:
|
||||
|
||||
@@ -39,6 +39,52 @@
|
||||
flex: 1;
|
||||
caret-color: #0f0;
|
||||
}
|
||||
#nano-editor {
|
||||
display: none;
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
background: #000;
|
||||
z-index: 1000;
|
||||
flex-direction: column;
|
||||
}
|
||||
#nano-editor.active {
|
||||
display: flex;
|
||||
}
|
||||
#nano-header {
|
||||
padding: 10px 20px;
|
||||
border-bottom: 1px solid #0f0;
|
||||
color: #0f0;
|
||||
}
|
||||
#nano-textarea {
|
||||
flex: 1;
|
||||
background: #000;
|
||||
color: #fff;
|
||||
border: none;
|
||||
padding: 20px;
|
||||
font-family: 'Courier New', monospace;
|
||||
font-size: 16px;
|
||||
resize: none;
|
||||
outline: none;
|
||||
}
|
||||
#nano-footer {
|
||||
padding: 10px 20px;
|
||||
border-top: 1px solid #0f0;
|
||||
color: #0f0;
|
||||
font-size: 14px;
|
||||
}
|
||||
#nano-save-prompt {
|
||||
display: none;
|
||||
padding: 10px 20px;
|
||||
background: #111;
|
||||
border-top: 1px solid #0f0;
|
||||
color: #fff;
|
||||
}
|
||||
#nano-save-prompt.active {
|
||||
display: block;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
@@ -56,13 +102,27 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Nano Editor -->
|
||||
<div id="nano-editor">
|
||||
<div id="nano-header">nano - <span id="nano-filename"></span></div>
|
||||
<textarea id="nano-textarea"></textarea>
|
||||
<div id="nano-save-prompt">Save modified buffer? (Y/N)</div>
|
||||
<div id="nano-footer">^X Exit ^O Save (Ctrl+X to exit, Ctrl+O to save)</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const terminal = document.getElementById('terminal');
|
||||
const input = document.getElementById('command-input');
|
||||
const inputLine = document.getElementById('input-line');
|
||||
const nanoEditor = document.getElementById('nano-editor');
|
||||
const nanoTextarea = document.getElementById('nano-textarea');
|
||||
const nanoFilename = document.getElementById('nano-filename');
|
||||
let commandHistory = [];
|
||||
let historyIndex = -1;
|
||||
let currentPwd = '~';
|
||||
let nanoCurrentFile = '';
|
||||
let nanoOriginalContent = '';
|
||||
let nanoSavePromptActive = false;
|
||||
|
||||
// Function to update the prompt with current pwd
|
||||
async function updatePrompt() {
|
||||
@@ -78,6 +138,124 @@
|
||||
}
|
||||
}
|
||||
|
||||
// Function to open nano editor
|
||||
async function openNano(filename) {
|
||||
nanoCurrentFile = filename;
|
||||
nanoFilename.textContent = filename;
|
||||
nanoSavePromptActive = false;
|
||||
document.getElementById('nano-save-prompt').classList.remove('active');
|
||||
|
||||
// Try to fetch existing file content
|
||||
try {
|
||||
const parts = ['cat'].concat(filename.split(' '));
|
||||
const response = await fetch('/terminal/execute/cat', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ args: filename })
|
||||
});
|
||||
const data = await response.json();
|
||||
|
||||
if (data.output && !data.output.startsWith('cat:')) {
|
||||
nanoTextarea.value = data.output;
|
||||
nanoOriginalContent = data.output;
|
||||
} else {
|
||||
nanoTextarea.value = '';
|
||||
nanoOriginalContent = '';
|
||||
}
|
||||
} catch (err) {
|
||||
nanoTextarea.value = '';
|
||||
nanoOriginalContent = '';
|
||||
}
|
||||
|
||||
nanoEditor.classList.add('active');
|
||||
nanoTextarea.focus();
|
||||
}
|
||||
|
||||
// Function to close nano editor
|
||||
async function closeNano(shouldSave = false) {
|
||||
if (shouldSave) {
|
||||
await saveNano();
|
||||
}
|
||||
|
||||
nanoEditor.classList.remove('active');
|
||||
nanoSavePromptActive = false;
|
||||
document.getElementById('nano-save-prompt').classList.remove('active');
|
||||
terminal.appendChild(inputLine);
|
||||
inputLine.style.display = 'block';
|
||||
input.disabled = false;
|
||||
input.focus();
|
||||
terminal.scrollTop = terminal.scrollHeight;
|
||||
}
|
||||
|
||||
// Function to save nano content
|
||||
async function saveNano() {
|
||||
const content = nanoTextarea.value;
|
||||
try {
|
||||
const response = await fetch('/terminal/execute/nano', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ args: `${nanoCurrentFile} CONTENT ${content}` })
|
||||
});
|
||||
const data = await response.json();
|
||||
|
||||
nanoOriginalContent = content; // Update original content after save
|
||||
// If output contains error, display it
|
||||
if (data.output && data.output.startsWith('nano:')) {
|
||||
const errorLine = document.createElement('div');
|
||||
errorLine.className = 'line error';
|
||||
errorLine.textContent = data.output;
|
||||
terminal.appendChild(errorLine);
|
||||
}
|
||||
|
||||
// Don't display output - nano saves silently
|
||||
} catch (err) {
|
||||
const errorLine = document.createElement('div');
|
||||
errorLine.className = 'line error';
|
||||
errorLine.textContent = 'Error saving file: ' + err.message;
|
||||
terminal.appendChild(errorLine);
|
||||
}
|
||||
}
|
||||
|
||||
// Check if content has changed
|
||||
function hasUnsavedChanges() {
|
||||
return nanoTextarea.value !== nanoOriginalContent;
|
||||
}
|
||||
|
||||
// Nano editor keyboard shortcuts
|
||||
nanoTextarea.addEventListener('keydown', async (e) => {
|
||||
if (e.ctrlKey && e.key === 'x') {
|
||||
e.preventDefault();
|
||||
|
||||
if (nanoSavePromptActive) {
|
||||
return; // Ignore if prompt is already active
|
||||
}
|
||||
|
||||
if (hasUnsavedChanges()) {
|
||||
// Show save prompt
|
||||
nanoSavePromptActive = true;
|
||||
document.getElementById('nano-save-prompt').classList.add('active');
|
||||
document.getElementById('nano-footer').textContent = 'Press Y to save, N to discard, Ctrl+C to cancel';
|
||||
} else {
|
||||
await closeNano(false);
|
||||
}
|
||||
} else if (e.ctrlKey && e.key === 'o') {
|
||||
e.preventDefault();
|
||||
await saveNano();
|
||||
} else if (e.ctrlKey && e.key === 'c' && nanoSavePromptActive) {
|
||||
e.preventDefault();
|
||||
// Cancel save prompt
|
||||
nanoSavePromptActive = false;
|
||||
document.getElementById('nano-save-prompt').classList.remove('active');
|
||||
document.getElementById('nano-footer').textContent = '^X Exit ^O Save (Ctrl+X to exit, Ctrl+O to save)';
|
||||
} else if (nanoSavePromptActive && (e.key === 'y' || e.key === 'Y')) {
|
||||
e.preventDefault();
|
||||
await closeNano(true);
|
||||
} else if (nanoSavePromptActive && (e.key === 'n' || e.key === 'N')) {
|
||||
e.preventDefault();
|
||||
await closeNano(false);
|
||||
}
|
||||
});
|
||||
|
||||
// Update prompt on load
|
||||
updatePrompt();
|
||||
|
||||
@@ -118,12 +296,30 @@
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle nano command specially
|
||||
const parts = command.split(' ');
|
||||
const baseCommand = parts[0];
|
||||
|
||||
if (baseCommand === 'nano') {
|
||||
if (parts.length < 2) {
|
||||
const errorLine = document.createElement('div');
|
||||
errorLine.className = 'line output';
|
||||
errorLine.textContent = 'Usage: nano [filename]';
|
||||
terminal.appendChild(errorLine);
|
||||
terminal.appendChild(inputLine);
|
||||
inputLine.style.display = 'block';
|
||||
input.disabled = false;
|
||||
input.focus();
|
||||
return;
|
||||
}
|
||||
await openNano(parts.slice(1).join(' '));
|
||||
return;
|
||||
}
|
||||
|
||||
// Execute command
|
||||
if (command) {
|
||||
try {
|
||||
// Split command by space
|
||||
const parts = command.split(' ');
|
||||
const baseCommand = parts[0];
|
||||
const args = parts.slice(1).join(' ');
|
||||
|
||||
// POST request to /terminal/execute/command
|
||||
|
||||
Reference in New Issue
Block a user