所以,我一直在尝试通过bash提示进行自定义,以便它看起来像
[feralin@localhost ~]$ _
Run Code Online (Sandbox Code Playgroud)
与颜色.我设法得到恒定的颜色(每次看到提示时颜色相同)但我希望用户名('feralin')显示为红色,而不是绿色,如果最后一个命令具有非零退出状态.我提出了:
\e[1;33m[$(if [[ $? == 0 ]]; then echo "\e[0;31m"; else echo "\e[0;32m"; fi)\u\e[m@\e[1;34m\h \e[0;35m\W\e[1;33m]$ \e[m
Run Code Online (Sandbox Code Playgroud)
但是,根据我的观察,$(if ...; fi)似乎在.bashrc运行时评估一次,结果在之后永远被替换.这使得名称始终为绿色,即使最后一个退出代码非零(如,echo $?).这是发生了什么?或者我的提示是否只是其他错误?长问题简介,我如何获得使用最后退出代码的提示?
我正在为Windows编写一个基本shell,我想知道是否有任何方法可以运行subprocess(Process process)以便它使用当前的控制台窗口.我的意思是说我不希望重定向输入/输出; 我希望进程从当前控制台获取输入并将输出直接打印到同一控制台窗口.
原因是我希望允许此子进程为输出设置控制台颜色,如果我重定向进程的标准输出,则不会发生这种情况.另外,我目前使用的是代码
while (!process.HasExited)
process.StandardInput.WriteLine(Console.ReadLine());
Run Code Online (Sandbox Code Playgroud)
将标准输入重定向到流程.然而,如果处理的输入后,立即离开(例如,I型"退出" + Enter,并且处理退出),此循环将执行一次以上,所以在控制台是从将永远不会被使用的用户等待输入通过这个过程(它即将退出).
所以,问题很简单,如何在当前控制台中运行一个进程,以便它可以设置控制台颜色并直接从控制台获取输入?
编辑:以下是我的代码中与此问题相关的方法:
static int runExe(string exePath, params string[] args)
{
ProcessStartInfo startInfo = new ProcessStartInfo(exePath, args)
{
ErrorDialog = false,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true
};
Process process = new Process() { StartInfo = startInfo };
process.Start();
ReadThreadState stdout = readThread(process.StandardOutput, false);
ReadThreadState stderr = readThread(process.StandardError, true);
while (!process.HasExited)
process.StandardInput.WriteLine(Console.ReadLine()); …Run Code Online (Sandbox Code Playgroud) 我正在用 C# 编写一个 web 服务器,只是为了好玩,我能够为我的浏览器提供基本的文本文件。但是,在提供图像(例如image.png)时,我在我的服务器上测试的所有浏览器(IE、Firefox 和 Chrome)都会显示图像的某种占位符缩略图,就好像图像已损坏或无效一样。
我发送到浏览器的响应看起来像
HTTP/1.0 200 Ok
Content-Type: image/png
Content-Length: 14580053
{image data here}
Run Code Online (Sandbox Code Playgroud)
我是否使用了正确的 HTTP 标头?或者,如果我是,为什么浏览器不接受图像?
我有这段代码(实际上是垃圾收集的Forth系统的解释器的一部分):
#define PRIMITIVE(name) \
do \
{ \
VocabEntry* entry = (VocabEntry*)gc_alloc(sizeof(VocabEntry)); \
entry->code = name; \
entry->name = cstr_to_pstr(#name); \
entry->prev = latest_vocab_entry; \
latest_vocab_entry = entry; \
} \
while (false)
PRIMITIVE(dup);
PRIMITIVE(drop);
PRIMITIVE(swap);
// and a lot more
Run Code Online (Sandbox Code Playgroud)
但是有一个问题:在线
entry->name = cstr_to_pstr(#name);
Run Code Online (Sandbox Code Playgroud)
该name字段被取代dup,drop,swap,和其余部分.我希望字段名称不被替换.
那么,有没有办法解决这个问题,除了简单地重命名宏参数?
作为答案,请解释一般是否有一种方法来抑制宏体中宏参数名称的替换.不要回答"只是这样做"(请).
假设我在字符串中有一些 python 代码
code = """
a = 42
a
"""
Run Code Online (Sandbox Code Playgroud)
和我exec那串代码:
result = exec(code)
Run Code Online (Sandbox Code Playgroud)
那么result永远都是None。有没有办法获得最后一个表达式的值?在这种情况下,那将是5, 因为a是最后一个表达式。
编辑:这是我询问的功能的另一个示例。假设我们有 python 代码(存储在变量中code)
a = 100
sqrt(a)
Run Code Online (Sandbox Code Playgroud)
那么我怎样才能以这样的方式执行代码以给我结果10- 也就是说,sqrt(a)?
编辑 编辑:另一个例子:我希望的代码exec是
function_a()
function_b()
function_c()
Run Code Online (Sandbox Code Playgroud)
有什么办法可以定义某种magic_exec函数,以便
magic_exec(code)
Run Code Online (Sandbox Code Playgroud)
会为我提供价值function_c()吗?
是否有可能(在C#中)使checked(...)表达式具有动态"范围"以进行溢出检查?换句话说,在以下示例中:
int add(int a, int b)
{
return a + b;
}
void test()
{
int max = int.MaxValue;
int with_call = checked(add(max, 1)); // does NOT cause OverflowException
int without_call = checked(max + 1); // DOES cause OverflowException
}
Run Code Online (Sandbox Code Playgroud)
因为在表达式中checked(add(max, 1)),函数调用会导致溢出,而不会OverflowException抛出,即使在表达式的动态范围内存在溢出checked(...).
有没有办法让两种方式评估int.MaxValue + 1投掷OverflowException?
编辑:嗯,要么告诉我是否有办法,或者给我一个更好的方法来做到这一点(请).
我认为我需要这个的原因是因为我的代码如下:
void do_op(int a, int b, Action<int, int> forSmallInts, Action<long, long> forBigInts)
{
try
{
checked(forSmallInts(a, b));
} …Run Code Online (Sandbox Code Playgroud) 这个问题很简单:有没有什么办法让问题System.Type从S InvalidCastException?我希望能够以诸如"Expected {to-type}; found {from-type}"之类的格式显示有关失败类型转换的信息,但我找不到访问所涉及类型的方法.
编辑:我需要能够访问所涉及的类型的原因是因为我有一些时间有关于较短名称的信息.例如,RFSmallInt我想说的是类型,而不是类型smallint.而不是错误消息
Unable to cast object of type 'ReFactor.RFSmallInt' to type 'ReFactor.RFBigInt'.
Run Code Online (Sandbox Code Playgroud)
我其实想要展示
Expected bigint; recieved smallint.
Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码测试出现问题之前计算机可以处理的线程数:
static void Main(string[] args)
{
List<Thread> threads = new List<Thread>();
int count = 0;
try
{
while (true)
{
Console.Write('m'); // make
Thread thread = new Thread(() => { Thread.Sleep(Timeout.Infinite); }, 1024 * 64);
Console.Write('s'); // start
thread.Start();
Console.Write('p'); // suspend
thread.Suspend();
Console.Write('a'); // add
threads.Add(thread);
Console.Write(' ');
Console.WriteLine(count++);
}
}
catch (Exception e)
{
Console.WriteLine("\nGot exception of type " + e.GetType().Name);
}
Console.WriteLine(count);
Console.ReadKey(true);
}
Run Code Online (Sandbox Code Playgroud)
当系统无法创建更多线程时,我被期望new Thread(...)构造函数抛出异常(可能OutOfMemoryException),而构造函数挂起并且永远不会返回.
而不是上面的输出
...
mspa 67
m
Got exception …Run Code Online (Sandbox Code Playgroud)