28 lines
1.4 KiB
JavaScript
28 lines
1.4 KiB
JavaScript
// 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);
|