mirror of
https://github.com/45Drives/cockpit-navigator.git
synced 2025-07-30 17:15:16 +02:00
define constants in central file
This commit is contained in:
parent
d3d16bf903
commit
a0c179380d
@ -38,6 +38,7 @@ import { notificationsInjectionKey, settingsInjectionKey } from '../keys';
|
||||
import DirectoryEntry from './DirectoryEntry.vue';
|
||||
import getDirListing from '../functions/getDirListing';
|
||||
import getDirEntryObjects from '../functions/getDirEntryObjects';
|
||||
import { RECORD_SEPARATOR, UNIT_SEPARATOR } from '../constants';
|
||||
|
||||
export default {
|
||||
name: 'DirectoryEntryList',
|
||||
@ -155,18 +156,16 @@ export default {
|
||||
}
|
||||
selection.lastSelectedInd = null;
|
||||
processingHandler.start();
|
||||
const US = '\x1F';
|
||||
const RS = '\x1E';
|
||||
const processLinks = (linkTargets) => {
|
||||
if (linkTargets.length === 0)
|
||||
return null;
|
||||
const callback = state => state.stdout
|
||||
.trim()
|
||||
.split('\n')
|
||||
.split(RECORD_SEPARATOR)
|
||||
.filter(record => record)
|
||||
.map((record, index) => {
|
||||
if (record.includes(US)) {
|
||||
const [type, mode] = record.split(US);
|
||||
if (record.includes(UNIT_SEPARATOR)) {
|
||||
const [type, mode] = record.split(UNIT_SEPARATOR);
|
||||
linkTargets[index].type = type;
|
||||
linkTargets[index].mode = mode;
|
||||
linkTargets[index].broken = false;
|
||||
@ -175,7 +174,7 @@ export default {
|
||||
}
|
||||
});
|
||||
return new Promise((resolve, reject) =>
|
||||
useSpawn(['stat', `--printf=%F${US}%f\n`, ...linkTargets.map(target => target.path)], { superuser: 'try', err: 'out' }).promise()
|
||||
useSpawn(['stat', `--printf=%F${UNIT_SEPARATOR}%f${RECORD_SEPARATOR}`, ...linkTargets.map(target => target.path)], { superuser: 'try', err: 'out' }).promise()
|
||||
.then(callback)
|
||||
.catch(callback)
|
||||
.finally(resolve)
|
||||
|
13
navigator-vue/src/constants.js
Normal file
13
navigator-vue/src/constants.js
Normal file
@ -0,0 +1,13 @@
|
||||
/**
|
||||
* ASCII field delimiter
|
||||
*/
|
||||
const UNIT_SEPARATOR = '\x1F';
|
||||
/**
|
||||
* ASCII record delimiter
|
||||
*/
|
||||
const RECORD_SEPARATOR = '\x1E';
|
||||
|
||||
export {
|
||||
UNIT_SEPARATOR,
|
||||
RECORD_SEPARATOR,
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
import { useSpawn, errorString } from "@45drives/cockpit-helpers";
|
||||
import { UNIT_SEPARATOR, RECORD_SEPARATOR } from "../constants";
|
||||
|
||||
/**
|
||||
* Callback for handling errors during parsing of `dir` output lines
|
||||
@ -47,8 +48,6 @@ import { useSpawn, errorString } from "@45drives/cockpit-helpers";
|
||||
* @returns {Promise<DirectoryEntry[]>} Array of DirectoryEntry objects
|
||||
*/
|
||||
export default async function getDirEntryObjects(dirListing, cwd, failCallback, byteFormatter = cockpit.format_bytes) {
|
||||
const US = '\x1F';
|
||||
const RS = '\x1E';
|
||||
const fields = [
|
||||
'%n', // path
|
||||
'%f', // mode (raw hex)
|
||||
@ -67,7 +66,7 @@ export default async function getDirEntryObjects(dirListing, cwd, failCallback,
|
||||
(
|
||||
await useSpawn([
|
||||
'stat',
|
||||
`--printf=${fields.join(US)}${RS}`,
|
||||
`--printf=${fields.join(UNIT_SEPARATOR)}${RECORD_SEPARATOR}`,
|
||||
...dirListing
|
||||
], { superuser: 'try', directory: cwd }
|
||||
)
|
||||
@ -87,13 +86,13 @@ export default async function getDirEntryObjects(dirListing, cwd, failCallback,
|
||||
* @returns {DirectoryEntry[]}
|
||||
*/
|
||||
function parseRawEntryStats(raw, cwd, failCallback, byteFormatter = cockpit.format_bytes) {
|
||||
const US = '\x1F';
|
||||
const RS = '\x1E';
|
||||
return raw.split(RS)
|
||||
const UNIT_SEPARATOR = '\x1F'; // "Unit Separator" - ASCII field delimiter
|
||||
const RECORD_SEPARATOR = '\x1E'; // "Record Separator" - ASCII record delimiter
|
||||
return raw.split(RECORD_SEPARATOR)
|
||||
.filter(record => record) // remove empty lines
|
||||
.map(record => {
|
||||
try {
|
||||
let [name, mode, modeStr, size, owner, group, ctime, mtime, atime, type, symlinkStr] = record.split(US);
|
||||
let [name, mode, modeStr, size, owner, group, ctime, mtime, atime, type, symlinkStr] = record.split(UNIT_SEPARATOR);
|
||||
[size, ctime, mtime, atime] = [size, ctime, mtime, atime].map(num => parseInt(num));
|
||||
[ctime, mtime, atime] = [ctime, mtime, atime].map(ts => ts ? new Date(ts * 1000) : null);
|
||||
mode = parseInt(mode, 16);
|
||||
|
Loading…
x
Reference in New Issue
Block a user