假设我在PowerShell窗口中打印100行带有随机背景颜色的行。我该如何确定事实之后应用于特定行和字符的背景颜色?
我尝试更改位置并打印控制台背景色,但是我怀疑显示的背景色已经设置为输出默认值:
$pos = (Get-Host).UI.RawUI.CursorPosition
$pos.X = 10
$pos.Y = 10
(Get-Host).UI.RawUI.CursorPosition = $pos
Write-Host (Get-Host).UI.RawUI.BackgroundColor
答案 0 :(得分:0)
正如PetSerAl在评论中指出的那样, String url="http://www.mclista.pl/json/daj_diax/";
URL object=new URL(url);
HttpURLConnection con = (HttpURLConnection) object.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("dataType", "JSON");
con.setRequestMethod("POST");
JSONObject cred = new JSONObject();
cred.put("id_serwera", "41673").put("csrf_mclista_token", "587fb64c54defa881d293ee1aadb93fa");
System.out.println(new String(cred.toString().getBytes(StandardCharsets.UTF_8)));
OutputStream os = con.getOutputStream();
os.write(cred.toString().getBytes("UTF-8"));
os.close();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
能够从缓冲区中获取任意范围的内容,包括颜色。我将其包装在一个可能会用于此目的的函数中:
$Host.UI.RawUI.GetBufferContents
用法:
function Get-ColorsAtPosition {
Param(
[Parameter(Mandatory=$true)]
[int]$x,
[Parameter(Mandatory=$true)]
[int]$y
)
$colors = $Host.UI.RawUI.GetBufferContents(@{
Left=$x; Right=$x; Top=$y; Bottom=$y;
})
return @{
ForegroundColor=$colors.ForegroundColor;
BackgroundColor=$colors.BackgroundColor;
}
}
请记住,$c = Get-ColorsAtPosition -x 3 -y 5
$c.ForegroundColor # White
$c.BackgroundColor # Blue
和-x
的索引为零。