Coder Social home page Coder Social logo

pavkam / sharpie Goto Github PK

View Code? Open in Web Editor NEW
23.0 3.0 0.0 18.84 MB

Terminal manipulation library based on Curses (ncurses, pdcurses and pdcurses-mod) for modern .NET applications.

License: BSD 3-Clause "New" or "Revised" License

C# 100.00%
csharp curses curses-library curses-ui dotnet-core dotnet gui terminal tui pdcurses

sharpie's Introduction

Build, Test and Release codecov Issues License .NET 6 NuGet Downloads

Sharpie

Sharpie is a terminal manipulation library based on NCurses and targeting .NET 6 (dotnet core) and above.

Demo

Or, if you prefer the Snake game...

Demo

Platforms

Sharpie supports any platform that .NET 6.0+, ncurses, pdcurses and pdcurses-mod support. This essentially means that Linux, Macos and Windows are supported on both x64 and ARM64 architectures.

For more information on supported platforms visit .NET Supported Platforms Page and NCurses Packages Page.

Sharpie provides unofficial builds for all the supported libraries located in the lib directory

Installation

Install the main library by adding the NuGet package:

dotnet add package Sharpie-Curses

Additionally, one can install packages containing prebuilt native libraries. These packages are useful when targeting platforms that do not bundle a supported curses library by default (e.g. Windows).

dotnet add package Sharpie-Libs-PdCurses
dotnet add package Sharpie-Libs-PdCursesMod
dotnet add package Sharpie-Libs-NCurses

Features

Almost all of Curses functionality is exposed through Sharpie. What follows is a list of suppoerted features:

  1. Terminal allows interfacing with terminal functionality exposed by Curses,
  2. Screen abstracts away the screen handling,
  3. Pad abstracts away the off-screen windows (known as pads),
  4. SubPad and SubWindow carves out areas of their respective parents for easy management,
  5. EventPump allows developers to listen for Curses events such as key presses, mouse moves and resizes,
  6. ColorManager abstracts away the management of colors,
  7. Even SoftLabelKeys are supported (though antiquated),

In addition to wrapping NCurses, this library also adds numerous quality-of-life improvements such as:

  1. Management of overlapping Windows within a Screen,
  2. Proper Window resizing and placement during terminal resizing events,
  3. Automatic Screen refresh management and support for atomic refreshes,
  4. Supports SynchronizationContext-type execution,
  5. Supports protected/synchronized or raw access to all classes,
  6. And other small additions here and there...

Reasons

There are a number of libraries out there that already offer bindings to NCurses. One of the more popular one is Terminal.Gui; and others such as dotnet-curses also exist.

So why another one? The are many reasons, but the most important ones are:

  1. There is no .NET, object-oriented version of NCurses bindings,
  2. Existing versions are old, or are targeting old versions of .NET which do not benefit from numerous advances in the .NET platform,
  3. No other library exposes most of Curses functionality,
  4. Testing is either very limited or completely non-existent.
  5. And finally -- because I wanted to dabble in Curses.

How To

What follows is a small example of how to use the library: Demo

// Create the terminal instance without any non-standard settings.
using var terminal = new Terminal(CursesBackend.Load(), new(ManagedWindows: true));

// Set the main screen attributes for text and drawings.
terminal.Screen.ColorMixture = terminal.Colors.MixColors(StandardColor.Green, StandardColor.Blue);

// Draw a border on the screen.
terminal.Screen.DrawBorder();

// Force a refresh so that all drawings will be actually pushed to teh screen.
terminal.Screen.Refresh();

// Create a child window within the terminal to operate within.
// The other cells contain the border so we don't want to overwrite those.
var subWindow = terminal.Screen.SubWindow(
    new(1, 1, terminal.Screen.Size.Width - 2, terminal.Screen.Size.Height - 2));

// Process all events coming from the terminal.
foreach (var @event in terminal.Events.Listen(subWindow))
{
    // Write the  event that occured.
    subWindow.WriteText($"{@event}\n");
    
    // If the event is a resize, change the size of the child window
    // to allow for the screen to maintain its border.
    // And then redraw the border of the main screen.
    if (@event is TerminalResizeEvent re)
    {
        subWindow.Size = new(re.Size.Width - 2, re.Size.Height - 2);
        terminal.Screen.DrawBorder();
    }
    
    // If the user pressed CTRL+C, break the loop.
    if (@event is KeyEvent { Key: Key.Character, Char.IsAscii: true, Char.Value: 'C', Modifiers: ModifierKey.Ctrl })
    {
        break;
    }
}

As you can imagine, there are numerous other uses built into the library. Start out by reading Setting Up The Terminal wiki page.

sharpie's People

Contributors

dependabot[bot] avatar pavkam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sharpie's Issues

SoftKeys

Hi, nice library! I was testing out the demos in your repo, and I can't seem to get SoftKeys to work, unless I use PdCursesMod, which in it's turn seem to have issues with the font in Visual Studio 2022 Dev Console (garbled characters and refresh issues as far as I can tell). PdCurses works fine and all demos do work without issues, just the slk demo that is problematic. So i guess my question is, is Softkeys only supported in PdCursesMod or is it supposed to be working in PdCurses and Ncurses as well?

Here's the error that's thrown:
Sharpie.CursesOperationException: 'The call to slk_init failed: Failed to initialize soft label keys.'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.