goblinfactory / konsole

Home of the simple console library consisting of ProgressBar, Window, Form, Draw & MockConsole (C# console progress bar with support for single or multithreaded progress updates) Window is a 100%-ish console compatible window, supporting all normal console writing to a windowed section of the screen, supporting scrolling and clipping of console output.
721 stars 62 forks source link

how to update the value in the same line? #23

Closed wjkhappy14 closed 5 years ago

wjkhappy14 commented 5 years ago

update the value in the same line ,not progress bar !

eg: update the realtime push data (last price,bid price,the time)?

goblinfactory commented 5 years ago

these are the IConsole methods you probably would want to use;

void PrintAt(int x, int y, string format, params object[] args); void PrintAt(int x, int y, string text); void PrintAt(int x, int y, char c); void PrintAtColor(ConsoleColor foreground, int x, int y, string text, ConsoleColor? background);

example:

Using the internal (but public) FixLeft in string extensions in Konsole.Internal.StringExtensions see: https://github.com/goblinfactory/konsole/blob/master/Konsole/Internal/StringExtensions.cs do the following

(i've not tested this, so take this as a pseudo code, as a guide to the concept, and post any fixes you needed to do in the comments below to help anyone else. :)


var c = new Console();
int width = c.WindowWidth;

// do some stuff
c.WriteLine("did something");
// record stock price position
int stockprice1Y = c.CursorTop;
c.WriteLine("stock price will go here");
// do some more stuff 

c.WriteLine("something else, now cursor has moved");
c.WriteLine("stock price 2 will go here");
int stockprice2Y = c.CursorTop;

// do something and update stock 1
string sp1 = GetStockPriceFormattted("MSFT");
c.PrintAtColor(ConsoleColor.Red, 0,  stockprice1Y,  sp1.FixLeft(width)); 

// do something and update stock 2
string sp2 = GetStockPriceFormattted("AAPL");
c.PrintAtColor(ConsoleColor.Red, 0,  stockpriceY,  sp1.FixLeft(width)); 

Alternatively you could create a tiny window for each of only 1 line, then each time you WriteLine to that window it will automatically scroll the old line off. Warning this will not work on OSX since .NET console scroll has not been implemented by Microsoft on non windows platforms, even in dotnet standard. PrintAt using FixLeft will work cross platform.

e.g.


var c = new Console();

// do some stuff
c.WriteLine("did something");
// record stock price position

var w1 = new Window(c, 0,0, c.WindowWidth, 1); // 1 row high, current window location relative to cursor position of passed in console.
c.WriteLine("stock price will go here");
// do some more stuff 

c.WriteLine("something else, now cursor has moved");
c.WriteLine("stock price 2 will go here");
var w2 = new Window(c, 0,0, c.WindowWidth, 1); 

// do something and update stock 1
string sp1 = GetStockPriceFormattted("MSFT");
w1.WriteLine(ConsoleColor.Red, sp1); 

// do something and update stock 2
string sp2 = GetStockPriceFormattted("AAPL");
w2.WriteLine(ConsoleColor.Red,sp2); 

The advantage of the second approach using windows, is you can have a multiline window, say 3 lines, and then as long as you keep writeLine 'ing 3 new lines each time, you'll clear the window, and you won't have an restrictions of what information you want to display over any number of lines. Again I need to emphasise that the scrolling behavior does not work cross platform, so this will only work on a windows machine.

wjkhappy14 commented 5 years ago

thanks ! very very

goblinfactory commented 5 years ago

closing :)