Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite21a76f

Browse files
saulKevinRansom
authored andcommitted
Add colours to FSI output (dotnet#2156)
* Add colours to FSI output* Update test baselines (made whitespace consistent)
1 parentf8e579f commite21a76f

13 files changed

+1258
-943
lines changed

‎src/fsharp/CompileOptions.fs‎

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,27 +1347,34 @@ let GetGeneratedILModuleName (t:CompilerTarget) (s:string) =
13471347
letext=match twith| Dll->"dll"| Module->"netmodule"|_->"exe"
13481348
s+"."+ ext
13491349

1350-
13511350
letignoreFailureOnMono1_1_16 f=try f()with_->()
13521351

1353-
letDoWithErrorColor isError f=
1354-
ifnot enableConsoleColoringthen
1352+
letforeBackColor()=
1353+
try
1354+
letc= Console.ForegroundColor// may fail, perhaps on Mac, and maybe ForegroundColor is Black
1355+
letb= Console.BackgroundColor// may fail, perhaps on Mac, and maybe BackgroundColor is White
1356+
Some(c,b)
1357+
with
1358+
e-> None
1359+
1360+
letDoWithColor newColor f=
1361+
match enableConsoleColoring, foreBackColor()with
1362+
|false,_
1363+
|true, None->
1364+
// could not get console colours, so no attempt to change colours, can not set them back
13551365
f()
1356-
else
1357-
letforeBackColor=
1358-
try
1359-
letc= Console.ForegroundColor// may fail, perhaps on Mac, and maybe ForegroundColor is Black
1360-
letb= Console.BackgroundColor// may fail, perhaps on Mac, and maybe BackgroundColor is White
1361-
Some(c,b)
1362-
with
1363-
e-> None
1364-
match foreBackColorwith
1365-
| None-> f()(* could not get console colours, so no attempt to change colours, can not set them back*)
1366-
| Some(c,_)->
1367-
try
1368-
letwarnColor=if Console.BackgroundColor= ConsoleColor.Whitethen ConsoleColor.DarkBlueelse ConsoleColor.Cyan
1369-
leterrorColor= ConsoleColor.Red
1370-
ignoreFailureOnMono1_1_16(fun()-> Console.ForegroundColor<-(if isErrorthen errorColorelse warnColor))
1371-
f()
1372-
finally
1373-
ignoreFailureOnMono1_1_16(fun()-> Console.ForegroundColor<- c)
1366+
|true, Some(c,_)->
1367+
try
1368+
ignoreFailureOnMono1_1_16(fun()-> Console.ForegroundColor<- newColor)
1369+
f()
1370+
finally
1371+
ignoreFailureOnMono1_1_16(fun()-> Console.ForegroundColor<- c)
1372+
1373+
letDoWithErrorColor isError f=
1374+
match foreBackColor()with
1375+
| None-> f()
1376+
| Some(_, backColor)->
1377+
letwarnColor=if backColor= ConsoleColor.Whitethen ConsoleColor.DarkBlueelse ConsoleColor.Cyan
1378+
leterrorColor= ConsoleColor.Red
1379+
letcolor=if isErrorthen errorColorelse warnColor
1380+
DoWithColor color f

‎src/fsharp/CompileOptions.fsi‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ val NormalizeAssemblyRefs : TcImports -> (AbstractIL.IL.ILScopeRef -> AbstractIL
9292
// Miscellany
9393
val ignoreFailureOnMono1_1_16:(unit-> unit)-> unit
9494
val mutable enableConsoleColoring: bool
95-
val DoWithErrorColor: isError:bool->(unit-> 'a)-> 'a
95+
val DoWithColor: System.ConsoleColor->(unit-> 'a)-> 'a
96+
val DoWithErrorColor: bool->(unit-> 'a)-> 'a
9697
val ReportTime: TcConfig-> string-> unit
9798
val GetAbbrevFlagSet: TcConfigBuilder-> bool-> Set<string>
9899
val PostProcessCompilerArgs: string Set-> string[]-> string list

‎src/fsharp/fsi/fsi.fs‎

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ open Microsoft.FSharp.Compiler.Tastops
5555
openMicrosoft.FSharp.Compiler.TcGlobals
5656

5757
openInternal.Utilities.Collections
58+
openInternal.Utilities.StructuredFormat
5859

5960
typeFormatOptions= Internal.Utilities.StructuredFormat.FormatOptions
6061

@@ -148,6 +149,45 @@ module internal Utilities =
148149
letm=match typeArgswith[||]-> m|_-> m.MakeGenericMethod(typeArgs)
149150
m.Invoke(obj,[|v1;v2;v3|])|> unbox
150151

152+
letcolorPrintL(outWriter:TextWriter)opts layout=
153+
letrenderer=
154+
{new LayoutRenderer<NoResult,NoState>with
155+
memberr.Start()= NoState
156+
157+
memberr.AddText z s=
158+
letcolor=
159+
match swith
160+
| Keyword_-> ConsoleColor.Blue
161+
| TypeParameter_
162+
| Alias_
163+
| Class_-> ConsoleColor.Cyan
164+
| StringLiteral_-> ConsoleColor.Red
165+
| NumericLiteral_-> ConsoleColor.Magenta
166+
|_-> Console.ForegroundColor
167+
168+
DoWithColor color(fun()-> outWriter.Write s.Value)
169+
170+
z
171+
172+
memberr.AddBreak z n=
173+
outWriter.WriteLine()
174+
outWriter.Write(String.replicate n"")
175+
z
176+
177+
memberr.AddTag z(tag,attrs,start)= z
178+
179+
memberr.Finish z=
180+
outWriter.WriteLine()
181+
NoResult
182+
}
183+
184+
layout
185+
|> Internal.Utilities.StructuredFormat.Display.squash_layout opts
186+
|> Layout.renderL renderer
187+
|> ignore
188+
189+
outWriter.WriteLine()
190+
151191
letreferencedAssemblies= Dictionary<string, DateTime>()
152192

153193
#if FX_RESHAPED_REFLECTION
@@ -237,7 +277,7 @@ type public FsiEvaluationSessionHostConfig () =
237277

238278
/// Used to print value signatures along with their values, according to the current
239279
/// set of pretty printers installed in the system, and default printing rules.
240-
typeinternalFsiValuePrinter(fsi: FsiEvaluationSessionHostConfig,g: TcGlobals,generateDebugInfo,resolvePath,outWriter)=
280+
typeinternalFsiValuePrinter(fsi: FsiEvaluationSessionHostConfig,g: TcGlobals,generateDebugInfo,resolvePath,outWriter: TextWriter)=
241281

242282
/// This printer is used by F# Interactive if no other printers apply.
243283
letDefaultPrintingIntercept(ienv:Internal.Utilities.StructuredFormat.IEnvironment)(obj:obj)=
@@ -405,10 +445,8 @@ type internal FsiValuePrinter(fsi: FsiEvaluationSessionHostConfig, g: TcGlobals,
405445
NicePrint.layoutValOrMember denv vref(* the rhs was suppressed by the printer, so no value to print*)
406446
else
407447
(NicePrint.layoutValOrMember denv vref++ wordL(TaggedTextOps.tagText"="))--- rhsL.Value
408-
Internal.Utilities.StructuredFormat.Display.output_layout opts outWriter fullL;
409-
outWriter.WriteLine()
410-
411448

449+
Utilities.colorPrintL outWriter opts fullL
412450

413451
/// Used to make a copy of input in order to include the input when displaying the error text.
414452
typeinternalFsiStdinSyphon(errorWriter: TextWriter)=
@@ -450,10 +488,9 @@ type internal FsiStdinSyphon(errorWriter: TextWriter) =
450488
Utilities.ignoreAllErrors(fun()->
451489
letisError=true
452490
DoWithErrorColor isError(fun()->
453-
errorWriter.WriteLine();
454491
writeViaBufferWithEnvironmentNewLines errorWriter(OutputDiagnosticContext"" syphon.GetLine) err;
455492
writeViaBufferWithEnvironmentNewLines errorWriter(OutputDiagnostic(tcConfig.implicitIncludeDir,tcConfig.showFullPaths,tcConfig.flatErrors,tcConfig.errorStyle,isError)) err;
456-
errorWriter.WriteLine()
493+
errorWriter.WriteLine("\n")
457494
errorWriter.Flush()))
458495

459496

@@ -498,7 +535,7 @@ type internal ErrorLoggerThatStopsOnFirstError(tcConfigB:TcConfigBuilder, fsiStd
498535
fsiConsoleOutput.Error.WriteLine()
499536
writeViaBufferWithEnvironmentNewLines fsiConsoleOutput.Error(OutputDiagnosticContext"" fsiStdinSyphon.GetLine) err
500537
writeViaBufferWithEnvironmentNewLines fsiConsoleOutput.Error(OutputDiagnostic(tcConfigB.implicitIncludeDir,tcConfigB.showFullPaths,tcConfigB.flatErrors,tcConfigB.errorStyle,isError)) err
501-
fsiConsoleOutput.Error.WriteLine())
538+
fsiConsoleOutput.Error.WriteLine("\n"))
502539

503540
overridex.ErrorCount= errorCount
504541

@@ -1049,12 +1086,9 @@ type internal FsiDynamicCompiler
10491086

10501087
for(TImplFile(_qname,_,mexpr,_,_))in declaredImplsdo
10511088
letresponseL= NicePrint.layoutInferredSigOfModuleExprfalse denv infoReader AccessibleFromSomewhere rangeStdin mexpr
1052-
ifnot(Layout.isEmptyL responseL)then
1053-
fsiConsoleOutput.uprintfn"";
1089+
ifnot(Layout.isEmptyL responseL)then
10541090
letopts= valuePrinter.GetFsiPrintOptions()
1055-
letresponseL= Internal.Utilities.StructuredFormat.Display.squash_layout opts responseL
1056-
Layout.renderL(Layout.channelR outWriter) responseL|> ignore
1057-
fsiConsoleOutput.uprintfnn""
1091+
Utilities.colorPrintL outWriter opts responseL|> ignore
10581092

10591093
// Build the new incremental state.
10601094
letistate={istatewith optEnv= optEnv;
@@ -1741,7 +1775,7 @@ type internal FsiInteractionProcessor
17411775
initialInteractiveState)=
17421776

17431777
let mutablecurrState= initialInteractiveState
1744-
letevent= Event<unit>()
1778+
letevent=Control.Event<unit>()
17451779
letsetCurrState s= currState<- s; event.Trigger()
17461780
letrunCodeOnEventLoop errorLogger f istate=
17471781
try

‎tests/fsharp/core/load-script/out.stdout.bsl‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@ Test 3=================================================
2222
Hello
2323
World
2424
-the end
25-
2625
namespace FSI_0002
2726

28-
2927
namespace FSI_0002
3028

31-
3229
namespace FSI_0002
3330

3431
>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp