@kt3k/tku 1.0.8 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.js +31 -7
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -60,7 +60,9 @@ async function listTextFiles(repoPath, options = {}) {
|
|
|
60
60
|
const cwd = resolve(repoPath);
|
|
61
61
|
const files = await listFiles(repoPath, options);
|
|
62
62
|
const results = [];
|
|
63
|
-
for (
|
|
63
|
+
for (let i = 0; i < files.length; i++) {
|
|
64
|
+
const file = files[i];
|
|
65
|
+
options.onProgress?.(file, i + 1, files.length);
|
|
64
66
|
const fullPath = resolve(cwd, file);
|
|
65
67
|
try {
|
|
66
68
|
if (!await isBinary(fullPath)) {
|
|
@@ -76,12 +78,14 @@ async function listTextFiles(repoPath, options = {}) {
|
|
|
76
78
|
import { readFile } from "node:fs/promises";
|
|
77
79
|
import { resolve as resolve2 } from "node:path";
|
|
78
80
|
import { get_encoding } from "tiktoken";
|
|
79
|
-
async function tokenizeFiles(repoPath, files, encoding) {
|
|
81
|
+
async function tokenizeFiles(repoPath, files, encoding, options = {}) {
|
|
80
82
|
const enc = get_encoding(encoding);
|
|
81
83
|
try {
|
|
82
84
|
const results = [];
|
|
83
85
|
let totalTokens = 0;
|
|
84
|
-
for (
|
|
86
|
+
for (let i = 0; i < files.length; i++) {
|
|
87
|
+
const file = files[i];
|
|
88
|
+
options.onProgress?.(file, i + 1, files.length);
|
|
85
89
|
const fullPath = resolve2(repoPath, file);
|
|
86
90
|
const content = await readFile(fullPath, "utf-8");
|
|
87
91
|
const tokens = enc.encode_ordinary(content).length;
|
|
@@ -112,7 +116,7 @@ function formatTokenCount(n) {
|
|
|
112
116
|
}
|
|
113
117
|
return String(n);
|
|
114
118
|
}
|
|
115
|
-
function formatTable(result) {
|
|
119
|
+
function formatTable(result, omitted = 0) {
|
|
116
120
|
const lines = [];
|
|
117
121
|
const formatted = result.files.map((f) => ({
|
|
118
122
|
path: f.path,
|
|
@@ -123,6 +127,9 @@ function formatTable(result) {
|
|
|
123
127
|
for (const f of formatted) {
|
|
124
128
|
lines.push(`${f.display.padStart(maxWidth)} ${f.path}`);
|
|
125
129
|
}
|
|
130
|
+
if (omitted > 0) {
|
|
131
|
+
lines.push(`${"".padStart(maxWidth)} ... ${omitted} more files`);
|
|
132
|
+
}
|
|
126
133
|
lines.push(`${"\u2500".repeat(maxWidth)}\u2500\u2500`);
|
|
127
134
|
const totalDisplay = formatTokenCount(result.totalTokens);
|
|
128
135
|
lines.push(`${totalDisplay.padStart(maxWidth)} total (${result.totalFiles} files)`);
|
|
@@ -139,11 +146,12 @@ function formatResult(result, options = {}) {
|
|
|
139
146
|
sorted.sort((a, b) => b.tokens - a.tokens);
|
|
140
147
|
}
|
|
141
148
|
const filtered = top !== void 0 ? sorted.slice(0, top) : sorted;
|
|
149
|
+
const omitted = sorted.length - filtered.length;
|
|
142
150
|
const adjusted = {
|
|
143
151
|
...result,
|
|
144
152
|
files: filtered
|
|
145
153
|
};
|
|
146
|
-
return json ? JSON.stringify(adjusted, null, 2) : formatTable(adjusted);
|
|
154
|
+
return json ? JSON.stringify(adjusted, null, 2) : formatTable(adjusted, omitted);
|
|
147
155
|
}
|
|
148
156
|
|
|
149
157
|
// src/main.ts
|
|
@@ -203,12 +211,28 @@ async function main() {
|
|
|
203
211
|
const encoding = values.encoding;
|
|
204
212
|
const sort = values.sort;
|
|
205
213
|
const top = values.top !== void 0 ? Number(values.top) : void 0;
|
|
214
|
+
const isTTY = process.stderr.isTTY;
|
|
215
|
+
function status(msg) {
|
|
216
|
+
if (isTTY) {
|
|
217
|
+
process.stderr.write(`\r\x1B[K${msg}`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
function clearStatus() {
|
|
221
|
+
if (isTTY) {
|
|
222
|
+
process.stderr.write("\r\x1B[K");
|
|
223
|
+
}
|
|
224
|
+
}
|
|
206
225
|
try {
|
|
207
226
|
const files = await listTextFiles(repoPath, {
|
|
208
227
|
exclude: values.exclude,
|
|
209
|
-
noGitignore: !values.gitignore
|
|
228
|
+
noGitignore: !values.gitignore,
|
|
229
|
+
onProgress: (file, i, total) => status(`Scanning [${i}/${total}] ${file}`)
|
|
230
|
+
});
|
|
231
|
+
clearStatus();
|
|
232
|
+
const result = await tokenizeFiles(repoPath, files, encoding, {
|
|
233
|
+
onProgress: (file, i, total) => status(`Tokenizing [${i}/${total}] ${file}`)
|
|
210
234
|
});
|
|
211
|
-
|
|
235
|
+
clearStatus();
|
|
212
236
|
const output = formatResult(result, {
|
|
213
237
|
json: values.json,
|
|
214
238
|
top,
|