Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Merging from dotnet/corefx
Browse files Browse the repository at this point in the history
  • Loading branch information
bartdesmet committed Oct 10, 2015
2 parents 22d5ae9 + daa6140 commit 1cf8578
Show file tree
Hide file tree
Showing 26 changed files with 676 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Sys
{
[StructLayout(LayoutKind.Sequential)]
private struct WinSize
{
internal ushort Row;
internal ushort Col;
internal ushort XPixel;
internal ushort YPixel;
};

[DllImport(Libraries.SystemNative, SetLastError = true)]
private static extern int GetWindowSize(out WinSize winSize);

internal static int GetWindowWidth()
{
WinSize winsize;

if (GetWindowSize(out winsize) == 0)
{
return winsize.Col;
}

return -1;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Runtime.InteropServices;

internal partial class Interop
{
internal partial class mincore
{
[StructLayoutAttribute(LayoutKind.Sequential)]
internal struct CONSOLE_CURSOR_INFO
{
internal int dwSize;
internal bool bVisible;
}

[DllImport(Libraries.Console_L2, SetLastError = true)]
internal static extern bool GetConsoleCursorInfo(IntPtr hConsoleOutput, out CONSOLE_CURSOR_INFO cci);

[DllImport(Libraries.Console_L2, SetLastError = true)]
internal static extern bool SetConsoleCursorInfo(IntPtr hConsoleOutput, ref CONSOLE_CURSOR_INFO cci);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Runtime.InteropServices;

internal partial class Interop
{
internal partial class mincore
{
[DllImport(Libraries.Console_L2, SetLastError = true)]
internal static extern Interop.mincore.COORD GetLargestConsoleWindowSize(IntPtr hConsoleOutput);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Runtime.InteropServices;

internal partial class Interop
{
internal partial class mincore
{
[DllImport(Libraries.Console_L2, SetLastError = true)]
internal static extern bool SetConsoleScreenBufferSize(IntPtr hConsoleOutput, Interop.mincore.COORD size);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Runtime.InteropServices;

internal partial class Interop
{
internal partial class mincore
{
[DllImport(Libraries.Console_L2, SetLastError = true)]
internal static unsafe extern bool SetConsoleWindowInfo(IntPtr hConsoleOutput, bool absolute, SMALL_RECT* consoleWindow);
}
}
29 changes: 29 additions & 0 deletions src/Common/tests/System/IO/InterceptStreamWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.IO;
using System.Text;

internal sealed class InterceptStreamWriter : StreamWriter
{
private readonly StreamWriter _wrappedWriter;

public InterceptStreamWriter(Stream baseStream, StreamWriter wrappedWriter, Encoding encoding, int bufferSize, bool leaveOpen) :
base(baseStream, encoding, bufferSize, leaveOpen)
{
_wrappedWriter = wrappedWriter;
}

public override void Write(string value)
{
base.Write(value);
_wrappedWriter.Write(value);
}

public override void Write(char value)
{
base.Write(value);
_wrappedWriter.Write(value);
}
}
2 changes: 2 additions & 0 deletions src/Native/Common/pal_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#cmakedefine01 HAVE_POSIX_ADVISE
#cmakedefine01 PRIORITY_REQUIRES_INT_WHO
#cmakedefine01 HAVE_IN6_U
#cmakedefine01 HAVE_IOCTL
#cmakedefine01 HAVE_TIOCGWINSZ

// Mac OS X has stat64, but it is deprecated since plain stat now
// provides the same 64-bit aware struct when targeting OS X > 10.5
Expand Down
20 changes: 20 additions & 0 deletions src/Native/System.Native/pal_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <syslog.h>
#include <unistd.h>

Expand Down Expand Up @@ -793,3 +794,22 @@ extern "C" int32_t Write(int32_t fd, const void* buffer, int32_t bufferSize)
assert(count >= -1 && count <= bufferSize);
return static_cast<int32_t>(count);
}

extern "C" int32_t GetWindowSize(WinSize* windowSize)
{
assert(windowSize != nullptr);

#if HAVE_IOCTL && HAVE_TIOCGWINSZ
int error = ioctl(STDOUT_FILENO, TIOCGWINSZ, windowSize);

if (error != 0)
{
*windowSize = {}; // managed out param must be initialized
}

return error;
#else
errno = ENOTSUP;
return -1;
#endif
}
19 changes: 19 additions & 0 deletions src/Native/System.Native/pal_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ struct FileStatus
int64_t BirthTime; // time the file was created
};

/*
* Window Size of the terminal
*/
struct WinSize
{
uint16_t Row;
uint16_t Col;
uint16_t XPixel;
uint16_t YPixel;
};

/************
* The values below in the header are fixed and correct for managed callers to use forever.
* We must never change them. The implementation must either static_assert that they are equal
Expand Down Expand Up @@ -610,3 +621,11 @@ extern "C" void Sync();
* Returns the number of bytes written on success; otherwise, returns -1 and sets errno
*/
extern "C" int32_t Write(int32_t fd, const void* buffer, int32_t bufferSize);


/**
* Gets the windows size of the terminal
*
* Returns 0 on success; otherwise, returns errorNo.
*/
extern "C" int32_t GetWindowSize(WinSize* windowsSize);
10 changes: 10 additions & 0 deletions src/Native/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include(CheckStructHasMember)
include(CheckCXXSourceCompiles)
include(CheckCXXSourceRuns)
include(CheckPrototypeDefinition)
include(CheckSymbolExists)

#CMake does not include /usr/local/include into the include search path
#thus add it manually. This is required on FreeBSD.
Expand Down Expand Up @@ -32,6 +33,15 @@ check_function_exists(
posix_fadvise
HAVE_POSIX_ADVISE)

check_function_exists(
ioctl
HAVE_IOCTL)

check_symbol_exists(
TIOCGWINSZ
"sys/ioctl.h"
HAVE_TIOCGWINSZ)

check_struct_has_member(
"struct stat"
st_birthtime
Expand Down
2 changes: 2 additions & 0 deletions src/System.Console/ref/System.Console.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace System
public static partial class Console
{
public static System.ConsoleColor BackgroundColor { get { return default(System.ConsoleColor); } set { } }
public static bool CursorVisible { get { return default(bool); } set { } }
public static System.IO.TextWriter Error { get { return default(System.IO.TextWriter); } }
public static System.ConsoleColor ForegroundColor { get { return default(System.ConsoleColor); } set { } }
public static bool IsInputRedirected { get { return false; } }
Expand All @@ -27,6 +28,7 @@ public static void ResetColor() { }
public static void SetError(System.IO.TextWriter newError) { }
public static void SetIn(System.IO.TextReader newIn) { }
public static void SetOut(System.IO.TextWriter newOut) { }
public static int WindowWidth { get { return default(int); } set { } }
public static void Write(bool value) { }
public static void Write(char value) { }
public static void Write(char[] buffer) { }
Expand Down
9 changes: 9 additions & 0 deletions src/System.Console/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,18 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="ArgumentOutOfRange_ConsoleWindowBufferSize" xml:space="preserve">
<value>The value must be less than the console's current maximum window size of {0} in that dimension. Note that this value depends on screen resolution and the console font.</value>
</data>
<data name="ArgumentOutOfRange_ConsoleWindowSize_Size" xml:space="preserve">
<value>The new console window size would force the console buffer size to be too large.</value>
</data>
<data name="ArgumentOutOfRange_NeedNonNegNum" xml:space="preserve">
<value>Non-negative number required.</value>
</data>
<data name="ArgumentOutOfRange_NeedPosNum" xml:space="preserve">
<value>Positive number required.</value>
</data>
<data name="ArgumentNull_Buffer" xml:space="preserve">
<value>Buffer cannot be null.</value>
</data>
Expand Down
17 changes: 16 additions & 1 deletion src/System.Console/src/System.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.FormatMessage.cs">
<Link>Common\Interop\Windows\Interop.FormatMessage.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.ConsoleCursorInfo.cs">
<Link>Common\Interop\Windows\Interop.ConsoleCursorInfo.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.ConsoleScreenBufferInfo.cs">
<Link>Common\Interop\Windows\Interop.ConsoleScreenBufferInfo.cs</Link>
Expand All @@ -62,6 +65,9 @@
<Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.GetConsoleOutputCP.cs">
<Link>Common\Interop\Windows\Interop.GetConsoleOutputCP.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.GetLargestConsoleWindowSize.cs">
<Link>Common\Interop\Windows\Interop.GetLargestConsoleWindowSize.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.GetFileType_IntPtr.cs">
<Link>Common\Interop\Windows\Interop.GetFileType.cs</Link>
</Compile>
Expand All @@ -77,12 +83,18 @@
<Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.ReadConsole.cs">
<Link>Common\Interop\Windows\Interop.ReadConsole.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.SetConsoleCtrlHandler.cs">
<Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.SetConsoleCtrlHandler.cs">
<Link>Common\Interop\Windows\Interop.SetConsoleCtrlHandler.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.SetConsoleScreenBufferSize.cs">
<Link>Common\Interop\Windows\Interop.SetConsoleScreenBufferSize.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.SetConsoleTextAttribute.cs">
<Link>Common\Interop\Windows\Interop.SetConsoleTextAttribute.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.SetConsoleWindowInfo.cs">
<Link>Common\Interop\Windows\Interop.SetConsoleWindowInfo.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.WriteFile_IntPtr.cs">
<Link>Common\Interop\Windows\Interop.WriteFile.cs</Link>
</Compile>
Expand Down Expand Up @@ -161,6 +173,9 @@
<Compile Include="$(CommonPath)\Interop\Unix\libcoreclr\Interop.SetConsoleCtrlHandler.cs">
<Link>Common\Interop\Unix\Interop.SetConsoleCtrlHandler.cs"</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.GetWindowWidth.cs">
<Link>Common\Interop\Unix\Interop.GetWindowWidth.cs"</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="project.json" />
Expand Down
24 changes: 24 additions & 0 deletions src/System.Console/src/System/Console.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,30 @@ public static void ResetColor()
ConsolePal.ResetColor();
}

public static int WindowWidth
{
get
{
return ConsolePal.WindowWidth;
}
set
{
ConsolePal.WindowWidth = value;
}
}

public static bool CursorVisible
{
get
{
return ConsolePal.CursorVisible;
}
set
{
ConsolePal.CursorVisible = value;
}
}

public static event ConsoleCancelEventHandler CancelKeyPress
{
add
Expand Down
Loading

0 comments on commit 1cf8578

Please sign in to comment.