Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alignment of text #9

Open
Roy-Ermers opened this issue Mar 29, 2016 · 7 comments
Open

alignment of text #9

Roy-Ermers opened this issue Mar 29, 2016 · 7 comments
Assignees

Comments

@Roy-Ermers
Copy link

wouldn't it be handy if we can align text in console?

@tomakita
Copy link
Owner

Hi,

Would it be possible to give some examples of what you mean?

Thanks!

@tomakita tomakita self-assigned this Mar 30, 2016
@Roy-Ermers
Copy link
Author

like Console.WriteLineAligned("hello World!",Color.Red,Align.Center);
then it would align to center, so the text would be in the middle of the window.
i would like to use this feature with acsii.

@codepunkt
Copy link

I'm interested. This would be a welcome addition! 👍

@tomakita
Copy link
Owner

tomakita commented Jun 23, 2017

Thanks for the feedback. I've planned to add alignment as a part of another feature which I'm working on at the moment. Unfortunately, I don't have an ETA (though I warn you that progress is slow due to other commitments), but I will update this thread when I get closer to release!

@xxcanizeusxx
Copy link

xxcanizeusxx commented Jan 18, 2018

For those looking for a simple solution this enables you to write to the console at any position you wish. However, this does not work with the library's ACSII method. Everything moves tot he x and y postion but the top of the ASCII dashes are out of order. If you want plain text this works everywhere in the console window. Please ignore my typewritter function. This function just adds a nice old school sound effect.

public class Screen
    {
        
        private const int delay = 300;
        private static int _leftPos;
        private static int _topPos;

        public Screen(int leftPos, int topPos, int screenWidth, int screenHeight)
        {
            _leftPos = leftPos;
            _topPos = topPos;
            SetUpScreen(screenWidth, screenHeight);
        }

        private static void SetUpScreen(int width, int height)
        {
            IntPtr ptr = GetConsoleWindow();
            MoveWindow(ptr, 0, 0, 1000, 400, true);
            Console.SetWindowSize(width, height);
        }

        public void WriteAt(string message, int x, int y, bool typeWritter, bool newLine)
        {
            try
            {
                Console.SetCursorPosition(_leftPos + x, _topPos + y);
                if(typeWritter && message != null)
                {
                    TypeWritter(message, delay, newLine);
                }
            }
            catch(ArgumentOutOfRangeException e)
            {
                Console.Clear();
                Console.Beep(37, 500);
                Console.Write(e.Message);
            }
        }

        public void TypeWritter(string message, int delay, bool newLine)
        {
            var player = new SoundPlayer
            {
                SoundLocation = @"C:\Users\Erick\Desktop\C#\CanizHospital\CanizHospital\typewriter-key-1.wav"
            };
            foreach (char c in message)
            {
                player.Play();
                Console.Write(c);
                Thread.Sleep(delay);
                player.Stop();
            }
            if (newLine)
            {
                Console.Write(Environment.NewLine);
                player.SoundLocation = @"C:\Users\Erick\Desktop\C#\CanizHospital\CanizHospital\typewriter-return-1.wav";
                player.PlaySync();
                //Thread.Sleep(delay); // Might not be necessary
            }
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr GetConsoleWindow();

        [DllImport("user32.dll", SetLastError = true)]
        internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
    }

@wangshuai-007
Copy link

from msdn:Composite formatting,I can write line with alignment in consol like this:

Console.WriteLine($"  {123,-10}{"--123",-10}")
Console.WriteLine($"  {123456,-10}{"--123",-10}")

And in Colorful.Console,it is also worked.

Colorful.Console.WriteLine($"  {123,-10}{"--123",-10}")
Colorful.Console.WriteLine($"  {123456,-10}{"--123",-10}")

But,when I use this Code

Colorful.Console.WriteLineFormatted("  {0,-10}--{1,-10}",Color.White,new Colorful.Formatter[]{new Colorful.Formatter(123,Color.Red),new Colorful.Formatter("--123",Color.Aqua)});

,I will get error:

異常信息:
Input string was not in a correct format.

调用堆栈:
   at System.Number.StringToNumber(ReadOnlySpan`1 str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseInt32(ReadOnlySpan`1 s, NumberStyles style, NumberFormatInfo info)
   at System.Int32.Parse(String s)
   at Colorful.TextFormatter.GetFormatMap(String input, Object[] args, Color[] colors)
   at Colorful.Console.WriteInColorFormatted[T](String trailer, T target0, Formatter[] targets, Color defaultColor)
   at Colorful.Console.WriteLineFormatted(String format, Color defaultColor, Formatter[] args)

can the Colorful.Console support Alignment ?

@Jirubizu
Copy link

Jirubizu commented Jun 5, 2021

Has this been resolved? I still can't use alignments with formatted texts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants