[Prev][Next][Index][Thread]

Re: clearing the console



"Matthew Luckie" <kluckie@ihug.co.nz> writes:

> I am using functional developer in windows

Here is a way of doing it using Windows API calls:

------------------8<----------------------
define C-struct <COORD>
  slot x-value :: <USHORT>;
  slot y-value :: <USHORT>;
  pointer-type-name: <COORD*>;
end C-struct <COORD>;

define C-function WriteConsoleOutputCharacter
  parameter hConsoleOutput :: <HANDLE>;
  parameter lpCharacter :: <LPCSTR>;
  parameter nLength :: <DWORD>;
  parameter dwWriteCoord :: <COORD>;
  parameter lpNumberOfCharsWritten :: <LPDWORD>;
  result value :: <BOOL>;
  c-name: "WriteConsoleOutputCharacterA", c-modifiers: "__stdcall";
end;

define C-function SetConsoleCursorPosition
  parameter hConsoleOutput :: <HANDLE>;
  parameter dwCursorPosition :: <COORD>;
  result value :: <BOOL>;
  c-name: "SetConsoleCursorPosition", c-modifiers: "__stdcall";
end;

define constant $console-size = 80 * 40;

define method clear-console-screen()
  let std-out = GetStdHandle($STD-OUTPUT-HANDLE);
  unless(std-out = $INVALID-HANDLE-VALUE)
    with-stack-structure(text :: <c-string>, element-count: $console-size)
      clear-memory!(text, $console-size);

      with-stack-structure( coord :: <COORD*> )
        coord.x-value := 0;
        coord.y-value := 0;
        with-stack-structure(result-size :: <LPDWORD>)
          WriteConsoleOutputCharacter(std-out,
                                      text,
                                      $console-size,
                                      coord,
                                      result-size)
        end with-stack-structure;
        SetConsoleCursorPosition(std-out, coord);
      end with-stack-structure;
    end with-stack-structure; 
  end unless;
end method clear-console-screen;

// Quick test...
define method main () => ()
  map(curry(format-out, "Hello, World!\n"), range(from: 0, below: 20));
  clear-console-screen();
  format-out("Done!\n");
end method main;

begin
  main();
end;
------------------8<----------------------

You'll need these libraries and the equivalent modules:

  use c-ffi;
  use win32-common;
  use win32-kernel;

There may be a better way of doing it rather than output lots of
spaces to the screen but I couldn't find one off hand.

Chris.
-- 
http://www.double.co.nz/dylan





Follow-Ups: References: