Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Size support #15

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/Process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ export class Process {
mode: entry.mode | mode,
owner: entry.owner,
group: entry.group,
size: 0, // TODO: size
size: isRegularFile(entry) ? entry.data.byteLength : 0,
// TODO: ctime, atime, mtime
};
}
Expand Down
36 changes: 36 additions & 0 deletions packages/core/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,42 @@ export function split(content: string, variables: Record<string, string> = {}) {
);
}

/**
* サイズを人間が読みやすい単位に変換します。
* @param size サイズ
* @param si 1024 の代わりに 1000 を使用するか
*/
export function convertSizeToHumanReadable(size: number, si: boolean = true) {
/**
* 単位 {@link unit} における {@link size} をまとめて表示します。
* @param size サイズ
* @param unit 単位
*/
function formatWithUnit(size: number, unit: string): string {
// NOTE: もし整数部分が1桁であれば、少数1桁までを丸めて表示する
if (Math.floor(size) < 10) {
return (Math.round(size * 10) / 10).toString() + unit;
} else {
return Math.round(size).toString() + unit;
}
}

const base = si ? 1000 : 1024;
if (size > base ** 5) {
return formatWithUnit(size / base ** 5, "P");
} else if (size > base ** 4) {
return formatWithUnit(size / base ** 4, "T");
} else if (size > base ** 3) {
return formatWithUnit(size / base ** 3, "G");
} else if (size > base ** 2) {
return formatWithUnit(size / base ** 2, "M");
} else if (size > base) {
return formatWithUnit(size / base, "K");
} else {
return formatWithUnit(size, "");
}
}

type oneHyphenOptionsList = (string | { id: string, needsArgument?: boolean })[];
type twoHyphensOptionsList = (string | { id: string, usesArgument?: boolean, needsArgument?: boolean })[];
type parseConfig = { stopInvalidOption: boolean; };
Expand Down
32 changes: 26 additions & 6 deletions packages/os/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Terminal } from "@xterm/xterm";
import { EmulatorInit } from "@/core/Emulator";
import { EISDIR, ELIBBAD, ENOENT, ENOTDIR } from "@/core/Error";
import { OpenFlag, StatMode, StdReadFlag } from "@/core/Flags";
import { basename, concatArrayBuffer, join, split, parseOptions } from "@/core/Utils";
import { basename, concatArrayBuffer, join, split, parseOptions, convertSizeToHumanReadable } from "@/core/Utils";
import { File } from "@/core/File";
import { Stat } from "@/core/Process";

Expand Down Expand Up @@ -615,9 +615,9 @@ There is NO WARRANTY, to the extent permitted by law.
// TODO: Add more options (https://github.com/kotonone/kotonemu/pull/7#issuecomment-2081303265)
let options = parseOptions(
args,
["-F", "-A", "-a", "-r", "-1", "-m", "-Q", "-U", "-X", "-e", "-q", "-N", "-S", "-v", "-t", "-p", "-R", "-C", "-l", "-o", "-G", "-g", "-n"],
["-F", "-A", "-a", "-r", "-1", "-m", "-Q", "-U", "-X", "-e", "-q", "-N", "-S", "-v", "-t", "-p", "-R", "-C", "-l", "-o", "-G", "-g", "-n", "-H"],
[
"--help", "--version", "--all", "--almost-all", "--classify", "--quote-name", "--reverse", "--escape", "--hide-control-chars", "--show-control-chars", "--literal", "--file-type", "--recursive", "--numeric-uid-gid", "--no-group",
"--help", "--version", "--all", "--almost-all", "--classify", "--quote-name", "--reverse", "--escape", "--hide-control-chars", "--show-control-chars", "--literal", "--file-type", "--recursive", "--numeric-uid-gid", "--no-group", "--human-readable",
{ "id": "--sort", "usesArgument": true, "needsArgument": true },
{ "id": "--quoting-style", "usesArgument": true, "needsArgument": true },
{ "id": "--indicator-style", "usesArgument": true, "needsArgument": true },
Expand Down Expand Up @@ -661,7 +661,7 @@ There is NO WARRANTY, to the extent permitted by law.
-r, --reverse 並び順を反転させる
-S ファイルのサイズで大きい順に並び替える
--sort=WORD 名前の代わりに WORD にしたがって並び替える:
none (-U), extension (-X)
none (-U), size (-S), extension (-X)
-t ファイルの更新時間で新しい順に並び替える
-U 要素を並び替えない
-v テキスト内の(バージョン)番号で自然に並び替える
Expand Down Expand Up @@ -703,6 +703,10 @@ There is NO WARRANTY, to the extent permitted by law.
options.index["-r"] = Math.max(options.index["--reverse"], options.index["-r"])
delete options.index["--reverse"];
}
if (options.index["--human-readable"] !== -1) {
options.index["-h"] = Math.max(options.index["--human-readable"], options.index["-h"])
delete options.index["--human-readable"];
}


sortingType: {
Expand Down Expand Up @@ -746,7 +750,7 @@ There is NO WARRANTY, to the extent permitted by law.
case "none":
case "extension":
case "time":
case "extensizesion":
case "size":
case "version":
if (options.index["--sort"] >= sortingType.index){
sortingType.type = options.optionsArguments["--sort"];
Expand Down Expand Up @@ -1029,7 +1033,7 @@ There is NO WARRANTY, to the extent permitted by law.
directories.push("./");
}
const sortFileList = (fileList : string[]) => {
// TODO: size, version, time
// TODO: version, time
if (sortingType.type !== "none") {
if (sortingType.type === "extension") {
fileList.sort((a, b) => {
Expand Down Expand Up @@ -1073,6 +1077,10 @@ There is NO WARRANTY, to the extent permitted by law.
}
}
});
} else if (sortingType.type === "size") {
fileList.sort((a, b) => {
return this.lstat(b).size - this.lstat(a).size;
});
} else {
fileList.sort();
}
Expand Down Expand Up @@ -1219,6 +1227,7 @@ There is NO WARRANTY, to the extent permitted by law.
fileDetails += `${(stats[fileName].mode & 0b100000000) ? "r" : "-"}${(stats[fileName].mode & 0b010000000) ? "w" : "-"}${(stats[fileName].mode & 0b001000000) ? "x" : "-"}${(stats[fileName].mode & 0b000100000) ? "r" : "-"}${(stats[fileName].mode & 0b000010000) ? "w" : "-"}${(stats[fileName].mode & 0b000001000) ? "x" : "-"}${(stats[fileName].mode & 0b000000100) ? "r" : "-"}${(stats[fileName].mode & 0b000000010) ? "w" : "-"}${(stats[fileName].mode & 0b000000001) ? "x" : "-"} ` ;

if (options.index["-o"] === -1 && options.index["-G"] === -1) {
// TODO: ユーザー名のパディングができていない
if (options.index["-n"] === -1) {
fileDetails += `${groups[stats[fileName].group]} `;
} else {
Expand All @@ -1227,12 +1236,23 @@ There is NO WARRANTY, to the extent permitted by law.
}

if (options.index["-g"] === -1) {
// TODO: ユーザー名のパディングができていない
if (options.index["-n"] === -1) {
fileDetails += `${groups[stats[fileName].owner]} `;
} else {
fileDetails += `${stats[fileName].owner} `;
}
}

// NOTE: ファイルサイズの追加
if (options.index["-h"] === -1) {
fileDetails += `${stats[fileName].size} `;
} else {
fileDetails += `${convertSizeToHumanReadable(stats[fileName].size)} `;
}

// TODO: fileDetails を廃止、配列に push していってあとで pad しながら join

fileData = fileDetails + fileData;
}

Expand Down