Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.Console F# snippets #7539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// <Snippet1>
open System
open System.IO

let tabSize = 4
let usageText = "Usage: EXPANDTABSEX inputfile.txt outputfile.txt"

[<EntryPoint>]
let main args =
if args.Length < 2 then
printfn $"{usageText}"
else
try
use writer = new StreamWriter(args[1])
Console.SetOut writer
Console.SetIn(new StreamReader(args[0]))
let mutable i = Console.Read()
while i <> -1 do
let c = char i
if c = '\t' then
Console.WriteLine(("").PadRight(tabSize, ' '))
else
printf $"{c}"
i <- Console.Read()
// Recover the standard output stream so that a
// completion message can be displayed.
use standardOutput = new StreamWriter(Console.OpenStandardOutput())
standardOutput.AutoFlush <- true
Console.SetOut standardOutput
printfn $"EXPANDTABSEX has completed the processing of {args[0]}."
with :? IOException as e ->
let errorWriter = Console.Error
errorWriter.WriteLine e.Message
errorWriter.WriteLine usageText
0

// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="expandtabsex.fs" />
</ItemGroup>
</Project>
35 changes: 35 additions & 0 deletions samples/snippets/fsharp/VS_Snippets_CLR/console.beep/FS/beep.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//<snippet1>
// This example demonstrates the Console.Beep() method.
open System

[<EntryPoint>]
let main args =
if args.Length = 1 then
match Int32.TryParse args[0] with
| true, x when x >= 1 && x <= 9 ->
for i = 1 to x do
Console.WriteLine $"Beep number {i}."
Console.Beep()
| _ ->
Console.WriteLine "Usage: Enter the number of times (between 1 and 9) to beep."
else
Console.WriteLine "Usage: Enter the number of times (between 1 and 9) to beep."

0

// This example produces the following results:

// >beep
// Usage: Enter the number of times (between 1 and 9) to beep

// >beep 9
// Beep number 1.
// Beep number 2.
// Beep number 3.
// Beep number 4.
// Beep number 5.
// Beep number 6.
// Beep number 7.
// Beep number 8.
// Beep number 9.
//</snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="beep.fs" />
</ItemGroup>
</Project>
68 changes: 68 additions & 0 deletions samples/snippets/fsharp/VS_Snippets_CLR/console.beep2/FS/b2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//<snippet1>
// This example demonstrates the Console.Beep(Int32, Int32) method
open System
open System.Threading

// Define the frequencies of notes in an octave, as well as
// silence (rest).
type Tone =
| REST = 0
| GbelowC = 196
| A = 220
| Asharp = 233
| B = 247
| C = 262
| Csharp = 277
| D = 294
| Dsharp = 311
| E = 330
| F = 349
| Fsharp = 370
| G = 392
| Gsharp = 415

// Define the duration of a note in units of milliseconds.
type Duration =
| WHOLE = 1600
| HALF = 800
| QUARTER = 400
| EIGHTH = 200
| SIXTEENTH = 100

// Define a note as a frequency (tone) and the amount of
// time (duration) the note plays.
[<Struct>]
type Note =
{ Tone: Tone
Duration: Duration }

// Play the notes in a song.
let play tune =
for n in tune do
if n.Tone = Tone.REST then
Thread.Sleep(int n.Duration)
else
Console.Beep(int n.Tone, int n.Duration)

// Declare the first few notes of the song, "Mary Had A Little Lamb".
let mary =
[ { Tone = Tone.B; Duration = Duration.QUARTER }
{ Tone = Tone.A; Duration = Duration.QUARTER }
{ Tone = Tone.GbelowC; Duration = Duration.QUARTER }
{ Tone = Tone.A; Duration = Duration.QUARTER }
{ Tone = Tone.B; Duration = Duration.QUARTER }
{ Tone = Tone.B; Duration = Duration.QUARTER }
{ Tone = Tone.B; Duration = Duration.HALF }
{ Tone = Tone.A; Duration = Duration.QUARTER }
{ Tone = Tone.A; Duration = Duration.QUARTER }
{ Tone = Tone.A; Duration = Duration.HALF }
{ Tone = Tone.B; Duration = Duration.QUARTER }
{ Tone = Tone.D; Duration = Duration.QUARTER }
{ Tone = Tone.D; Duration = Duration.HALF } ]

// Play the song
play mary

// This example plays the first few notes of "Mary Had A Little Lamb"
// through the console speaker.
//</snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="b2.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="hw.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//<snippet1>
// This example demonstrates the Console.BufferHeight and
// Console.BufferWidth properties.
open System

printfn $"The current buffer height is {Console.BufferHeight} rows."
printfn $"The current buffer width is {Console.BufferWidth} columns."

// This example produces the following results:
//
// The current buffer height is 300 rows.
// The current buffer width is 85 columns.
//</snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// <Snippet1>
open System

let myHandler sender (args: ConsoleCancelEventArgs) =
printfn "\nThe read operation has been interrupted."

printfn $" Key pressed: {args.SpecialKey}"

printfn $" Cancel property: {args.Cancel}"

// Set the Cancel property to true to prevent the process from terminating.
printfn "Setting the Cancel property to true..."
args.Cancel <- true

// Announce the new value of the Cancel property.
printfn $" Cancel property: {args.Cancel}"
printfn "The read operation will resume...\n"

// Establish an event handler to process key press events.
Console.CancelKeyPress.AddHandler(ConsoleCancelEventHandler myHandler)

let mutable quit = false
while not quit do
printf "Press any key, or 'X' to quit, or "
printfn "CTRL+C to interrupt the read operation:"

// Start a console read operation. Do not display the input.
let cki = Console.ReadKey true

// Announce the name of the key that was pressed .
printfn $" Key pressed: {cki.Key}\n"

// Exit if the user pressed the 'X' key.
if cki.Key = ConsoleKey.X then
quit <- true

// The example displays output similar to the following:
// Press any key, or 'X' to quit, or CTRL+C to interrupt the read operation:
// Key pressed: J
//
// Press any key, or 'X' to quit, or CTRL+C to interrupt the read operation:
// Key pressed: Enter
//
// Press any key, or 'X' to quit, or CTRL+C to interrupt the read operation:
//
// The read operation has been interrupted.
// Key pressed: ControlC
// Cancel property: False
// Setting the Cancel property to true...
// Cancel property: True
// The read operation will resume...
//
// Key pressed: Q
//
// Press any key, or 'X' to quit, or CTRL+C to interrupt the read operation:
// Key pressed: X
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="ckp.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="lts.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//<snippet1>
// This example demonstrates the
// Console.CursorLeft and
// Console.CursorTop properties, and the
// Console.SetCursorPosition and
// Console.Clear methods.
open System

// Clear the screen, then save the top and left coordinates.
Console.Clear()
let origRow = Console.CursorTop
let origCol = Console.CursorLeft

let writeAt s x y =
try
Console.SetCursorPosition(origCol + x, origRow + y)
printfn $"%s{s}"
with :? ArgumentOutOfRangeException as e ->
Console.Clear()
printfn $"{e.Message}"

// Draw the left side of a 5x5 rectangle, from top to bottom.
writeAt "+" 0 0
writeAt "|" 0 1
writeAt "|" 0 2
writeAt "|" 0 3
writeAt "+" 0 4

// Draw the bottom side, from left to right.
writeAt "-" 1 4 // shortcut: writeAt "---", 1, 4)
writeAt "-" 2 4 // ...
writeAt "-" 3 4 // ...
writeAt "+" 4 4

// Draw the right side, from bottom to top.
writeAt "|" 4 3
writeAt "|" 4 2
writeAt "|" 4 1
writeAt "+" 4 0

// Draw the top side, from right to left.
writeAt "-" 3 0 // shortcut: writeAt "---", 1, 0)
writeAt "-" 2 0 // ...
writeAt "-" 1 0 // ...

writeAt "All done!" 0 6
printfn ""


// This example produces the following results:
//
// +---+
// | |
// | |
// | |
// +---+
//
// All done!
//</snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//<snippet1>
// This example demonstrates the Console.CursorSize property.
open System

let sizes = [ 1; 10; 20; 30; 40; 50; 60; 70; 80; 90; 100 ]

let saveCursorSize = Console.CursorSize
printfn "This example increments the cursor size from 1%% to 100%%:\n"

for size in sizes do
Console.CursorSize <- size
printfn $"Cursor size = {size}%%. (Press any key to continue...)"

Console.ReadKey() |> ignore

Console.CursorSize <- saveCursorSize

// This example produces the following results:

// This example increments the cursor size from 1% to 100%:

// Cursor size = 1%. (Press any key to continue...)
// Cursor size = 10%. (Press any key to continue...)
// Cursor size = 20%. (Press any key to continue...)
// Cursor size = 30%. (Press any key to continue...)
// Cursor size = 40%. (Press any key to continue...)
// Cursor size = 50%. (Press any key to continue...)
// Cursor size = 60%. (Press any key to continue...)
// Cursor size = 70%. (Press any key to continue...)
// Cursor size = 80%. (Press any key to continue...)
// Cursor size = 90%. (Press any key to continue...)
// Cursor size = 100%. (Press any key to continue...)
//</snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="csize.fs" />
</ItemGroup>
</Project>
Loading