Skip to content

Commit

Permalink
Run file without saving
Browse files Browse the repository at this point in the history
  • Loading branch information
vshymanskyy committed Jun 10, 2024
1 parent 42746d5 commit 0112f2c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ A MicroPython IDE that works directly in the browser, leveraging modern web tech
- Viewer mode for `Markdown`
- `Hex` viewer for binary files
- Unicode support (`UTF8`)
- Run file without saving - *WIP*
- Run file without saving
- **File Manager**
- Add, remove files and directories
- Root FS stats display
Expand Down
48 changes: 31 additions & 17 deletions ViperIDE.html
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ <h1>ViperIDE</h1>

<script>

const VIPER_IDE_VERSION = "0.1.6"
const VIPER_IDE_VERSION = "0.1.7"

function addCSS(css) { document.head.appendChild(document.createElement("style")).innerHTML = css }

Expand Down Expand Up @@ -534,7 +534,7 @@ <h1>ViperIDE</h1>
}*/
}

async readExactly(n, timeout = 3000) {
async readExactly(n, timeout = 5000) {
const prevRecvCbk = this.receiveCallback
this.receiveCallback = (data) => this.receivedData += data;
const endTime = +Date.now() + timeout
Expand All @@ -553,12 +553,16 @@ <h1>ViperIDE</h1>
throw new Error('Timeout')
}

async readUntil(ending, timeout = 3000) {
async readUntil(ending, timeout = 5000, emit = false) {
const prevRecvCbk = this.receiveCallback
this.receiveCallback = (data) => this.receivedData += data;
this.receiveCallback = (data) => {
this.receivedData += data
if (emit) { prevRecvCbk(data) }
}
if (emit) { prevRecvCbk(this.receivedData) }
const endTime = +Date.now() + timeout
try {
while (+Date.now() < endTime) {
while (emit || (+Date.now() < endTime)) {
const idx = this.receivedData.indexOf(ending) + ending.length
if (idx >= ending.length) {
const res = this.receivedData.substring(0, idx)
Expand Down Expand Up @@ -990,29 +994,28 @@ <h1>ViperIDE</h1>
await port.write('\x02') // Ctrl-B: exit raw REPL
await port.readUntil('>\r\n')
const banner = await port.readUntil('>>> ')
term.clear()
//term.clear()
term.write(banner)
release()
}
return exitRawRepl
} catch (error) {
toastr.error("Cannot enter RAW mode", error)
term.clear()
release()
throw error
}
}

async function execCmd(cmd) {
async function execCmd(cmd, emit = false) {
await port.readUntil('>')
await port.write(cmd.trim())
await port.write('\x04') // Ctrl-D: execute
const status = await port.readExactly(2)
if (status != 'OK') {
throw new Error('Cannot exec command:' + status)
}
const res = (await port.readUntil('\x04')).slice(0, -1)
const err = (await port.readUntil('\x04')).slice(0, -1)
const res = (await port.readUntil('\x04', 5000, emit)).slice(0, -1)
const err = (await port.readUntil('\x04', 5000, emit)).slice(0, -1)

if (err.length) {
throw new Error('Cannot exec command: ' + err)
Expand All @@ -1021,10 +1024,10 @@ <h1>ViperIDE</h1>
return res
}

async function execRawRepl(cmd) {
async function execRawRepl(cmd, emit = false) {
const exitRaw = await enterRawRepl()
try {
return await execCmd(cmd)
return await execCmd(cmd, emit)
} catch (error) {
toastr.error("Execution failed", error)
} finally {
Expand All @@ -1034,7 +1037,10 @@ <h1>ViperIDE</h1>

async function execReplNoFollow(cmd) {
await port.write('\r\x03\x03')
await port.write(cmd.trim() + '\r\n')
//await port.flushInput()
//await port.write('\x05') // Ctrl-E: enter paste mode
await port.write(cmd + '\r\n')
//await port.write('\x04') // Ctrl-D: execute
}

async function fetchFileList() {
Expand Down Expand Up @@ -1364,12 +1370,13 @@ <h1>ViperIDE</h1>
async function runCurrentFile() {
if (!port) return;

toastr.info('This function is under construction')
if (editorFn.endsWith(".py")) {
execRawRepl(editor.getValue())
} else {
if (!editorFn.endsWith(".py")) {
toastr.error(`${editorFn} file is not executable`)
return
}

term.write('\r\n')
await execRawRepl(editor.getValue(), true)
}

/*
Expand Down Expand Up @@ -1650,6 +1657,13 @@ <h1>ViperIDE</h1>
}

(async () => {
/*window.addEventListener('error', (e) => {
toastr.error(e, "Error")
})
window.addEventListener('unhandledrejection', (e) => {
toastr.error(e.reason, "Unhandled Rejection")
})*/

setupTabs(document.getElementById('side-menu'))
setupTabs(document.getElementById('terminal-container'))

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "ViperIDE",
"short_name": "ViperIDE",
"description": "MicroPython Web IDE",
"version": "0.1.6",
"version": "0.1.7",
"start_url": "index.html",
"background_color": "#272822",
"theme_color": "#272822",
Expand Down

0 comments on commit 0112f2c

Please sign in to comment.