forked from paulfitz/daff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableIO.hx
204 lines (188 loc) · 4.44 KB
/
TableIO.hx
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
#if !TOPLEVEL
package coopy;
#end
/**
*
* System services for the daff command-line utility.
*
*/
@:expose
class TableIO {
public function new() {
}
/**
*
* Check if system services are in fact implemented. For some
* platforms, an external implementation needs to be passed in.
*
*/
public function valid() : Bool {
#if coopyhx_util
return true;
#else
return false;
#end
}
/**
*
* Read a file.
* @param name the name of the file to read
* @return the content of the file
*
*/
public function getContent(name: String) : String {
#if coopyhx_util
return sys.io.File.getContent(name);
#else
return "";
#end
}
/**
*
* Save a file.
* @param name the name of the file to save
* @param txt the content of the file
* @return true on success
*
*/
public function saveContent(name: String, txt: String) : Bool {
#if coopyhx_util
sys.io.File.saveContent(name,txt);
return true;
#else
return false;
#end
}
/**
*
* @return the command-line arguments
*
*/
public function args() : Array<String> {
#if coopyhx_util
return Sys.args();
#else
return [];
#end
}
/**
*
* @param txt text to write to standard output stream
*
*/
public function writeStdout(txt: String) : Void {
#if coopyhx_util
Sys.stdout().writeString(txt);
#end
}
/**
*
* @param txt text to write to standard error stream
*
*/
public function writeStderr(txt: String) : Void {
#if coopyhx_util
Sys.stderr().writeString(txt);
#end
}
/**
*
* Execute a command.
* @param cmd the command to execute
* @param args the arguments to pass
* @return the return value of the command
*
*/
public function command(cmd:String, args:Array<String>) : Int {
#if coopyhx_util
try {
return Sys.command(cmd,args);
} catch (e: Dynamic) {
return 1;
}
#else
return 1;
#end
}
/**
*
* @return true if the platform has no built-in way to call a command
* synchronously i.e. IT IS (OLD) NODE
*
*/
public function hasAsync() : Bool {
return false;
}
/**
*
* Check if a file exists.
* @param path the name of the (putative) file
* @return true if the file exists
*
*/
public function exists(path:String) : Bool {
#if coopyhx_util
return sys.FileSystem.exists(path);
#else
return false;
#end
}
/**
*
* @return true if we can determine whether the output is a TTY. This needs to be
* implemented natively, I haven't found a call for this in Haxe.
*
*/
public function isTtyKnown() : Bool {
#if python
return true;
#else
return false;
#end
}
/**
*
* @return true if output is a TTY. Only trustworthy if isTtyKnown() is true.
*
*/
public function isTty() : Bool {
#if python
if (python.Syntax.code("__import__('sys').stdout.isatty()")) return true;
#end
#if js
return true;
#else
if (Sys.getEnv("GIT_PAGER_IN_USE")=="true") return true;
return false;
#end
}
/**
*
* Try to open an sqlite database.
* @param path to the database
* @return opened database, or null on failure
*
*/
public function openSqliteDatabase(path: String) : SqlDatabase {
#if python
return (python.Syntax.code("SqliteDatabase(path,path)"));
#end
return null;
}
public function sendToBrowser(html: String) : Void {
#if python
// Python implementation of sendToBrowser is simpler than JS version -
// we don't try to clean up the temporary file immediately, but rely
// on the OS to clean it up in its own good time (typically on reboot).
// TODO: replicate JS version in env/js/util.js that avoids need for
// temporary file.
var f = python.Syntax.code("__import__('tempfile').NamedTemporaryFile('wb', delete=False, suffix='.html')");
python.Syntax.code("f.write(html.encode('utf-8', 'strict'))");
python.Syntax.code("f.close()");
python.Syntax.code("__import__('webbrowser').open('file://' + f.name)");
#else
trace("do not know how to send to browser in this language");
#end
}
}