Skip to content

Commit 0f690d5

Browse files
committed
Bug fixes and improvements for the show command with different text files
1 parent 713d7dc commit 0f690d5

File tree

7 files changed

+56
-13
lines changed

7 files changed

+56
-13
lines changed

extraterm/src/mimetype_detector/MimeTypeDetector.jest.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ describe.each([
2525
// Random binary data.
2626
["Random", "random.bin", "application/octet-stream"],
2727
// Part of a linux lib.so file.
28-
["LibSo", "libso.bin", "application/octet-stream"]
28+
["LibSo", "libso.bin", "application/octet-stream"],
29+
["Shell Script", "foo.sh", "text/plain"],
30+
["Fish shell script", "foo.fish", "text/plain"],
31+
["ini file", "foo.ini", "text/plain"],
2932
])("", (name: string, filename: string, mimeType: string) => {
3033
test(`${name} => ${mimeType}`, () => {
3134
const result = MimeTypeDetector.detect(null, readTestFile(filename));

extraterm/src/mimetype_detector/MimeTypeDetector.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 Simon Edwards <[email protected]>
2+
* Copyright 2016-2019 Simon Edwards <[email protected]>
33
*
44
* This source code is licensed under the MIT license which is detailed in the LICENSE.txt file.
55
*/
@@ -170,6 +170,7 @@ const modeInfo: ModeInfo[] = [
170170
{name: "Rust", mime: "text/x-rustsrc", mode: "rust", ext: ["rs"]},
171171
{name: "SAS", mime: "text/x-sas", mode: "sas", ext: ["sas"]},
172172
{name: "Sass", mime: "text/x-sass", mode: "sass", ext: ["sass"]},
173+
{name: "scad", mime: "application/x-scad", mode: "text", ext: ["scad"]},
173174
{name: "Scala", mime: "text/x-scala", mode: "clike", ext: ["scala"]},
174175
{name: "Scheme", mime: "text/x-scheme", mode: "scheme", ext: ["scm", "ss"]},
175176
{name: "SCSS", mime: "text/x-scss", mode: "css", ext: ["scss"]},

extraterm/src/render_process/DownloadApplicationModeHandler.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ enum DownloadHandlerState {
5454

5555
const MAX_CHUNK_BYTES = 3 * 1024;
5656
const invalidBodyRegex = /[^\n\rA-Za-z0-9/+:=]/;
57-
57+
const ONE_KILOBYTE = 1024;
5858

5959
class DownloadSession {
6060

@@ -69,6 +69,7 @@ class DownloadSession {
6969

7070
onCreatedBulkFile: Event<BulkFileHandle>;
7171
private _onCreatedBulkFileEventEmitter = new EventEmitter<BulkFileHandle>();
72+
private _createdEventFired = false;
7273

7374
constructor(private _emulator: TermApi.EmulatorApi, private _broker: BulkFileBroker) {
7475
this._log = getLogger("DownloadApplicationModeHandler DownloadSession", this);
@@ -130,14 +131,24 @@ class DownloadSession {
130131
this._fileHandle = this._broker.createWriteableBulkFileHandle(metadata, filesize);
131132
this._fileHandle.ref();
132133
this._availableWriteBufferSizeChangedDisposable = this._fileHandle.onAvailableWriteBufferSizeChange(this._handleAvailableWriteBufferSizeChange.bind(this));
133-
this._onCreatedBulkFileEventEmitter.fire(this._fileHandle);
134134

135135
this._state = DownloadHandlerState.BODY;
136136
return this._handleBody("");
137137
}
138138
return {action: TermApi.ApplicationModeResponseAction.CONTINUE};
139139
}
140140

141+
private _fireOnCreatedEvent(force=false): void {
142+
if (this._fileHandle == null || this._createdEventFired) {
143+
return;
144+
}
145+
146+
if (force || this._fileHandle.getAvailableSize() >= ONE_KILOBYTE) {
147+
this._onCreatedBulkFileEventEmitter.fire(this._fileHandle);
148+
this._createdEventFired = true;
149+
}
150+
}
151+
141152
private _closeFileHandle(success: boolean): void {
142153
if (this._fileHandle !== null) {
143154
this._fileHandle.close(success);
@@ -160,6 +171,7 @@ class DownloadSession {
160171
if (invalidBodyRegex.test(chunk)) {
161172
this._log.warn("Chunk contains illegal characters. Aborting.");
162173
this._state = DownloadHandlerState.ERROR;
174+
this._fireOnCreatedEvent(true);
163175
this._closeFileHandle(false);
164176
return {action: TermApi.ApplicationModeResponseAction.ABORT, remainingData: this._encodedDataBuffer};
165177
}
@@ -177,6 +189,7 @@ class DownloadSession {
177189
if (chunk.charAt(1) !== ":" || chunk.charAt(lastColonIndex) !== ":" || (commandChar !== "D" && commandChar !== "E")) {
178190
this._log.warn("Data chunk is malformed. Aborting.");
179191
this._state = DownloadHandlerState.ERROR;
192+
this._fireOnCreatedEvent(true);
180193
this._closeFileHandle(false);
181194
return {action: TermApi.ApplicationModeResponseAction.ABORT, remainingData: this._encodedDataBuffer};
182195
}
@@ -196,6 +209,7 @@ class DownloadSession {
196209
if (this._previousHash.toString("hex") !== hashHex) {
197210
this._log.warn("Data chunk hash is incorrect.");
198211
this._state = DownloadHandlerState.ERROR;
212+
this._fireOnCreatedEvent(true);
199213
this._closeFileHandle(false);
200214
return {action: TermApi.ApplicationModeResponseAction.ABORT, remainingData: this._encodedDataBuffer};
201215
}
@@ -211,6 +225,8 @@ class DownloadSession {
211225

212226
this._flushBuffer();
213227

228+
this._fireOnCreatedEvent();
229+
214230
if (this._state === DownloadHandlerState.BODY && this._fileHandle.getAvailableWriteBufferSize() <= 0) {
215231
return {action: TermApi.ApplicationModeResponseAction.PAUSE};
216232
}
@@ -272,6 +288,7 @@ class DownloadSession {
272288
}
273289

274290
if (this._decodedDataBuffers.length === 0 && this._state === DownloadHandlerState.COMPLETE) {
291+
this._fireOnCreatedEvent(true);
275292
this._closeFileHandle(true);
276293
}
277294
}
@@ -280,6 +297,7 @@ class DownloadSession {
280297
let response: TermApi.ApplicationModeResponse = null;
281298

282299
this._flushBuffer();
300+
this._fireOnCreatedEvent(true);
283301

284302
if (this._state !== DownloadHandlerState.COMPLETE) {
285303
this._state = DownloadHandlerState.ERROR;

extraterm/src/render_process/Terminal.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,14 +1284,8 @@ export class EtTerminal extends ThemeableElementBase implements AcceptsKeybindin
12841284
private _handleShowFile(bulkFileHandle: BulkFileHandle): void {
12851285
const isDownload = bulkFileHandle.getMetadata()["download"] === "true";
12861286
const {mimeType, charset} = BulkFileUtils.guessMimetype(bulkFileHandle);
1287-
if (mimeType == null || isDownload) {
1288-
this._appendMimeViewer("application/octet-stream", bulkFileHandle);
1289-
} else {
1290-
const newViewer = this._appendMimeViewer(mimeType, bulkFileHandle);
1291-
if (newViewer == null) {
1292-
this._appendMimeViewer("application/octet-stream", bulkFileHandle);
1293-
}
1294-
}
1287+
this._appendMimeViewer(mimeType == null || isDownload ? "application/octet-stream" : mimeType,
1288+
bulkFileHandle);
12951289
}
12961290

12971291
private _appendMimeViewer(mimeType: string, bulkFileHandle: BulkFileHandle): ViewerElement {
@@ -1318,7 +1312,7 @@ export class EtTerminal extends ThemeableElementBase implements AcceptsKeybindin
13181312

13191313
private _createMimeViewer(mimeType: string, bulkFileHandle: BulkFileHandle): ViewerElement {
13201314
let tag: string = null;
1321-
const candidates = viewerClasses.filter( (viewerClass) => viewerClass.supportsMimeType(mimeType) );
1315+
const candidates = this._findViewersForMimeType(mimeType);
13221316
if (candidates.length !== 0) {
13231317
tag = candidates[0].TAG_NAME;
13241318
} else {
@@ -1340,6 +1334,27 @@ export class EtTerminal extends ThemeableElementBase implements AcceptsKeybindin
13401334
return dataViewer;
13411335
}
13421336

1337+
private _findViewersForMimeType(mimeType: string): SupportsMimeTypes[] {
1338+
const candidates = this._findViewersForMimeTypeStrict(mimeType);
1339+
if (candidates.length !== 0) {
1340+
return candidates;
1341+
}
1342+
1343+
if (mimeType.startsWith("text/")) {
1344+
const textCandidates = this._findViewersForMimeTypeStrict("text/plain");
1345+
if (textCandidates.length !== 0) {
1346+
return textCandidates;
1347+
}
1348+
}
1349+
1350+
return this._findViewersForMimeTypeStrict("application/octet-stream");
1351+
}
1352+
1353+
private _findViewersForMimeTypeStrict(mimeType: string): SupportsMimeTypes[] {
1354+
return viewerClasses.filter( viewerClass => viewerClass.supportsMimeType(mimeType) );
1355+
}
1356+
1357+
13431358
private _getNextTag(): string {
13441359
let tag = this._nextTag;
13451360
if (tag === null) {

extraterm/src/testfiles/foo.fish

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/fish
2+
echo "A fish script"

extraterm/src/testfiles/foo.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[Section]
2+
key = value

extraterm/src/testfiles/foo.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
echo "A shell script"

0 commit comments

Comments
 (0)