mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-25 20:48:24 +01:00
45 lines
2.1 KiB
C#
45 lines
2.1 KiB
C#
using Brutal.Strings;
|
|
|
|
namespace Brutal.NotCursesApi;
|
|
|
|
/// <summary>
|
|
/// Hand-written ergonomics for the notcurses direct-mode handle. Most instance methods are
|
|
/// auto-generated by <c>Brutal.NotCurses.Generate</c> (see <c>Generated/Wrappers.gen.cs</c>);
|
|
/// only the things the generator can't infer (static factory, IDisposable, convenience overloads) live here.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Direct mode doesn't take over the terminal. It lets you write colored/styled output inline.
|
|
/// Init once at the start, write what you need, then <see cref="Dispose"/>.
|
|
/// </remarks>
|
|
public readonly unsafe partial struct NcDirectHandle : IDisposable
|
|
{
|
|
/// <summary>Initializes notcurses in direct mode against <c>stdout</c>. Throws on init failure.</summary>
|
|
/// <param name="termtype">The terminfo entry to use; default (<c>default</c>) reads <c>$TERM</c>.</param>
|
|
/// <param name="flags">Bitmask of <see cref="NcDirectOption"/> flags.</param>
|
|
public static NcDirectHandle Init(RefString8 termtype = default, NcDirectOption flags = default)
|
|
{
|
|
var d = NotCurses.PInvoke.ncdirect_core_init(termtype, default, flags);
|
|
if (d.IsNull())
|
|
throw new InvalidOperationException("ncdirect_core_init failed.");
|
|
return d;
|
|
}
|
|
|
|
/// <summary><c>string</c> overload of <see cref="Init(RefString8, NcDirectOption)"/>; encodes via <see cref="ScratchBuffer"/>.</summary>
|
|
public static NcDirectHandle Init(string? termtype, NcDirectOption flags = default)
|
|
{
|
|
if (termtype is null)
|
|
return Init(default(RefString8), flags);
|
|
using var buf = new ScratchBuffer(termtype, stackalloc byte[256]);
|
|
return Init(buf.Bytes, flags);
|
|
}
|
|
|
|
/// <summary>Convenience overload: write a UTF-8 string with default terminal colors (channels = 0).</summary>
|
|
public void PutStr(string text)
|
|
{
|
|
using var buf = new ScratchBuffer(text, stackalloc byte[256]);
|
|
PutStr(0, buf.Bytes);
|
|
}
|
|
|
|
/// <summary>Tears down direct mode and restores any modified terminal state. Calls <see cref="Stop"/>.</summary>
|
|
public void Dispose() => Stop();
|
|
}
|