Printing to console, no scroll

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Totte Karlsson

    Printing to console, no scroll

    Hi,
    How can I print to the console without having it scrolling to a new line for
    each print statement?
    I want to print a count down in the console, but for each count it scrolls
    the screen (of course).

    Is there another way?

    Here is the simple script for now

    print "Closing window in :"
    for second in range(10):
    time.sleep(1)
    print `10-second` +" seconds"

    thanks
    /totte




  • Harry George

    #2
    Re: Printing to console, no scroll

    "Totte Karlsson" <mtk@qm.com> writes:
    [color=blue]
    > Hi,
    > How can I print to the console without having it scrolling to a new line for
    > each print statement?
    > I want to print a count down in the console, but for each count it scrolls
    > the screen (of course).
    >
    > Is there another way?
    >
    > Here is the simple script for now
    >
    > print "Closing window in :"
    > for second in range(10):
    > time.sleep(1)
    > print `10-second` +" seconds"
    >
    > thanks
    > /totte
    >
    >
    >
    >[/color]

    You need to flush after the print statements. If you have several
    prints (not just 1 as in this example), it is easier to hide in a
    separate function.

    def msg(txt):
    sys.stdout.writ e(txt)
    sys.stdout.flus h()
    [color=blue]
    > for second in range(10):
    > time.sleep(1)
    > msg(`10-second` +" seconds ") #add a space at the end to delimit
    > print #to finish off the line[/color]


    --
    harry.g.george@ boeing.com
    6-6M31 Knowledge Management
    Phone: (425) 342-5601

    Comment

    • PiedmontBiz

      #3
      Re: Printing to console, no scroll

      >[color=blue]
      >
      >"Totte Karlsson" <mtk@qm.com> writes:
      >[color=green]
      >> Hi,
      >> How can I print to the console without having it scrolling to a new line[/color]
      >for[color=green]
      >> each print statement?
      >> I want to print a count down in the console, but for each count it scrolls
      >> the screen (of course).
      >>
      >> Is there another way?
      >>[/color][/color]


      =====
      Here is some code i got off the web.
      I use it on my os2 2.1 laptop for which I don't have curses or tkinker libs.
      I mostly use the color functions and move
      as a analog to turbo pascal/basic gotoxy.

      # begin code

      #!/usr/bin/env python
      '''
      ansi.py

      ANSI Terminal Interface

      (C)opyright 2000 Jason Petrone <jp@demonseed.n et>
      All Rights Reserved

      Color Usage:
      print RED + 'this is red' + RESET
      print BOLD + GREEN + WHITEBG + 'this is bold green on white' + RESET

      Commands:
      def move(new_x, new_y): 'Move cursor to new_x, new_y'
      def moveUp(lines): 'Move cursor up # of lines'
      def moveDown(lines) : 'Move cursor down # of lines'
      def moveForward(cha rs): 'Move cursor forward # of chars'
      def moveBack(chars) : 'Move cursor backward # of chars'
      def save(): 'Saves cursor position'
      def restore(): 'Restores cursor position'
      def clear(): 'Clears screen and homes cursor'
      def clrtoeol(): 'Clears screen to end of line'
      '''

      ############### ############### ##
      # C O L O R C O N S T A N T S #
      ############### ############### ##
      BLACK = '\033[30m'
      RED = '\033[31m'
      GREEN = '\033[32m'
      YELLOW = '\033[33m'
      BLUE = '\033[34m'
      MAGENTA = '\033[35m'
      CYAN = '\033[36m'
      WHITE = '\033[37m'

      RESET = '\033[0;0m'
      BOLD = '\033[1m'
      REVERSE = '\033[2m'

      BLACKBG = '\033[40m'
      REDBG = '\033[41m'
      GREENBG = '\033[42m'
      YELLOWBG = '\033[43m'
      BLUEBG = '\033[44m'
      MAGENTABG = '\033[45m'
      CYANBG = '\033[46m'
      WHITEBG = '\033[47m'

      def move(new_x, new_y):
      'Move cursor to new_x, new_y'
      print '\033[' + str(new_x) + ';' + str(new_y) + 'H'

      def moveUp(lines):
      'Move cursor up # of lines'
      print '\033[' + str(lines) + 'A'

      def moveDown(lines) :
      'Move cursor down # of lines'
      print '\033[' + str(lines) + 'B'

      def moveForward(cha rs):
      'Move cursor forward # of chars'
      print '\033[' + str(lines) + 'C'

      def moveBack(chars) :
      'Move cursor backward # of chars'
      print '\033[' + str(lines) + 'D'

      def save():
      'Saves cursor position'
      print '\033[s'

      def restore():
      'Restores cursor position'
      print '\033[u'

      def clear():
      'Clears screen and homes cursor'
      print '\033[2J'

      def clrtoeol():
      'Clears screen to end of line'
      print '\033[K'

      # ======end code=======



      allen



      Comment

      • PiedmontBiz

        #4
        Re: Printing to console, no scroll

        Sorry for my post with all the ansi.py code.

        I forgot the website I got it from.

        Allen

        Comment

        • mensanator

          #5
          Re: Printing to console, no scroll

          Harry George <harry.g.george @boeing.com> wrote in message news:<xqxr7y2sq 9l.fsf@cola2.ca .boeing.com>...[color=blue]
          > "Totte Karlsson" <mtk@qm.com> writes:
          >[color=green]
          > > Hi,
          > > How can I print to the console without having it scrolling to a new line for
          > > each print statement?
          > > I want to print a count down in the console, but for each count it scrolls
          > > the screen (of course).
          > >
          > > Is there another way?
          > >
          > > Here is the simple script for now
          > >
          > > print "Closing window in :"
          > > for second in range(10):
          > > time.sleep(1)
          > > print `10-second` +" seconds"
          > >
          > > thanks
          > > /totte
          > >
          > >
          > >
          > >[/color]
          >
          > You need to flush after the print statements. If you have several
          > prints (not just 1 as in this example), it is easier to hide in a
          > separate function.
          >
          > def msg(txt):
          > sys.stdout.writ e(txt)
          > sys.stdout.flus h()
          >[color=green]
          > > for second in range(10):
          > > time.sleep(1)
          > > msg(`10-second` +" seconds ") #add a space at the end to delimit
          > > print #to finish off the line[/color][/color]

          That produces

          10 seconds 9 seconds 8 seconds 7 seconds 6 seconds 5 seconds 4 seconds
          3 seconds 2 seconds 1 seconds



          This will produce a true countdown - the messages overwite each other:

          for sec in range(10):
          time.sleep(1)
          m = "%2d seconds" % (10-sec)
          msg(m + chr(13))

          But this will only work on systems that properly handle control
          characters. It works from a Windows command prompt but doesn't from
          Idle which shows that silly undefined character box (which I'll
          show here as []). So the Idle output looks like

          10 seconds[] 9 seconds[] 8 seconds[] 7 seconds[] 6 seconds[]
          5 seconds[] 4 seconds[] 3 seconds[] 2 seconds[] 1 seconds[]

          which, when copied and pasted into Google, results in

          10 seconds
          9 seconds
          8 seconds
          7 seconds
          6 seconds
          5 seconds
          4 seconds
          3 seconds
          2 seconds
          1 seconds

          This is the reason why the PROPER way to end a new line is
          with a carriage_return + line_feed and not simply line_feed.
          It also worked properly from the shell in Cygwin, but I
          don't have a real unix or linux system to try it on.

          Comment

          • Peter Hansen

            #6
            Re: Printing to console, no scroll

            PiedmontBiz wrote:[color=blue]
            >
            > Sorry for my post with all the ansi.py code.
            >
            > I forgot the website I got it from.[/color]

            Google is your friend (along with five seconds of effort):



            -Peter

            Comment

            • PiedmontBiz

              #7
              Re: Printing to console, no scroll

              >From: Peter Hansen peter@engcorp.c om[color=blue]
              >Date: 1/15/04 11:42 AM Eastern Standard Time
              >Message-id: <4006C2FB.B377B E82@engcorp.com >
              >
              >PiedmontBiz wrote:[color=green]
              >>
              >> Sorry for my post with all the ansi.py code.
              >>
              >> I forgot the website I got it from.[/color]
              >
              >Google is your friend (along with five seconds of effort):
              >
              > http://www.demonseed.net/~jp/code/ansi.py
              >
              >-Peter
              >
              >
              >
              >
              >
              >[/color]

              Appreciate the rebuke. Also, the url was saved in the html by IExplorer. I
              could have looked there.

              Allen

              Comment

              • Tim Roberts

                #8
                Re: Printing to console, no scroll

                "Totte Karlsson" <mtk@qm.com> wrote:[color=blue]
                >
                >How can I print to the console without having it scrolling to a new line for
                >each print statement?
                >I want to print a count down in the console, but for each count it scrolls
                >the screen (of course).
                >
                > Is there another way?
                >
                >Here is the simple script for now
                >
                >print "Closing window in :"
                >for second in range(10):
                > time.sleep(1)
                > print `10-second` +" seconds"[/color]

                print "Closing window in : "
                for second in range(10,0,-1):
                print "%2d\x08\x08\x0 8" % second,
                time.sleep(1)

                --
                - Tim Roberts, timr@probo.com
                Providenza & Boekelheide, Inc.

                Comment

                • Will Stuyvesant

                  #9
                  Re: Printing to console, no scroll

                  > I use it on my os2 2.1 laptop for which I don't have curses or tkinker libs.

                  Do you also know how to get it to work on a Windows XP laptop? I
                  tried cmd.exe and command.com, but they don't so ansi.

                  Comment

                  Working...