System.Console, Mono and NCurses

Hello everyone, in my work (and earlier in my studies) I often use C # and this language has become familiar to me, as they say. Having decided to practice some programming, I wrote a console text editor for Linux. I will briefly describe the experience gained.



In Mono, the System.Console class is not fully supported, but Mono is remarkable in that, just like in .NET, you can use P / Invoke to call methods of native C / C ++ Linux libraries.



In my case, it was a ncurses library. It looks like this:



public class Curses { /// <summary> ///    /// </summary> const string NCurses = "libncursesw.so.5"; /// <summary> ///     /// </summary> private IntPtr window; [DllImport(NCurses)] private extern static IntPtr initscr(); /// <summary> ///    /// </summary> public Curses() { window = initscr(); } [DllImport(NCurses)] private extern static int endwin(); /// <summary> ///   /// </summary> ~Curses() { int result = endwin(); } [DllImport(NCurses)] private extern static int mvwprintw(IntPtr window, int y, int x, string message); /// <summary> ///     /// </summary> public int Print(int x, int y, string message) { return mvwprintw(window, y, x, message); } [DllImport(NCurses)] private extern static int refresh(IntPtr window); /// <summary> ///    /// </summary> public int Refresh() { return refresh(window); } . . .
      
      





All ncurses methods in this project were connected as needed, in case the System.Console did not work properly, but if the standard method worked it made life much easier.



For example, the ncurses wgetch () method receives two bytes instead of one for Unicode characters, and for the Cyrillic layout I would have to use a more complex method with passing the pointer. Using the standard Console.ReadKey allowed you to leave all the code manageable and easily separate function keys from text characters.



There were no problems with reading and writing files, and everything worked properly as it did on .NET



image



When everything was working stably, an attempt was made to build this notebook on Windows. In order not to collect PDCurses from the sources, I took a ready-made dll from MinGW, namely libpdcursesw.dll and it did a good job as a replacement for ncurses.



I didn’t even need Mono to build a notebook, I just opened the MonoDevelop solution in Visual Studio without any adventures.



The only thing that required adjustment was the output to the console, I had to abandon mvwprintw and I rewrote Print, the output worked and the changes in the code were minimal:



 public void Print(int x, int y, string message) { Console.SetCursorPosition(x, y); Console.Write(message); }
      
      





I also had to adjust the size of the workspace, and methods for changing the color of the text.



All this convinced me that C # is quite suitable for Linux tasks and solutions, in which case it is easy to port to Windows.



The full implementation is available on GitHub , the master branch for Linux and windows for Windows.



All Articles