使用Colorama向上移动光标在屏幕底部有问题

时间:2014-12-03 12:29:28

标签: python ansi colorama

我在Windows 7(64位)上使用Python(32位2.7.2)中的Colorama,它非常适合在控制台中着色文本,但是在获取它来移动光标时我遇到了问题

具体来说,如果我使用ANSI代码上一行,当光标远离屏幕底部时它会起作用,但当光标靠近底部时光标不能正确向上移动然后发送文本开始在页面下方打印,使其滚动。

我用来向前移动的代码是:

sys.stdout.write('\x1b[4A')

其中4将它向上移动四行(类似'\ x1b [8A'会将它向上移动八行)

我不确定这是否是我对ANSI代码如何工作或者是否是Colorama的问题缺乏了解。

要重新创建它,请在正常的Windows命令提示符(cmd.exe)或Console2中运行类似的内容(似乎没有任何区别)

from __future__ import print_function
import colorama
from colorama import Fore, Back, Style
import sys

def main():

    print('Blah')
    print('Blah')
    print('Blah')
    print('Blah')
    print('Blah')
    print('Blah')
    print('Blah')
    print('Blah')
    print('Blah')
    print('Blah')
    print('Blah')
    sys.stdout.write('\x1b[6A')
    sys.stdout.write('some text')

if __name__ == '__main__':
    main()

如果您在屏幕顶部附近运行上面的代码,最终会在“ Blah ”输出中写入“某些文字”,但是如果你已经在屏幕底部附近启动它,那么“一些文字”就会在最后,光标似乎根本没有向后滚动。

我特别需要将光标向上移动,因此它相对于其他输出放置,而不是给它一个绝对的屏幕位置(即将其移动到位置x,y)

有关从哪里开始的任何建议?

2 个答案:

答案 0 :(得分:0)

您应该从colorama导入“init”并在尝试向上移动光标之前调用“init()”。

答案 1 :(得分:0)

需要添加

import React from 'react'; import {render} from 'react-dom'; import {Provider, connect} from 'react-redux'; import {createStore, compose, combineReducers} from 'redux'; import TransitionGroup from 'react/lib/ReactTransitionGroup'; // Box component class Box extends React.Component { componentWillEnter(callback) { console.log('componentWillEnter'); callback(); } componentWillLeave(callback) { console.log('componentWillLeave'); callback(); } render() { return <div className="box"/>; } } // Boxe connected component const BoxContainer = connect(null, {})(Box); // App component class App extends React.Component { state = { shouldShowBox: true }; toggleBox = () => { this.setState({ shouldShowBox: !this.state.shouldShowBox }); }; render() { return ( <div> <TransitionGroup> {this.state.shouldShowBox && <BoxContainer/>} </TransitionGroup> <button className="toggle-btn" onClick={this.toggleBox}> toggle </button> </div> ); } } // reducer function reducer(state = [], action) { switch (action.type) { default: return state } } // store const store = createStore(reducer); // render render( <Provider store={store}> <App /> </Provider> , document.getElementById('root') ); 。它在Windows中很重要,但在* nix系统中是可选的。

  

在Windows上,调用init()将从发送到stdout或stderr的任何文本中过滤ANSI转义序列,并用等效的Win32调用替换它们。在其他平台上,调用init()无效。

要在程序退出前停止使用colorama,只需致电colorama.init()即可。这会将stdout和stderr恢复为原始值,以便禁用Colorama。要重新使用Colorama,请致电deinit();再次调用reinit()会更便宜(但做同样的事情)。

init()接受一些** kwargs来覆盖其默认行为。

默认为

colorama.init()

colorama.init(convert = True, wrap = True, autoreset=False, strip = None) 将在每个语句后重置颜色/换行/条带更改。

init(autoreset=True)#将每种语句的颜色重置为默认值

colorama.init(autoreset=True)#的工作方式与colorama.init()

相同

colorama.init(convert = True)#当我们想要停止换色时使用

colorama.init(convert = False)#的工作方式与colorama.init()

相同

colorama.init(wrap = True)#当我们想要停止换色时使用

相关问题