From 6fb280d6519c248a888966d594cf0d660313413e Mon Sep 17 00:00:00 2001 From: Foil-hat-guy Date: Mon, 28 Apr 2025 22:57:43 +0300 Subject: [PATCH] First commit. --- LICENSE | 35 +++++++++++++++++++++++++++++++++++ README.md | 31 +++++++++++++++++++++++++++++++ go.mod | 3 +++ stty.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 go.mod create mode 100644 stty.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6ccdb6b --- /dev/null +++ b/LICENSE @@ -0,0 +1,35 @@ +3-Clause BSD NON-AI License + +Copyright 2025 The foilhatguy.casa domain name owner. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors +may be used to endorse or promote products derived from this software without +specific prior written permission. + +4. The source code and the binary form, and any modifications made to them may +not be used for the purpose of training or improving machine learning algorithms, +including but not limited to artificial intelligence, natural language processing, +or data mining. This condition applies to any derivatives, modifications, or +updates based on the Software code. Any usage of the source code or the binary +form in an AI-training dataset is considered a breach of this License. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED +OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..202e493 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +### The "UI" package + +This package provides --verbose and --quiet flag features to your cli. +It recognize both short and long flags. The package contains three functions: +- **PrintOut** to print regular messages unless --quiet flag is set. +- **PrintVerbose** to print messages only if --verbose flag is set, but --quiet +flag is not. +- **SetupOutputMode** to reconfigure output mode in the runtime. + +------------- + +### Usage +1. Download the package by **go get** command: +```shell +go get code.foilhatguy.casa/pub/ui +``` +2. Import package to your golang code: +```go +import ( + "code.foilhatguy.casa/pub/ui" +) +``` +There is no need for additional configuration. The package have its own **init** +function to detect --verbose and --quiet flags. + +3. Use **PrintOut** and **PrintVerbose** functions to print +messages to the standard output: +```go + ui.PrintOut("Some regular message.") + ui.PrintVerbose("Some message printed only if --verbose flag is set.") +``` \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..fbd413e --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module code.foilhatguy.casa/pub/stty + +go 1.19 diff --git a/stty.go b/stty.go new file mode 100644 index 0000000..5888f7c --- /dev/null +++ b/stty.go @@ -0,0 +1,55 @@ +package stty + +import ( + "fmt" + "os/exec" +) + +type stty struct { +} + +func Get() (*stty, error) { + checkSttyCMD := exec.Command("stty", "--version") + sttyOutput, err := checkSttyCMD.Output() + if err != nil || len(sttyOutput) == 0 { + return nil, fmt.Errorf("Stty is missing. Because: " + err.Error()) + } + return &stty{}, nil +} + +func (s *stty) SetBaudRate(portPath string, baudrate int) error { + setBaudRateCMD := exec.Command("stty", "-F", portPath, fmt.Sprint(baudrate)) + _, err := setBaudRateCMD.Output() + return err +} + +func (s *stty) CheckBaudRate(portPath string) (baudrate int, err error) { + checkBaudRateCMD := exec.Command("stty", "-F", portPath, "speed") + sttyOutput, err := checkBaudRateCMD.Output() + if err != nil { + return 0, fmt.Errorf("Failed to invoke stty. Because: " + err.Error()) + } + + _, err = fmt.Sscan(string(sttyOutput), &baudrate) + if err != nil { + return 0, fmt.Errorf("Failed to read baudrate from stty output. Because:" + err.Error()) + } + return baudrate, nil +} + +func (s *stty) TestBaudRate(portPath string, baudrate int) (bool, error) { + err := s.SetBaudRate(portPath, baudrate) + if err != nil { + return false, fmt.Errorf("Failed to set baudrate using stty. Because: " + err.Error()) + } + + newBaudRate, err := s.CheckBaudRate(portPath) + if err != nil { + return false, fmt.Errorf("Failed to check baudrate using stty. Because: " + err.Error()) + } + + if newBaudRate == baudrate { + return true, nil + } + return false, nil +}