我试图在nodejs中创建一个程序,它可以等待用户输入,同时在屏幕上打印其他东西而不会弄乱一切。
使用以下代码:
setInterval( function (){
console.log("just displaying some stuff");
},1000);
process.stdin.on("data", function(char) {
console.log(char);
});
我得到以下结果:
just displaying some stuff
just displaying some stuff
'this is' just displaying some stuff
'in'just displaying some stuff
'put'just displaying some stuff
我正在寻找的结果是:
just displaying some stuff
just displaying some stuff
just displaying some stuff
just displaying some stuff
just displaying some stuff
'this is input'
有什么想法吗?我找不到任何有用的东西。
答案 0 :(得分:2)
像rsp所说,祝福是一个很好的候选工具。这是您使用的示例实现。
我已经使用祝福来在屏幕底部创建一个文本框。屏幕的其余部分是' body'您的代码输出将被添加到哪里。这样,当您输入输入时,您的程序就不会像您所经历的那样混淆输出。
要尝试此操作,请运行Unable to generate the model because of the following exception: 'System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Too many parameters were provided in this RPC request. The maximum is 2100.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext`1 c)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
--- End of inner exception stack trace ---
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.Execute(EntityCommand entityCommand, CommandBehavior behavior)
at System.Data.Entity.Core.EntityClient.EntityCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.Data.Entity.Design.VersioningFacade.ReverseEngineerDb.SchemaDiscovery.FunctionDetailsReader..ctor(EntityCommand command, Version storeSchemaModelVersion)
at Microsoft.Data.Entity.Design.VersioningFacade.ReverseEngineerDb.SchemaDiscovery.EntityStoreSchemaGeneratorDatabaseSchemaLoader.LoadFunctionDetails(IEnumerable`1 filters)
at Microsoft.Data.Entity.Design.VersioningFacade.ReverseEngineerDb.SchemaDiscovery.EntityStoreSchemaGeneratorDatabaseSchemaLoader.LoadStoreSchemaDetails(IList`1 filters)
at Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.Engine.ModelGenerator.GetStoreSchemaDetails(StoreSchemaConnectionFactory connectionFactory)
at Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.Engine.ModelGenerator.CreateStoreModel()
at Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.Engine.ModelGenerator.GenerateModel(List`1 errors)
at Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.Engine.ModelBuilderEngine.GenerateModels(String storeModelNamespace, ModelBuilderSettings settings, List`1 errors)
at Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.Engine.ModelBuilderEngine.GenerateModel(ModelBuilderSettings settings, IVsUtils vsUtils, ModelBuilderEngineHostContext hostContext)'.
Loading metadata from the database took 00:00:00.4485064.
Generating the model took 00:00:25.4756427.
,然后在节点中运行此代码:
npm install blessed --save
这里的要点是:
const blessed = require('blessed');
var screen = blessed.screen();
var body = blessed.box({
top: 0,
left: 0,
height: '100%-1',
width: '100%',
keys: true,
mouse: true,
alwaysScroll: true,
scrollable: true,
scrollbar: {
ch: ' ',
bg: 'red'
}
});
var inputBar = blessed.textbox({
bottom: 0,
left: 0,
height: 1,
width: '100%',
keys: true,
mouse: true,
inputOnFocus: true,
style: {
fg: 'white',
bg: 'blue' // Blue background so you see this is different from body
}
});
// Add body to blessed screen
screen.append(body);
screen.append(inputBar);
// Close the example on Escape, Q, or Ctrl+C
screen.key(['escape', 'q', 'C-c'], (ch, key) => (process.exit(0)));
// Handle submitting data
inputBar.on('submit', (text) => {
log(text);
inputBar.clearValue();
});
// Add text to body (replacement for console.log)
function log(text) {
body.pushLine(text);
screen.render();
}
/*
* Demonstration purposes
*/
// Listen for enter key and focus input then
screen.key('enter', (ch, key) => {
inputBar.focus();
});
// Log example output
setInterval(() => {
log("just displaying some stuff");
}, 1000);
函数显示输出。将此视为log
替代品。不再使用console.log
功能,他们会搞乱有福的屏幕。console.*
元素,以便接收输入。但是,你可以让它始终专注,但是我的演示显示了按住回车键或点击蓝色条来聚焦它。inputBar
以呈现更改。答案 1 :(得分:0)
你可以用ncurses或其他一些基于文本的UI库做类似的事情。在Node中,有一个名为blessed
的优秀模块可以帮助您完成类似的事情。参见:
它有一些很好的演示,有更复杂的用例:
在您的情况下,您可以创建一个窗口,将整个屏幕减去一行并在那里打印文本,同时在下面的第二个窗口中获取输入,如果需要,可以是一行或更高。
这比简单console.log()
更复杂,但肯定是可能的,不要听那些告诉你不是的人。