How to make a C # Windows Console application that respects previous console content?

Like Vim: when it starts, it transfers you to another "buffer". When vim closes, the user sees the previous command line content.

Does anyone know how to do this using C #?

thanks

Edit: the user will no longer see the output of the application. Hope this explains it better.

+6
source share
3 answers

I realized this by looking at the Vim source code the corresponding bits can be found in os_win32.c in the mch_init function I copied and pasted the corresponding bit here.

  /* Obtain handles for the standard Console I/O devices */ if (read_cmd_fd == 0) g_hConIn = GetStdHandle(STD_INPUT_HANDLE); else create_conin(); g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE); #ifdef FEAT_RESTORE_ORIG_SCREEN /* Save the initial console buffer for later restoration */ SaveConsoleBuffer(&g_cbOrig); g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes; #else /* Get current text attributes */ GetConsoleScreenBufferInfo(g_hConOut, &csbi); g_attrCurrent = g_attrDefault = csbi.wAttributes; #endif if (cterm_normal_fg_color == 0) cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1; if (cterm_normal_bg_color == 0) cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1; /* set termcap codes to current text attributes */ update_tcap(g_attrCurrent); GetConsoleCursorInfo(g_hConOut, &g_cci); GetConsoleMode(g_hConIn, &g_cmodein); GetConsoleMode(g_hConOut, &g_cmodeout); #ifdef FEAT_TITLE SaveConsoleTitleAndIcon(); /* * Set both the small and big icons of the console window to Vim icon. * Note that Vim presently only has one size of icon (32x32), but it * automatically gets scaled down to 16x16 when setting the small icon. */ if (g_fCanChangeIcon) SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon); #endif 

This way, it just saves the console information (including the title and icon), and then restores it again when you exit.

Unfortunately, the Console class does not provide access to the contents of the screen buffer, so for this you need P / Invoke in the corresponding Win32 functions.

As an alternative, the Win32 console actually supports several screen buffers , which can be an easier way to implement this - instead of copying an existing screen buffer, just create a new one with CreateConsoleScreenBuffer and set this buffer as the current one shown with SetConsoleActiveScreenBuffer . Again, the Console class, unfortunately, does not support multiple screen buffers, so for this you will need P / Invoke. Also, the Console class always writes to the screen buffer that was active at the time the application was launched, so if you replace the active screen buffer, the Console class will still write to the old screen buffer (which is not a longer view) - to get around this, you you will need to P / call all access to the console, see Working with console screen buffers in .NET .

+4
source

You can do this by writing the old content to the console buffer, as described here: How to write fast color output to the Console?

0
source

I believe that Vim records the history of open files and most likely also buffers them as a backup. The idea to do the same in C # is to implement a file buffer, some kind of storage that will record and track the things you need - input parameters, etc.

Another problem is that Vim (e.g. Vi) has command mode, which also needs to be implemented in C #. Now it depends on what exactly you want to accomplish using your C # program - that is the editor, and then fine, in any case, you need to distinguish between command and other modes.

-3
source

All Articles