-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
148 lines (140 loc) · 5.87 KB
/
Copy pathProgram.cs
File metadata and controls
148 lines (140 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System.Text;
using AnsiTui;
Console.OutputEncoding = Encoding.UTF8;
Console.InputEncoding = Encoding.UTF8;
// ─── Parse CLI Arguments ─────────────────────────────────────────────────
string? imagePath = null;
int width = 0;
AnsiColorDepth? colorDepth = null;
AnsiCharMode? charMode = null;
AnsiBgColor? bgColor = null;
AnsiDithering? dithering = null;
string? outputPath = null;
bool interactive = false;
for (int i = 0; i < args.Length; i++)
{
switch (args[i].ToLowerInvariant())
{
case "-i" or "--image":
imagePath = args[++i];
break;
case "-w" or "--width":
width = int.Parse(args[++i]);
break;
case "-c" or "--colors":
colorDepth = args[++i].ToLowerInvariant() switch
{
"true" or "truecolor" or "24bit" => AnsiColorDepth.TrueColor,
"256" => AnsiColorDepth.Color256,
"16" => AnsiColorDepth.Color16,
"bw" or "mono" => AnsiColorDepth.BlackAndWhite,
_ => throw new ArgumentException($"Unknown color depth: {args[i]}")
};
break;
case "-m" or "--mode":
charMode = args[++i].ToLowerInvariant() switch
{
"halfblock" or "half" => AnsiCharMode.HalfBlock,
"quadrant" or "quad" => AnsiCharMode.Quadrant,
"fullblock" or "full" => AnsiCharMode.FullBlock,
"halftone" => AnsiCharMode.Halftone,
"ascii" => AnsiCharMode.Ascii,
"braille" => AnsiCharMode.Braille,
_ => throw new ArgumentException($"Unknown char mode: {args[i]}")
};
break;
case "-b" or "--bg":
bgColor = args[++i].ToLowerInvariant() switch
{
"black" => AnsiBgColor.Black,
"white" => AnsiBgColor.White,
_ => throw new ArgumentException($"Unknown bg color: {args[i]}")
};
break;
case "-d" or "--dither":
dithering = args[++i].ToLowerInvariant() switch
{
"none" => AnsiDithering.None,
"floyd" or "fs" or "floyd-steinberg" => AnsiDithering.FloydSteinberg,
"ordered" or "bayer" => AnsiDithering.Ordered,
_ => throw new ArgumentException($"Unknown dithering: {args[i]}")
};
break;
case "-o" or "--output":
outputPath = args[++i];
break;
case "--interactive":
interactive = true;
break;
case "-h" or "--help":
PrintUsage();
return;
default:
if (!args[i].StartsWith('-') && imagePath == null)
imagePath = args[i];
break;
}
}
// No args or no output path → interactive TUI mode
if (args.Length == 0 || outputPath == null || interactive)
{
var tui = new TuiApp();
tui.Run(imagePath, width, colorDepth, charMode, bgColor, dithering);
}
else
{
// One-shot CLI mode: convert and save
if (width <= 0) width = Math.Min(Console.WindowWidth > 0 ? Console.WindowWidth : 80, 200);
var converter = new AnsiConverter
{
Width = width,
ColorDepth = colorDepth ?? AnsiColorDepth.TrueColor,
CharMode = charMode ?? AnsiCharMode.HalfBlock,
BgColor = bgColor ?? AnsiBgColor.Black,
Dithering = dithering ?? AnsiDithering.None
};
try
{
var lines = converter.ConvertFile(imagePath!);
foreach (var line in lines)
Console.WriteLine(line);
AnsiConverter.SaveToFile(lines, outputPath);
Console.WriteLine($"\x1b[32mSaved to:\x1b[0m {outputPath}");
}
catch (Exception ex)
{
Console.WriteLine($"\x1b[31mError:\x1b[0m {ex.Message}");
}
}
// ─── Usage ───────────────────────────────────────────────────────────────
static void PrintUsage()
{
Console.WriteLine("\x1b[1;36mansi-tui\x1b[0m — Convert images to ANSI terminal art");
Console.WriteLine();
Console.WriteLine("\x1b[1mUsage:\x1b[0m");
Console.WriteLine(" ansi-tui [image] Interactive TUI");
Console.WriteLine(" ansi-tui -i image.png -o out.ans CLI mode (one-shot)");
Console.WriteLine();
Console.WriteLine("\x1b[1mOptions:\x1b[0m");
Console.WriteLine(" -i, --image <path> Input image file");
Console.WriteLine(" -w, --width <cols> Output width in characters (10-500)");
Console.WriteLine(" -c, --colors <depth> truecolor|256|16|bw");
Console.WriteLine(" -m, --mode <mode> halfblock|quadrant|braille|fullblock|halftone|ascii");
Console.WriteLine(" -d, --dither <type> none|floyd|ordered");
Console.WriteLine(" -b, --bg <color> black|white");
Console.WriteLine(" -o, --output <path> Save to .ans file (enables CLI mode)");
Console.WriteLine(" --interactive Force interactive TUI even with -o");
Console.WriteLine(" -h, --help Show this help");
Console.WriteLine();
Console.WriteLine("\x1b[1mTUI Keys:\x1b[0m");
Console.WriteLine(" M/C/D/W/B Change Mode/Colors/Dither/Width/Background");
Console.WriteLine(" I Change image");
Console.WriteLine(" P Paste image from clipboard");
Console.WriteLine(" S Save to .ans file");
Console.WriteLine(" ↑↓ PgUp PgDn Scroll art");
Console.WriteLine(" Q / Esc Quit");
Console.WriteLine();
Console.WriteLine("\x1b[1mExamples:\x1b[0m");
Console.WriteLine(" ansi-tui photo.jpg");
Console.WriteLine(" ansi-tui -i photo.png -w 120 -m braille -c truecolor -o art.ans");
}