First commit.

This commit is contained in:
Foil-hat-guy 2025-04-28 22:57:43 +03:00
commit 6fb280d651
No known key found for this signature in database
GPG key ID: 221CC305A7B23591
4 changed files with 124 additions and 0 deletions

35
LICENSE Normal file
View file

@ -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.

31
README.md Normal file
View file

@ -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.")
```

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module code.foilhatguy.casa/pub/stty
go 1.19

55
stty.go Normal file
View file

@ -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
}