Skip to content

Commit

Permalink
add - doc - Added wrapped writer
Browse files Browse the repository at this point in the history
---

We've added a new wrapped writer that uses the text viewer interactive
TUI.

---

Type: add
Breaking: False
Doc Required: True
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Jan 7, 2025
1 parent 61a7227 commit 731b24a
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// Terminaux Copyright (C) 2023-2025 Aptivi
//
// This file is part of Terminaux
//
// Terminaux is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Terminaux is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using Terminaux.Writer.ConsoleWriters;

namespace Terminaux.Console.Fixtures.Cases.Inputs.CJK
{
internal class TestTextViewerFullscreenInteractiveCjk : IFixture
{
public FixtureCategory Category => FixtureCategory.InputCjk;

public void RunFixture()
{
string toBeEdited =
"""
我们已经删除了使用 mod 部件来组织多个同名的 mod,因为当 mod 只由单个源代码文件(Visual Basic 或 C#)
组成时,它们是相关的,并且大型 mod 将使用多个源代码文件进行分组,这些源代码文件相互连接。 互相运作。
某些版本之前,我们删除了基于源代码的 mod,并依赖 mod 开发人员制作 .DLL 版本,以确保它们获得最大的灵
活性。 由于其刚性,此功能不会再出现。 Nitrocid KS 的所有 API v3.0 之前的版本(包括 0.0.24.x)都以 KS
作为其根命名空间,因为当时的名称是 Kernel Simulator。 我们给这个应用程序命名为 Nitrocid KS,内核名称
已更改为 Nitrocid Kernel。 因此,根命名空间已从 KS 更改为 Nitrocid,以便与应用程序品牌更加一致。 由于
历史原因,一些重大变更注释仍然使用 KS 根命名空间,不应修改它们,因为它们列出了上一个版本和下一个版本之
间发生的更改历史记录。 您必须更新导入以指向新的根命名空间。
""";
WrappedWriter.WriteWrapped(toBeEdited);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// Terminaux Copyright (C) 2023-2025 Aptivi
//
// This file is part of Terminaux
//
// Terminaux is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Terminaux is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using Terminaux.Writer.ConsoleWriters;

namespace Terminaux.Console.Fixtures.Cases.Inputs
{
internal class TestTextViewerFullscreenInteractive : IFixture
{
public FixtureCategory Category => FixtureCategory.Input;

public void RunFixture()
{
string toBeEdited =
"""
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Quam lacus suspendisse faucibus interdum. Fermentum odio eu
feugiat pretium nibh ipsum consequat nisl. Purus in mollis nunc sed id semper. Ultrices sagittis
orci a scelerisque purus. Malesuada nunc vel risus commodo viverra maecenas accumsan lacus vel.
Nibh tortor id aliquet lectus proin. Mus mauris vitae ultricies leo integer malesuada nunc vel.
Proin fermentum leo vel orci porta non pulvinar. Nulla posuere sollicitudin aliquam ultrices
sagittis orci a scelerisque. Lacus vel facilisis volutpat est. Porta lorem mollis aliquam ut
porttitor leo. Feugiat scelerisque varius morbi enim nunc faucibus a pellentesque sit. A erat
nam at lectus. Semper risus in hendrerit gravida rutrum. Aliquet enim tortor at auctor urna nunc.
Eget nunc scelerisque viverra mauris in. Augue lacus viverra vitae congue eu consequat ac. Non
odio euismod lacinia at quis risus. Ullamcorper sit amet risus nullam. Malesuada fames ac turpis
egestas. Odio ut sem nulla pharetra diam sit. Viverra ipsum nunc aliquet bibendum enim facilisis
gravida. Urna neque viverra justo nec ultrices dui sapien eget mi. Donec massa sapien faucibus et
molestie ac feugiat sed. Potenti nullam ac tortor vitae purus faucibus ornare suspendisse sed.
Eleifend mi in nulla posuere sollicitudin aliquam ultrices sagittis orci. Arcu cursus vitae
congue mauris. Odio euismod lacinia at quis risus sed vulputate odio ut. Quis enim lobortis
scelerisque fermentum dui faucibus in ornare quam.
""";
WrappedWriter.WriteWrapped(toBeEdited);
}
}
}
2 changes: 2 additions & 0 deletions private/Terminaux.Console/Fixtures/FixtureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ internal static class FixtureManager
new TestHexViewerInteractive(),
new TestTextEditorInteractive(),
new TestTextViewerInteractive(),
new TestTextViewerFullscreenInteractive(),
new TestTestFixturesInteractive(),
new TestTestFixtures(),

Expand Down Expand Up @@ -256,6 +257,7 @@ internal static class FixtureManager
new TestHexViewerInteractiveCjk(),
new TestTextEditorInteractiveCjk(),
new TestTextViewerInteractiveCjk(),
new TestTextViewerFullscreenInteractiveCjk(),

// Color
new ColorTest(),
Expand Down
25 changes: 15 additions & 10 deletions public/Terminaux/Inputs/Styles/Editor/TextViewInteractive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public static class TextViewInteractive
/// </summary>
/// <param name="lines">Target number of lines</param>
/// <param name="settings">TUI settings</param>
public static void OpenInteractive(List<string> lines, InteractiveTuiSettings? settings = null)
/// <param name="fullscreen">Whether it's a fullscreen viewer or not</param>
public static void OpenInteractive(List<string> lines, InteractiveTuiSettings? settings = null, bool fullscreen = false)
{
// Set status
status = "Ready";
Expand All @@ -84,14 +85,18 @@ public static void OpenInteractive(List<string> lines, InteractiveTuiSettings? s
// Now, render the keybindings
RenderKeybindings(ref screen, settings);

// Render the box
RenderTextViewBox(ref screen, settings);
// Check to see if we need to render the box and the status
if (!fullscreen)
{
// Render the box
RenderTextViewBox(ref screen, settings);

// Now, render the visual hex with the current selection
RenderContentsWithSelection(lineIdx, ref screen, lines, settings);
// Render the status
RenderStatus(ref screen, settings);
}

// Render the status
RenderStatus(ref screen, settings);
// Now, render the visual text with the current selection
RenderContentsWithSelection(lineIdx, ref screen, lines, settings, fullscreen);

// Wait for a keypress
ScreenTools.Render(screen);
Expand Down Expand Up @@ -186,7 +191,7 @@ private static void RenderTextViewBox(ref Screen screen, InteractiveTuiSettings
screen.AddBufferedPart("Text editor interactive - Text view box", part);
}

private static void RenderContentsWithSelection(int lineIdx, ref Screen screen, List<string> lines, InteractiveTuiSettings settings)
private static void RenderContentsWithSelection(int lineIdx, ref Screen screen, List<string> lines, InteractiveTuiSettings settings, bool fullscreen)
{
// First, update the status
StatusTextInfo(lines);
Expand All @@ -201,14 +206,14 @@ private static void RenderContentsWithSelection(int lineIdx, ref Screen screen,
{
// Get the widths and heights
int SeparatorConsoleWidthInterior = ConsoleWrapper.WindowWidth - 2;
int SeparatorMinimumHeightInterior = 2;
int SeparatorMinimumHeightInterior = fullscreen ? 0 : 2;

// Get the colors
var unhighlightedColorBackground = settings.BackgroundColor;
var highlightedColorBackground = settings.PaneSelectedItemBackColor;

// Get the start and the end indexes for lines
int lineLinesPerPage = ConsoleWrapper.WindowHeight - 4;
int lineLinesPerPage = ConsoleWrapper.WindowHeight - 4 + SeparatorMinimumHeightInterior;
int currentPage = lineIdx / lineLinesPerPage;
int startIndex = lineLinesPerPage * currentPage + 1;
int endIndex = lineLinesPerPage * (currentPage + 1);
Expand Down
91 changes: 91 additions & 0 deletions public/Terminaux/Writer/ConsoleWriters/WrappedWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// Terminaux Copyright (C) 2023-2025 Aptivi
//
// This file is part of Terminaux
//
// Terminaux is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Terminaux is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using System;
using System.Diagnostics;
using System.Linq;
using Terminaux.Base;
using Terminaux.Base.Extensions;
using Terminaux.Colors;
using Terminaux.Inputs.Styles.Editor;
using Textify.General;

namespace Terminaux.Writer.ConsoleWriters
{
/// <summary>
/// Wrapped writer similar to <c>less</c> and <c>more</c> commands on Unix
/// </summary>
public static class WrappedWriter
{
/// <summary>
/// Writes the text in a pager similar to <c>less</c> and <c>more</c> commands on Unix
/// </summary>
/// <param name="text">Text to write. If it's shorter than the console height, it just prints the text</param>
/// <param name="force">Forces the text viewer to open, even if the text doesn't exceed the console height</param>
/// <param name="args">Arguments to format the text</param>
public static void WriteWrapped(string text, bool force = false, params object?[]? args) =>
WriteWrapped(text, ColorTools.currentForegroundColor, force, args);

/// <summary>
/// Writes the text in a pager similar to <c>less</c> and <c>more</c> commands on Unix
/// </summary>
/// <param name="text">Text to write. If it's shorter than the console height, it just prints the text</param>
/// <param name="force">Forces the text viewer to open, even if the text doesn't exceed the console height</param>
/// <param name="color">A color that will be changed to.</param>
/// <param name="args">Arguments to format the text</param>
public static void WriteWrapped(string text, Color color, bool force = false, params object?[]? args) =>
WriteWrapped(text, color, ColorTools.currentBackgroundColor, force, args);

/// <summary>
/// Writes the text in a pager similar to <c>less</c> and <c>more</c> commands on Unix
/// </summary>
/// <param name="text">Text to write. If it's shorter than the console height, it just prints the text</param>
/// <param name="force">Forces the text viewer to open, even if the text doesn't exceed the console height</param>
/// <param name="foregroundColor">A foreground color that will be changed to.</param>
/// <param name="backgroundColor">A background color that will be changed to.</param>
/// <param name="args">Arguments to format the text</param>
public static void WriteWrapped(string text, Color foregroundColor, Color backgroundColor, bool force = false, params object?[]? args)
{
lock (TextWriterRaw.WriteLock)
{
try
{
// Use the text viewer to avoid code repetition
text = text.FormatString(args);
var lines = ConsoleMisc.GetWrappedSentencesByWords(text, ConsoleWrapper.WindowWidth).ToList();
if (force || lines.Count >= ConsoleWrapper.WindowHeight)
{
TextViewInteractive.OpenInteractive(lines, new()
{
PaneSelectedItemBackColor = foregroundColor,
BackgroundColor = backgroundColor,
}, true);
}
else
TextWriterColor.WriteColorBack(text, foregroundColor, backgroundColor);
}
catch (Exception ex)
{
Debug.WriteLine(ex.StackTrace);
Debug.WriteLine($"There is a serious error when printing text. {ex.Message}");
}
}
}
}
}

0 comments on commit 731b24a

Please sign in to comment.