From 464824084255c07a8eff3bff2fedf1ddae289473 Mon Sep 17 00:00:00 2001 From: Dakedres Date: Mon, 25 Dec 2023 21:21:42 -0500 Subject: [PATCH] Some tweaks, directory viewing --- README.md | 1 + lib/platforms/neutralino.js | 56 ++++++++++++++++++++++++-- notes.md | 5 +++ package.json | 3 +- src/config.js | 3 +- src/qwe.js | 80 ++++++++++++++++++++++++++++--------- 6 files changed, 124 insertions(+), 24 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..27d250d --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Qwe \ No newline at end of file diff --git a/lib/platforms/neutralino.js b/lib/platforms/neutralino.js index 94dbff2..43a28a8 100644 --- a/lib/platforms/neutralino.js +++ b/lib/platforms/neutralino.js @@ -6,15 +6,21 @@ const handleNeutralinoError = err => { throw new Error(err.code + ': ' + err.message) } +const mapDirent = ({ entry, type }) => ({ + name: entry, + type +}) + window.Platform = { // // Paths // dirname(path) { - let index = path.lastIndexOf('/') + let normalPath = this.normalize(path) + let index = normalPath.lastIndexOf('/') - return index === -1 ? '' : path.slice(0, index) + return index === -1 ? '' : normalPath.slice(0, index) }, filename(path) { @@ -31,7 +37,13 @@ window.Platform = { }, join(...args) { - return args.join('/') + let parts = [] + + for(let arg of args) { + parts = parts.concat(arg.split('/')) + } + + return this.normalizePathFromParts(parts) }, // TODO: support non-posix @@ -39,6 +51,38 @@ window.Platform = { return path.charAt(0) === '/' }, + normalize(path) { + return this.normalizePathFromParts(path.split('/')) + }, + + normalizePathFromParts(parts) { + // let newPath = path !== '/' && path.endsWith('/') ? + // path.slice(0, -1) : + // path + + let out = [] + let skips = 0 + + for(let i = parts.length - 1; i >= 0; i--) { + let part = parts[i] + + if(part.length == 0 || part === '.') { + continue + } else if(part == '..') { + skips++ + continue + } else if(skips == 0) { + out.unshift(part) + } else { + skips-- + } + } + + console.log(out) + + return '/' + out.join('/') + }, + // // FS // @@ -66,6 +110,12 @@ window.Platform = { .catch(handleNeutralinoError) }, + readdir(path) { + return Neutralino.filesystem.readDirectory(path) + .catch(handleNeutralinoError) + .then(dirents => dirents.map(mapDirent)) + }, + // // OS // diff --git a/notes.md b/notes.md index 460d20a..a38c463 100644 --- a/notes.md +++ b/notes.md @@ -3,6 +3,11 @@ Needed to ```sudo apt install libsoup-2.4-dev libwebkit2gtk-4.0-dev libjavascriptcoregtk-4.0-dev``` +On void: +``` +sudo xbps-install webkit2gtk-devel +``` + --- # Functionality diff --git a/package.json b/package.json index 7cd014e..b6b0eb2 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "build-deps": "cd lib && rollup -c", "dev": "neu run", "build": "neu build", - "update": "neu update" + "update": "neu update", + "install:neu": "rm src/lib/platform.js && ln lib/platforms/neutralino.js src/lib/platforms.js && ln build/neutralino.js src/lib/neutralino.js" }, "type": "module" } diff --git a/src/config.js b/src/config.js index 673dadc..1414022 100644 --- a/src/config.js +++ b/src/config.js @@ -12,7 +12,8 @@ binds: { reload: 'Ctrl-Escape', editPath: 'Ctrl-r', save: 'Ctrl-s', - open: 'Ctrl-o' + open: 'Ctrl-o', + openDirectory: 'Ctrl-l' }, languages: [ diff --git a/src/qwe.js b/src/qwe.js index 2796d1c..57108b5 100644 --- a/src/qwe.js +++ b/src/qwe.js @@ -25,16 +25,10 @@ function Editor() { this.view.focus() } - // Opens a directory or a file - this.open = path => { - - } - this.openDocument = async (path) => { - this.doc = await Doc.open(path) - .catch(err => `Failed to open "${path}": ${err}`) - this.view.setState(await this.doc.createState()) - this.doc.setView(this.view) + await Doc.open(path) + .then(doc => this.setDoc(doc)) + .catch(err => console.error(`Failed to open "${path}": ${err}`)) } this.saveDocument = () => { @@ -51,6 +45,23 @@ function Editor() { statusbar.updateFilename() } + this.setDoc = async (doc, bind) => { + this.doc = doc + statusbar.updateFilename() + this.view.setState(await this.doc.createState()) + this.doc.setView(this.view) + } + + this.openDirectory = async (path) => { + let fileList = (await Platform.readdir(path)) + .filter(dirent => dirent.name != '.') + .map(dirent => dirent.type == 'FILE' ? dirent.name : dirent.name + '/') + .join('\n') + + let doc = new Doc(fileList, Platform.join(path, '~' + Platform.filename(path))) + editor.setDoc(doc) + } + // this.updateFilename = () => { // this.doc.path = Platform.dirname(this.doc.path) + '/' + statusbar.filename.value // } @@ -79,11 +90,11 @@ function Statusbar() { this.filename.value = path this.filename.select() - this.filename.selectionStart = path.length - Platform.filename(path).length + this.filename.selectionStart = path.length - getPathName(path).length } this.updateFilename = () => { - this.filename.value = Platform.filename(editor.doc.getPath()) + this.filename.value = getPathName(editor.doc.getPath()) } this.updateWorkspace = () => { @@ -109,6 +120,16 @@ function Statusbar() { } } + const getPathName = path => { + if(path.endsWith('/')) { + let index = path.lastIndexOf('/', path.length - 2) + 1 + + return path.slice(index) + } else { + return Platform.filename(path) + } + } + this.element = document.createElement('nav') this.filename = document.createElement('input') this.workspace = document.createElement('input') @@ -150,6 +171,7 @@ function Doc(content, initialPath) { cm.rectangularSelection(), cm.crosshairCursor(), cm.highlightSelectionMatches(), + cm.history(), Keymaps.default ] @@ -217,13 +239,9 @@ function Doc(content, initialPath) { } Doc.open = async (path) => { - console.log('Opening ' + path) - let content = await Platform.readText(path) .catch(console.error) - console.log(content) - return new Doc(content ?? '', path) } @@ -238,6 +256,7 @@ const Keymaps = { ...cm.searchKeymap, ...cm.foldKeymap, ...cm.lintKeymap, + ...cm.historyKeymap, // Alt completion keymap that merges with tab to indent { key: "Ctrl-Space", run: cm.startCompletion }, @@ -266,8 +285,7 @@ const Keymaps = { if(!mainSelection.empty) { let path = state.sliceDoc(mainSelection.from, mainSelection.to).trim() - - console.log(path) + let isDirectory = path.endsWith('/') editor.setDocPath( Platform.absolute(path) ? @@ -275,10 +293,18 @@ const Keymaps = { Platform.join(Platform.dirname(editor.doc.getPath()), path) ) - statusbar.updateFilename() + path = editor.doc.getPath() + + if(isDirectory) { + editor.openDirectory(path) + } else { + editor.openDocument(path) + } + + return } - editor.openDocument(editor.doc.getPath()) + editor.openDirectory(editor.doc.getPath()) } }, { @@ -287,6 +313,22 @@ const Keymaps = { editor.saveDocument() } }, + { + key: config.binds.openDirectory, + async run() { + let path = editor.doc.getPath() + + if(Platform.filename(path).startsWith('~')) + path = Platform.join(path, '../../') + else + path = Platform.dirname(path) + // path = path.endsWith('/') ? path.slice(0, -1) : path + + console.log(path) + + editor.openDirectory(path) + } + }, { key: config.binds.reload,