Thema: [C#] KConsole

  1. #1
    Avatar von patlux
    Registriert seit
    26.10.2011
    Beiträge
    1.195
    Thanked 1.596 Times in 725 Posts
    Blog Entries
    2

    Standard [C#] KConsole

    Eine Klasse die den KCode teilweise richtig interpretiert auf der Console. Gabs glaube ich schonmal irgendwo, aber egal..

    Die Klasse parsed nur die Farbcodes (°B°) und die einfachen Links (°>text|link<°).
    Ich hab die Klasse eigentlich auch nur erstellt damit ich nicht dauernd selber die ConsoleColor ändern muss...nach der Zeit geht das auf die Nerven.
    Außerdem hab ich mich jetzt nicht exakt an die von Knuddels gegebenen Farbcodes gehalten, sind noch andere drin und kann sein das etwas nicht korrekt ist. Die Farben hab ich mir halt so ausm Kopf gedacht

    Beispiel

    Name:  kconsole.png
Hits: 320
Größe:  5,7 KB

    Name:  kconsole2.png
Hits: 317
Größe:  9,7 KB

    Implementierung

    // 
    static void Main(string[] args)
    {
    Console.SetOut(new KConsole());
    // ....
    }


    Verwendung

    // 
    Console.WriteLine("Hallo °r°deine °B°Mutter");


    Klasse

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;

    namespace Knuddels.Utils
    {
    class KConsole : TextWriter
    {
    private TextWriter _out;
    private ConsoleColor _color;

    public KConsole(ConsoleColor pColor)
    {
    _out = Console.Out;
    _color = pColor;
    Console.ForegroundColor = _color;
    }

    public KConsole()
    : this(ConsoleColor.Gray)
    {

    }

    public override Encoding Encoding
    {
    get { return new System.Text.UTF8Encoding(); }
    }

    public override void WriteLine(string message)
    {
    WriteKCode(message + "\n");
    }
    public override void Write(string message)
    {
    WriteKCode(message);
    }

    private void WriteKCode(string pKCode)
    {
    for (int i = 0; i < pKCode.Length; i++)
    {
    switch (pKCode[i])
    {
    case '°':
    try
    {
    if (((pKCode.Length - 1) >= i + 1) && pKCode[i + 1] == '°')
    {
    Console.ForegroundColor = _color;
    i++;
    continue;
    }
    else if (((pKCode.Length - 1) >= i + 2) && pKCode[i + 2] == '°')
    {
    // °B°
    Console.ForegroundColor = GetColor(pKCode[i + 1].ToString());
    i += 2;
    continue;
    }
    else if (((pKCode.Length - 1) >= i + 3) && pKCode[i + 3] == '°')
    {
    // °BB°
    Console.ForegroundColor = GetColor(pKCode[i + 1] + "" + pKCode[i + 2]);
    i += 3;
    continue;
    }
    else if (((pKCode.Length - 1) >= i + 1) && pKCode[i + 1] == '>' && pKCode.IndexOf("<°", i) > 0)
    {
    // °>text|befehl<°
    int index = pKCode.IndexOf("<°", i);
    string text = pKCode.Substring(i + "°>".Length);
    text = text.Substring(0, text.IndexOf("<°"));
    if (text.StartsWith("_h"))
    text = text.Substring(2);
    if (text.Contains("|"))
    text = text.Substring(0, text.IndexOf('|'));
    _out.Write(text);
    i = index + 1;
    continue;
    }
    }
    catch (Exception ex)
    {

    }
    goto default;
    default:
    _out.Write(pKCode[i]);
    break;

    }
    }
    Console.ForegroundColor = _color;
    }

    private ConsoleColor GetColor(string pColor)
    {
    switch (pColor.ToLower())
    {
    case "r":
    return ConsoleColor.Red;
    case "dr":
    return ConsoleColor.DarkRed;
    case "g":
    return ConsoleColor.Green;
    case "dg":
    return ConsoleColor.DarkGreen;
    case "y":
    return ConsoleColor.Yellow;
    case "dy":
    return ConsoleColor.DarkYellow;
    case "w":
    return ConsoleColor.White;
    case "m":
    return ConsoleColor.Magenta;
    case "dm":
    return ConsoleColor.DarkMagenta;
    case "c":
    return ConsoleColor.Cyan;
    case "dc":
    return ConsoleColor.DarkCyan;
    case "b":
    case "bb":
    return ConsoleColor.Blue;
    case "db":
    return ConsoleColor.DarkBlue;
    }
    return _color;
    }

    }
    }
    Geändert von patlux (30.07.2013 um 14:30 Uhr)

  2. The Following 5 Users Say Thank You to patlux For This Useful Post:

    Dbzfreak1337 (27.07.2013), iToxic (27.07.2013), Nachto (27.07.2013), Snees (27.07.2013), Ta1lor (27.07.2013)

Diese Seite nutzt Cookies, um das Nutzererlebnis zu verbessern. Klicken Sie hier, um das Cookie-Tracking zu deaktivieren.