-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #736 from dominic31/read-command
Read command
- Loading branch information
Showing
7 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
"use strict"; | ||
|
||
import * as node from "../node"; | ||
import {readFile} from 'fs'; | ||
import {exec} from 'child_process'; | ||
import {TextEditor} from '../../textEditor'; | ||
|
||
export interface IReadCommandArguments extends node.ICommandArgs { | ||
file?: string; | ||
cmd?: string; | ||
} | ||
|
||
// | ||
// Implements :read and :read! | ||
// http://vimdoc.sourceforge.net/htmldoc/insert.html#:read | ||
// http://vimdoc.sourceforge.net/htmldoc/insert.html#:read! | ||
// | ||
export class ReadCommand extends node.CommandBase { | ||
protected _arguments : IReadCommandArguments; | ||
|
||
constructor(args : IReadCommandArguments) { | ||
super(); | ||
this._name = 'read'; | ||
this._shortName = 'r'; | ||
this._arguments = args; | ||
} | ||
|
||
get arguments() : IReadCommandArguments { | ||
return this._arguments; | ||
} | ||
|
||
async execute() : Promise<void> { | ||
const textToInsert = await this.getTextToInsert(); | ||
if (textToInsert) { | ||
await TextEditor.insert(textToInsert); | ||
} | ||
} | ||
|
||
async getTextToInsert() : Promise<string> { | ||
if (this.arguments.file && this.arguments.file.length > 0) { | ||
return await this.getTextToInsertFromFile(); | ||
} else if (this.arguments.cmd && this.arguments.cmd.length > 0) { | ||
return await this.getTextToInsertFromCmd(); | ||
} else { | ||
throw Error('Invalid arguments'); | ||
} | ||
} | ||
|
||
async getTextToInsertFromFile() : Promise<string> { | ||
// TODO: Read encoding from ++opt argument. | ||
return new Promise<string>((resolve, reject) => { | ||
try { | ||
readFile(this.arguments.file as string, 'utf8', (err, data) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(data); | ||
} | ||
}); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
|
||
async getTextToInsertFromCmd() : Promise<string> { | ||
return new Promise<string>((resolve, reject) => { | ||
try { | ||
exec(this.arguments.cmd as string, (err, stdout, stderr) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(stdout); | ||
} | ||
}); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"use strict"; | ||
|
||
import {ReadCommand, IReadCommandArguments} from '../commands/read'; | ||
import {Scanner} from '../scanner'; | ||
|
||
export function parseReadCommandArgs(args : string) : ReadCommand { | ||
if (!args) { | ||
throw Error('Expected arguments.'); | ||
} | ||
|
||
var scannedArgs : IReadCommandArguments = {}; | ||
var scanner = new Scanner(args); | ||
|
||
scanner.skipWhiteSpace(); | ||
let c = scanner.next(); | ||
// read command has 2 forms - 'read <file-path>' and 'read! <shell-command>' | ||
if (c === '!') { | ||
scanner.ignore(); | ||
scanner.skipWhiteSpace(); | ||
scannedArgs.cmd = scanner.remaining(); | ||
if (!scannedArgs.cmd || scannedArgs.cmd.length === 0) { | ||
throw Error('Expected shell command.'); | ||
} | ||
} else { | ||
scannedArgs.file = scanner.remaining(); | ||
if (!scannedArgs.file || scannedArgs.file.length === 0) { | ||
throw Error('Expected file path.'); | ||
} | ||
} | ||
|
||
return new ReadCommand(scannedArgs); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"use strict"; | ||
|
||
import { ModeHandler } from '../../src/mode/modeHandler'; | ||
import { setupWorkspace, cleanUpWorkspace, assertEqualLines } from './../testUtils'; | ||
import { runCmdLine } from '../../src/cmd_line/main'; | ||
|
||
suite("read", () => { | ||
let modeHandler: ModeHandler; | ||
|
||
setup(async () => { | ||
await setupWorkspace(); | ||
modeHandler = new ModeHandler(); | ||
}); | ||
|
||
teardown(cleanUpWorkspace); | ||
|
||
test("Can read shell command output", async () => { | ||
await runCmdLine('r! echo hey\\\\n\\\\tho', modeHandler); | ||
assertEqualLines([ | ||
'hey', | ||
'\tho', | ||
'' | ||
]); | ||
}); | ||
}); |