feat: workspace helpers - quoting paths, what a dropped file does, with node tests
This commit is contained in:
1 parent
0ada0899cf
commit
7b9daa524e
4 files changed
+90
No files matched your search
@@ -0,0 +1,27 @@
|
||||
// Pure helpers of the workspace: quoting paths for a shell, what a dropped
|
||||
// file does where it lands, sizes. No DOM here, so node can test them.
|
||||
(function (root) {
|
||||
'use strict';
|
||||
function shellQuote(p) { return "'" + String(p).replace(/'/g, "'\\''") + "'"; }
|
||||
function shellQuoteAll(ps) { return ps.map(shellQuote).join(' '); }
|
||||
const same = (a, b) => a.kind === b.kind && a.id === b.id;
|
||||
// dropAction says what dropping item on target does: a transfer between
|
||||
// file panes, paths pasted into a terminal of the same place, nothing on
|
||||
// its own folder, or a refusal with the reason.
|
||||
function dropAction(item, target) {
|
||||
if (target.kind === 'term') {
|
||||
return same(item.source, target.source) ? {kind: 'paste'}
|
||||
: {kind: 'deny', reason: 'Нельзя: файл на ' + item.source.label + ', а терминал на ' + target.source.label};
|
||||
}
|
||||
if (same(item.source, target.source) && target.dir === item.dir) return {kind: 'none'};
|
||||
return {kind: 'transfer'};
|
||||
}
|
||||
function fmtSize(n) {
|
||||
if (n < 1024) return n + ' B';
|
||||
if (n < 1048576) return (n / 1024).toFixed(1) + ' K';
|
||||
if (n < 1073741824) return (n / 1048576).toFixed(1) + ' M';
|
||||
return (n / 1073741824).toFixed(1) + ' G';
|
||||
}
|
||||
const api = {shellQuote, shellQuoteAll, dropAction, fmtSize};
|
||||
if (typeof module !== 'undefined') module.exports = api; else root.WSUtil = api;
|
||||
})(this);
|
||||
Reference in new issue
Block a user