From 42b66cc8afa83446a941029a5b81403b65b584d5 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Wed, 25 Oct 2023 11:55:04 -0400 Subject: [PATCH] reformatted playground and added averageWithArgs --- averageWithArgs/averageWithArgs.go | 10 +++ averageWithArgs/go.mod | 3 + playground/LICENSE | 9 +++ playground/README.md | 10 +++ playground/averageHeadFirstSolution.go | 24 ++++++ playground/averageMySolution.go | 37 +++++++++ playground/double.go | 13 +++ playground/error.go | 10 +++ playground/go.mod | 34 ++++++++ playground/go.sum | 100 +++++++++++++++++++++++ playground/guess.go | 47 +++++++++++ playground/myIntPointer.go | 12 +++ playground/passfail.go | 25 ++++++ playground/playground.go | 56 +++++++++++++ playground/shopping.go | 107 +++++++++++++++++++++++++ playground/slices.go | 41 ++++++++++ playground/test.go | 86 ++++++++++++++++++++ playground/toCelsius.go | 18 +++++ playground/wallArea.go | 44 ++++++++++ 19 files changed, 686 insertions(+) create mode 100644 averageWithArgs/averageWithArgs.go create mode 100644 averageWithArgs/go.mod create mode 100644 playground/LICENSE create mode 100644 playground/README.md create mode 100644 playground/averageHeadFirstSolution.go create mode 100644 playground/averageMySolution.go create mode 100644 playground/double.go create mode 100644 playground/error.go create mode 100644 playground/go.mod create mode 100644 playground/go.sum create mode 100644 playground/guess.go create mode 100644 playground/myIntPointer.go create mode 100644 playground/passfail.go create mode 100644 playground/playground.go create mode 100644 playground/shopping.go create mode 100644 playground/slices.go create mode 100644 playground/test.go create mode 100644 playground/toCelsius.go create mode 100644 playground/wallArea.go diff --git a/averageWithArgs/averageWithArgs.go b/averageWithArgs/averageWithArgs.go new file mode 100644 index 0000000..df8079c --- /dev/null +++ b/averageWithArgs/averageWithArgs.go @@ -0,0 +1,10 @@ +package main + +import ( + "fmt" + "os" +) + +func average2() { + fmt.Println(os.Args) +} diff --git a/averageWithArgs/go.mod b/averageWithArgs/go.mod new file mode 100644 index 0000000..aabddcc --- /dev/null +++ b/averageWithArgs/go.mod @@ -0,0 +1,3 @@ +module averageWithArgs + +go 1.21.3 diff --git a/playground/LICENSE b/playground/LICENSE new file mode 100644 index 0000000..83dc40b --- /dev/null +++ b/playground/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2023 john-okeefe + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/playground/README.md b/playground/README.md new file mode 100644 index 0000000..7b6fd22 --- /dev/null +++ b/playground/README.md @@ -0,0 +1,10 @@ +# My Personal Go Playground + +Playing with Golang mostly using the Head First Go book. + +Entry point is playground.go. + +To run use +```sh +go run trying_out_go +``` diff --git a/playground/averageHeadFirstSolution.go b/playground/averageHeadFirstSolution.go new file mode 100644 index 0000000..4db9977 --- /dev/null +++ b/playground/averageHeadFirstSolution.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "strings" + + "git.linuxhg.com/john-okeefe/datafile" + "git.linuxhg.com/john-okeefe/keyboard" +) + +func averageHeadFirstSolution() { + fmt.Printf("What is the name of the file: ") + fileString, err := keyboard.GetString() + Error(err) + fileString = strings.TrimSpace(fileString) + numbers, err := datafile.GetFloats(fileString) + Error(err) + var sum float64 = 0 + for _, number := range numbers { + sum += number + } + sampleCount := float64(len(numbers)) + fmt.Printf("Average: %0.2f\n", sum/sampleCount) +} diff --git a/playground/averageMySolution.go b/playground/averageMySolution.go new file mode 100644 index 0000000..506dc47 --- /dev/null +++ b/playground/averageMySolution.go @@ -0,0 +1,37 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "strconv" + "strings" + + "git.linuxhg.com/john-okeefe/keyboard" +) + +func averageMySolution() { + fmt.Printf("What is the name of the file: ") + fileString, err := keyboard.GetString() + Error(err) + fileString = strings.TrimSpace(fileString) + file, err := os.Open(fileString) + Error(err) + var sum float64 = 0 + scanner := bufio.NewScanner(file) + counter := 0 + for scanner.Scan() { + number, err := strconv.ParseFloat(scanner.Text(), 64) + Error(err) + sum += number + counter++ + } + err = file.Close() + Error(err) + if scanner.Err() != nil { + log.Fatal(scanner.Err()) + } + sampleCount := float64(counter) + fmt.Printf("Average: %0.2f\n", sum/sampleCount) +} diff --git a/playground/double.go b/playground/double.go new file mode 100644 index 0000000..c7b2fcb --- /dev/null +++ b/playground/double.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +func makeDouble(number *int) { + *number *= 2 +} + +func double() { + amount := 6 + makeDouble(&amount) + fmt.Println(amount) +} diff --git a/playground/error.go b/playground/error.go new file mode 100644 index 0000000..a54a5fa --- /dev/null +++ b/playground/error.go @@ -0,0 +1,10 @@ +package main + +import "log" + +func Error(err error) { + if err != nil { + log.Fatal(err) + } + return +} diff --git a/playground/go.mod b/playground/go.mod new file mode 100644 index 0000000..e1f73c8 --- /dev/null +++ b/playground/go.mod @@ -0,0 +1,34 @@ +module trying_out_go + +go 1.21.3 + +require ( + git.linuxhg.com/john-okeefe/keyboard v0.0.0-20231016161119-714c103574cc + github.com/charmbracelet/bubbles v0.16.1 + github.com/charmbracelet/bubbletea v0.24.2 + github.com/charmbracelet/lipgloss v0.9.1 + github.com/dixonwille/wmenu/v5 v5.1.0 +) + +require ( + git.linuxhg.com/john-okeefe/datafile v0.0.0-20231025144217-70a69b064e81 // indirect + github.com/atotto/clipboard v0.1.4 // indirect + github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect + github.com/daviddengcn/go-colortext v1.0.0 // indirect + github.com/dixonwille/wlog/v3 v3.0.1 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + github.com/mattn/go-localereader v0.0.1 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect + github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect + github.com/muesli/cancelreader v0.2.2 // indirect + github.com/muesli/reflow v0.3.0 // indirect + github.com/muesli/termenv v0.15.2 // indirect + github.com/rivo/uniseg v0.4.4 // indirect + github.com/sahilm/fuzzy v0.1.0 // indirect + golang.org/x/sync v0.4.0 // indirect + golang.org/x/sys v0.13.0 // indirect + golang.org/x/term v0.13.0 // indirect + golang.org/x/text v0.13.0 // indirect +) diff --git a/playground/go.sum b/playground/go.sum new file mode 100644 index 0000000..bd4d9d7 --- /dev/null +++ b/playground/go.sum @@ -0,0 +1,100 @@ +git.linuxhg.com/john-okeefe/datafile v0.0.0-20231019003321-0d46a9c14627 h1:CssED3WO+ie9309BqxqHWPzU2dJiVgn/M6m2Xi9B0mg= +git.linuxhg.com/john-okeefe/datafile v0.0.0-20231019003321-0d46a9c14627/go.mod h1:HexHt1UMGUDMV5bhB81nAKdvGaCnxKA2upRd7cV8laI= +git.linuxhg.com/john-okeefe/datafile v0.0.0-20231025144217-70a69b064e81 h1:M19j4vHPgap0R7r3Lc0C5QZIjBFixkwuvRnUGDIC9+o= +git.linuxhg.com/john-okeefe/datafile v0.0.0-20231025144217-70a69b064e81/go.mod h1:HexHt1UMGUDMV5bhB81nAKdvGaCnxKA2upRd7cV8laI= +git.linuxhg.com/john-okeefe/keyboard v0.0.0-20231016005355-05ed8b28800e h1:oCGqHZUhesu14KQUELz0jxIPnL/KcGFhsmNnKoAZMo4= +git.linuxhg.com/john-okeefe/keyboard v0.0.0-20231016005355-05ed8b28800e/go.mod h1:zxW/S0TzdzmyfloJ0ZHwMaTjMgfd8hK8TycSEAyMRkY= +git.linuxhg.com/john-okeefe/keyboard v0.0.0-20231016161119-714c103574cc h1:J5VWew9I6gCCYVxQ/5NEtsD4wcMZcc4e05V44OiSNi4= +git.linuxhg.com/john-okeefe/keyboard v0.0.0-20231016161119-714c103574cc/go.mod h1:zxW/S0TzdzmyfloJ0ZHwMaTjMgfd8hK8TycSEAyMRkY= +github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= +github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= +github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= +github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/charmbracelet/bubbles v0.16.1 h1:6uzpAAaT9ZqKssntbvZMlksWHruQLNxg49H5WdeuYSY= +github.com/charmbracelet/bubbles v0.16.1/go.mod h1:2QCp9LFlEsBQMvIYERr7Ww2H2bA7xen1idUDIzm/+Xc= +github.com/charmbracelet/bubbletea v0.24.2 h1:uaQIKx9Ai6Gdh5zpTbGiWpytMU+CfsPp06RaW2cx/SY= +github.com/charmbracelet/bubbletea v0.24.2/go.mod h1:XdrNrV4J8GiyshTtx3DNuYkR1FDaJmO3l2nejekbsgg= +github.com/charmbracelet/lipgloss v0.8.0 h1:IS00fk4XAHcf8uZKc3eHeMUTCxUH6NkaTrdyCQk84RU= +github.com/charmbracelet/lipgloss v0.8.0/go.mod h1:p4eYUZZJ/0oXTuCQKFF8mqyKCz0ja6y+7DniDDw5KKU= +github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg= +github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I= +github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY= +github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/daviddengcn/go-colortext v0.0.0-20180409174941-186a3d44e920 h1:d/cVoZOrJPJHKH1NdeUjyVAWKp4OpOT+Q+6T1sH7jeU= +github.com/daviddengcn/go-colortext v0.0.0-20180409174941-186a3d44e920/go.mod h1:dv4zxwHi5C/8AeI+4gX4dCWOIvNi7I6JCSX0HvlKPgE= +github.com/daviddengcn/go-colortext v1.0.0 h1:ANqDyC0ys6qCSvuEK7l3g5RaehL/Xck9EX8ATG8oKsE= +github.com/daviddengcn/go-colortext v1.0.0/go.mod h1:zDqEI5NVUop5QPpVJUxE9UO10hRnmkD5G4Pmri9+m4c= +github.com/dixonwille/wlog/v3 v3.0.1 h1:ViTSsNNndHlKW5S89x5O0KSTpTT9zdPqrkA/TZYY8+s= +github.com/dixonwille/wlog/v3 v3.0.1/go.mod h1:fPYZR9Ne5gFh3N8b3CuXVWHWxkY6Yg1wCeS3Km6Nc0I= +github.com/dixonwille/wmenu/v5 v5.1.0 h1:sKBHDoQ945NRvK0Eitd0kHDYHl1IYOSr1sdCK9c+Qr0= +github.com/dixonwille/wmenu/v5 v5.1.0/go.mod h1:l6EGfXHaN6DPgv5+V5RY+cydAXJsj/oDZVczcdukPzc= +github.com/golangplus/bytes v0.0.0-20160111154220-45c989fe5450/go.mod h1:Bk6SMAONeMXrxql8uvOKuAZSu8aM5RUGv+1C6IJaEho= +github.com/golangplus/bytes v1.0.0/go.mod h1:AdRaCFwmc/00ZzELMWb01soso6W1R/++O1XL80yAn+A= +github.com/golangplus/fmt v0.0.0-20150411045040-2a5d6d7d2995/go.mod h1:lJgMEyOkYFkPcDKwRXegd+iM6E7matEszMG5HhwytU8= +github.com/golangplus/fmt v1.0.0/go.mod h1:zpM0OfbMCjPtd2qkTD/jX2MgiFCqklhSUFyDW44gVQE= +github.com/golangplus/testing v0.0.0-20180327235837-af21d9c3145e h1:KhcknUwkWHKZPbFy2P7jH5LKJ3La+0ZeknkkmrSgqb0= +github.com/golangplus/testing v0.0.0-20180327235837-af21d9c3145e/go.mod h1:0AA//k/eakGydO4jKRoRL2j92ZKSzTgj9tclaCrvXHk= +github.com/golangplus/testing v1.0.0 h1:+ZeeiKZENNOMkTTELoSySazi+XaEhVO0mb+eanrSEUQ= +github.com/golangplus/testing v1.0.0/go.mod h1:ZDreixUV3YzhoVraIDyOzHrr76p6NUh6k/pPg/Q3gYA= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= +github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= +github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= +github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= +github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= +github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= +github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34= +github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= +github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= +github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= +github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= +github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= +github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= +github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= +github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI= +github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= +golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= +golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw= +golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= +golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= +golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= +golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= +golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/playground/guess.go b/playground/guess.go new file mode 100644 index 0000000..9589bad --- /dev/null +++ b/playground/guess.go @@ -0,0 +1,47 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "math/rand" + "os" + "strconv" + "strings" +) + +func guess() { + numberToGuess := rand.Intn(100) + 1 + + for i := 10; i > 0; i-- { + + fmt.Println("Please guess a number from 1 to 100") + fmt.Printf("You have %v guesses left.\n", i) + reader := bufio.NewReader(os.Stdin) + input, err := reader.ReadString('\n') + if err != nil { + log.Fatal(err) + } + input = strings.TrimSpace(input) + guess, err := strconv.Atoi(input) + if err != nil { + log.Fatal(err) + } + if guess == numberToGuess { + fmt.Println("Great job! You guessed it!") + os.Exit(0) + } + if guess > numberToGuess { + fmt.Println("Oops. Your guess was HIGH.") + continue + } + if guess < numberToGuess { + fmt.Println("Oops. Your guess was LOW.") + continue + } + if i == 1 { + fmt.Printf("Sorry. You didn't guess my number. It was: %v\n", numberToGuess) + os.Exit(0) + } + } +} diff --git a/playground/myIntPointer.go b/playground/myIntPointer.go new file mode 100644 index 0000000..4f74737 --- /dev/null +++ b/playground/myIntPointer.go @@ -0,0 +1,12 @@ +package main + +import "fmt" + +func myIntPointer() { + var myInt int + var myIntPointer *int + myInt = 42 + myIntPointer = &myInt + + fmt.Println(*myIntPointer) +} diff --git a/playground/passfail.go b/playground/passfail.go new file mode 100644 index 0000000..072526c --- /dev/null +++ b/playground/passfail.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "log" + + "git.linuxhg.com/john-okeefe/keyboard" +) + +func passFail() { + fmt.Print("Enter a grade: ") + grade, err := keyboard.GetFloat() + if err != nil { + log.Fatal(err) + } + var status string + + if grade >= 60 { + status = "passing" + } else { + status = "failing" + } + + fmt.Println("A grade of", grade, "is", status) +} diff --git a/playground/playground.go b/playground/playground.go new file mode 100644 index 0000000..3cb6a54 --- /dev/null +++ b/playground/playground.go @@ -0,0 +1,56 @@ +package main + +import ( + "fmt" + "log" + + "github.com/dixonwille/wmenu/v5" +) + +func main() { + menu := wmenu.NewMenu("Choose a program.") + menu.Action(func(opts []wmenu.Opt) error { + fmt.Printf("You chose " + opts[0].Text + ". Launching...\n") + switch opts[0].Text { + case "Pass or Fail": + passFail() + case "Guessing Game": + guess() + case "Shopping List": + shopping() + case "Commandline Test": + terminalTest() + case "Wall Area": + wallArea() + case "myIntPointer": + myIntPointer() + case "Double": + double() + case "Convert To Celsius": + toCelsius() + case "Get the Average My Solution": + averageMySolution() + case "Get the Average Head First Solution": + averageHeadFirstSolution() + case "Slices": + slices() + } + return nil + }) + menu.PadOptionID() + menu.Option("Pass or Fail", nil, false, nil) + menu.Option("Guessing Game", nil, false, nil) + menu.Option("Shopping List", nil, false, nil) + menu.Option("Commandline Test", nil, false, nil) + menu.Option("Wall Area", nil, false, nil) + menu.Option("myIntPointer", nil, false, nil) + menu.Option("Double", nil, false, nil) + menu.Option("Convert To Celsius", nil, false, nil) + menu.Option("Get the Average My Solution", nil, false, nil) + menu.Option("Get the Average Head First Solution", nil, false, nil) + menu.Option("Slices", nil, false, nil) + err := menu.Run() + if err != nil { + log.Fatal(err) + } +} diff --git a/playground/shopping.go b/playground/shopping.go new file mode 100644 index 0000000..f000be7 --- /dev/null +++ b/playground/shopping.go @@ -0,0 +1,107 @@ +package main + +import ( + "fmt" + "os" + + tea "github.com/charmbracelet/bubbletea" +) + +type shoppingModel struct { + choices []string // items on the shopping list + cursor int // which shopping list item our cursor is pointing at + selected map[int]struct{} // which shopping items are selected +} + +func initialModel() shoppingModel { + return shoppingModel{ + // Our shopping list is a grocery list + choices: []string{"Buy Carrots", "Buy Celery", "Buy Kohlrabi"}, + + // A map which indicates which choices are selected. We're using + // the map like a mathematical set. The keys refer to the indexes + // of the `choices` slice, above. + selected: make(map[int]struct{}), + } +} + +func (m shoppingModel) Init() tea.Cmd { + // Just return `nil`, which means "no I/O right now, please." + return nil +} + +func (m shoppingModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + // Is it a key press? + case tea.KeyMsg: + // Cool, what was the actual key pressed? + switch msg.String() { + case "ctrl+c", "q": + return m, tea.Quit + + // The "up" and "k" keys move the cursor up + case "up", "k": + if m.cursor > 0 { + m.cursor-- + } + + // The "down" and "j" keys move the cursor down + case "down", "j": + if m.cursor < len(m.choices)-1 { + m.cursor++ + } + + // The "enter" key and the spacebar (a literal space) toggle + // the selected state for the item that the cursor is pointing at. + case "enter", " ": + _, ok := m.selected[m.cursor] + if ok { + delete(m.selected, m.cursor) + } else { + m.selected[m.cursor] = struct{}{} + } + } + } + + // Return the updated shoppingModel to the Bubble Tea runtime for processing. + // Note that we're not returning a command. + return m, nil +} + +func (m shoppingModel) View() string { + // The header + s := "What should we buy at the market? \n\n" + + // Iterate over our choices + for i, choice := range m.choices { + + // Is the cursor pointing at this choice? + cursor := " " // no cursor + if m.cursor == i { + cursor = ">" // cursor! + } + + // Is this choice selected? + checked := " " // not selected + if _, ok := m.selected[i]; ok { + checked = "x" // selected! + } + + // Render the row + s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice) + } + + // The footer + s += "\nPress q to quit.\n" + + // Send the UI for rendering + return s +} + +func shopping() { + p := tea.NewProgram(initialModel()) + if _, err := p.Run(); err != nil { + fmt.Printf("Alas, there's been an error: %v", err) + os.Exit(1) + } +} diff --git a/playground/slices.go b/playground/slices.go new file mode 100644 index 0000000..b86678a --- /dev/null +++ b/playground/slices.go @@ -0,0 +1,41 @@ +package main + +import "fmt" + +func slices() { + var notes []string + notes = make([]string, 7) + + notes[0] = "do" + notes[1] = "re" + notes[2] = "mi" + + fmt.Println(notes[0]) + fmt.Println(notes[1]) + fmt.Println(len(notes)) + + primes := make([]int, 5) + primes[0] = 2 + primes[1] = 3 + primes[2] = 5 + primes[3] = 7 + primes[4] = 11 + fmt.Println(primes[0]) + fmt.Println(primes[1]) + fmt.Println(primes[2]) + fmt.Println(primes[3]) + fmt.Println(primes[4]) + fmt.Println(len(primes)) + + literalNotes := []string{"do", "re", "mi", "fa", "so", "la", "ti"} + fmt.Println(literalNotes[3], literalNotes[6], literalNotes[0]) + literalPrimes := []int{ + 2, + 3, + 5, + } + fmt.Println(literalPrimes[0], literalPrimes[1], literalPrimes[2]) + fmt.Println(literalPrimes, len(literalPrimes)) + literalPrimes = append(literalPrimes, 7, 11) + fmt.Println(literalPrimes, len(literalPrimes)) +} diff --git a/playground/test.go b/playground/test.go new file mode 100644 index 0000000..c88606c --- /dev/null +++ b/playground/test.go @@ -0,0 +1,86 @@ +package main + +import ( + "fmt" + "os" + + "github.com/charmbracelet/bubbles/list" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" +) + +var docStyle = lipgloss.NewStyle().Margin(1, 2) + +type item struct { + title, desc string +} + +func (i item) Title() string { return i.title } +func (i item) Description() string { return i.desc } +func (i item) FilterValue() string { return i.title } + +type testModel struct { + list list.Model +} + +func (m testModel) Init() tea.Cmd { + return nil +} + +func (m testModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case tea.KeyMsg: + if msg.String() == "ctrl+c" { + return m, tea.Quit + } + case tea.WindowSizeMsg: + h, v := docStyle.GetFrameSize() + m.list.SetSize(msg.Width-h, msg.Height-v) + } + + var cmd tea.Cmd + m.list, cmd = m.list.Update(msg) + return m, cmd +} + +func (m testModel) View() string { + return docStyle.Render(m.list.View()) +} + +func terminalTest() { + items := []list.Item{ + item{title: "Raspberry Pi’s", desc: "I have ’em all over my house"}, + item{title: "Nutella", desc: "It's good on toast"}, + item{title: "Bitter melon", desc: "It cools you down"}, + item{title: "Nice socks", desc: "And by that I mean socks without holes"}, + item{title: "Eight hours of sleep", desc: "I had this once"}, + item{title: "Cats", desc: "Usually"}, + item{title: "Plantasia, the album", desc: "My plants love it too"}, + item{title: "Pour over coffee", desc: "It takes forever to make though"}, + item{title: "VR", desc: "Virtual reality...what is there to say?"}, + item{title: "Noguchi Lamps", desc: "Such pleasing organic forms"}, + item{title: "Linux", desc: "Pretty much the best OS"}, + item{title: "Business school", desc: "Just kidding"}, + item{title: "Pottery", desc: "Wet clay is a great feeling"}, + item{title: "Shampoo", desc: "Nothing like clean hair"}, + item{title: "Table tennis", desc: "It’s surprisingly exhausting"}, + item{title: "Milk crates", desc: "Great for packing in your extra stuff"}, + item{title: "Afternoon tea", desc: "Especially the tea sandwich part"}, + item{title: "Stickers", desc: "The thicker the vinyl the better"}, + item{title: "20° Weather", desc: "Celsius, not Fahrenheit"}, + item{title: "Warm light", desc: "Like around 2700 Kelvin"}, + item{title: "The vernal equinox", desc: "The autumnal equinox is pretty good too"}, + item{title: "Gaffer’s tape", desc: "Basically sticky fabric"}, + item{title: "Terrycloth", desc: "In other words, towel fabric"}, + } + + m := testModel{list: list.New(items, list.NewDefaultDelegate(), 0, 0)} + m.list.Title = "My Fave Things" + + p := tea.NewProgram(m, tea.WithAltScreen()) + + if _, err := p.Run(); err != nil { + fmt.Println("Error running program:", err) + os.Exit(1) + } +} diff --git a/playground/toCelsius.go b/playground/toCelsius.go new file mode 100644 index 0000000..7f7e02b --- /dev/null +++ b/playground/toCelsius.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "log" + + "git.linuxhg.com/john-okeefe/keyboard" +) + +func toCelsius() { + fmt.Println("Enter a temperature in Fahrenheit: ") + fahrenheit, err := keyboard.GetFloat() + if err != nil { + log.Fatal(err) + } + celsius := (fahrenheit - 32) * 5 / 9 + fmt.Printf("%0.2f degrees Celsius\n", celsius) +} diff --git a/playground/wallArea.go b/playground/wallArea.go new file mode 100644 index 0000000..7aad9c2 --- /dev/null +++ b/playground/wallArea.go @@ -0,0 +1,44 @@ +package main + +import "fmt" + +func paintNeeded(width float64, height float64) (paintNeededCalculated float64, anError error) { + area := width * height + if width < 0.0 { + return 0, fmt.Errorf("a width of %0.2f is invalid.", width) + } + if height < 0.0 { + return 0, fmt.Errorf("a height of %0.2f is invalid.", height) + } + return area / 10.0, nil +} + +func printListersNeeded(amount float64) { + fmt.Printf("%.2f liters needed\n", amount) +} + +func wallArea() { + var amount, total float64 + amount, err := paintNeeded(4.2, 3.0) + if err != nil { + fmt.Println(err) + } else { + printListersNeeded(amount) + } + total += amount + amount, err = paintNeeded(-5.2, 3.5) + if err != nil { + fmt.Println(err) + } else { + printListersNeeded(amount) + } + total += amount + amount, err = paintNeeded(5.2, 5.0) + if err != nil { + fmt.Println(err) + } else { + printListersNeeded(amount) + } + total += amount + fmt.Printf("Total: %.2f liters\n", total) +}