-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutility.d
148 lines (117 loc) · 3.63 KB
/
utility.d
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
/*
This file sends various command sequences that terminalemulator.d understands.
It should be usable as both a library for your applications and from the command line.
./terminal-emulator-utility changeDefaultCursor bar
^^^^^^^^^^^^^^^^^^^ ^^^
function name arguments
Compile with -version=terminalextensions_commandline to produce the standalone binary.
Perhaps terminal.d can use this too with ufcs.
*/
module arsd.terminalextensions;
import arsd.terminal; // for sending the commands
import arsd.terminalemulator; // for the various enums and small image encoding functions
import ac = arsd.color;
export void changeWindowIcon(Terminal* t, string filename) {
import arsd.png;
auto image = readPng(filename);
auto ii = cast(IndexedImage) image;
assert(ii !is null);
/*
foreach(idx, b; ii.data) {
auto c = ii.palette[b];
changeBackgroundColor(t, c);
t.write(" ");
if(idx % 16 == 0) {
t.color(terminal.Color.DEFAULT, terminal.Color.DEFAULT, ForceOption.alwaysSend);
t.writeln();
}
}*/
t.writeStringRaw("\033]5000;"~encodeSmallTextImage(ii)~"\007");
}
export void changeForegroundColor(Terminal* t, ac.Color c) {
import std.string;
t.writeStringRaw(format("\033[38;2;%d;%d;%dm", c.r, c.g, c.b));
}
export void changeBackgroundColor(Terminal* t, ac.Color c) {
import std.string;
t.writeStringRaw(format("\033[48;2;%d;%d;%dm", c.r, c.g, c.b));
}
export void changeDefaultCursor(Terminal* t, TerminalEmulator.CursorStyle style) {
t.writeStringRaw("");
}
export void changeCurrentCursor(Terminal* t, TerminalEmulator.CursorStyle style) {
// default is [0 q
final switch(style) {
case TerminalEmulator.CursorStyle.block:
t.writeStringRaw("\033[2 q");
break;
case TerminalEmulator.CursorStyle.underline:
t.writeStringRaw("\033[4 q");
break;
case TerminalEmulator.CursorStyle.bar:
t.writeStringRaw("\033[6 q");
break;
}
}
export void displayImage(Terminal* t, string filename) {
import std.base64;
ubyte[] data;
if(filename == "-") {
import std.stdio;
foreach(chunk; stdin.byChunk(4096))
data ~= chunk;
} else {
import std.file;
data = cast(ubyte[]) std.file.read(filename);
}
t.writeStringRaw("\000");
t.writeStringRaw(extensionMagicIdentifier);
t.writeStringRaw(Base64.encode(data));
t.writeStringRaw("\000");
}
// intended for things like attaching screen
export void clearScrollbackHistory(Terminal* t) {
}
void addScrollbackHistory(Terminal* t, string[] history) {
}
version(terminalextensions_commandline) {
template PT(alias a) { alias PT = a; }
void main(string[] args) {
/*
if(env["TERM_EXTENSIONS"] != "arsd")
writeln("Warning: extensions may not be available on this terminal");
*/
auto term = Terminal(ConsoleOutputType.linear);
term._suppressDestruction = true;
string[] functions;
alias mod = PT!(mixin("arsd.terminalextensions"));
foreach(member; __traits(allMembers, mod))
static if(__traits(compiles, PT!(__traits(getMember, mod, member)))) {
alias mem = PT!(__traits(getMember, mod, member));
static if(__traits(getProtection, mem) == "export") {
if(args.length > 1 && member == args[1]) {
// call it
import std.traits;
import std.conv;
ParameterTypeTuple!mem a;
a[0] = &term;
foreach(i, arg; a) {
static if(i) {
static if(is(typeof(arg) == ac.Color))
a[i] = ac.Color.fromString(args[i + 1]);
else
a[i] = to!(typeof(a[i]))(args[i + 1]);
}
}
mem(a);
return;
}
functions ~= member;
}
}
term.writeln("Command not found, valid functions are:");
foreach(func; functions)
term.writeln("\t", func);
term.flush();
}
}