Skip to content

Commit

Permalink
Merge pull request #5 from jceaser/develop
Browse files Browse the repository at this point in the history
Develop - major update
  • Loading branch information
jceaser committed May 4, 2023
2 parents 49158ad + 696e227 commit 5697e94
Show file tree
Hide file tree
Showing 20 changed files with 2,219 additions and 325 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*.go]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 4

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ db
db2
iif
isodate
md2html
roll
rpn
scale
striper

thru
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ A collection of command line tools written in Go

* console - get the size of a console
* counter - count up or down with big numbers
* date-math - find difference between two dates
* db - simple name/value storage
* db2 - complex data storage with forms ; in progress
* iif - inline if for scripting
* roll - rotate files like logs
* isodate - current date in iso format because date stinks
* md2html - template based HTML generator from markdown source
* roll - rotate files like logs ; in progress
* rpn - an RPN calculator
* scale - console line chart scroler
* scale - console line chart scroler ; experimental
* smatrix - like cmatrix but a little basic ; experimental
* striper - remove spaces from lines or words
* thru - Marshal-Unmarshal example

## Details ##

Expand Down
111 changes: 111 additions & 0 deletions color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package main

import (
"fmt"
"os"
"bufio"
"flag"
"strconv"
"strings"
)

type App_Data struct {
verbose bool
}

var app_data = App_Data{verbose:false,}

func AsColor(color [3]byte, text string) string {
return fmt.Sprintf("\033[38;2;%d;%d;%dm%s\033[00m",
color[0], color[1], color[2], text)
return text
}

func ExpandColorText(color [3]byte) string {
box := AsColor(color, "\uEE03\uEE04\uEE05")
red := color[0]
green := color[1]
blue := color[2]
return fmt.Sprintf("%s%00x%0x%0x", box, red, green, blue)
}

/******************************************************************************/
/* Application Tasks */

/* Make sure that a supplied value in the byte range of 0-255 */
func ColorLimits(value int) byte {
var ret byte

if value>255 {
ret = 255
} else if value < 0 {
ret = 0
} else {
ret = byte(value)
}
return ret
}

func ValueToNumber(reader *bufio.Reader, fallback byte) byte {
var ret byte
text, read_err := reader.ReadString('\n')
if read_err != nil {
fmt.Fprintf(os.Stderr, "ReadString: %s", read_err)
}
text = strings.Trim(text, "\t \n")
value, err := strconv.Atoi(text)
if err == nil {
ret = ColorLimits(value)
} else {
fmt.Fprintf(os.Stderr, "Bad value [%s] using fallback: %d", text,
fallback)
ret = fallback
}
return ret
}

func AskForNumbers() [3]byte {
reader := bufio.NewReader(os.Stdin)

fmt.Println ("Enter in a value between 0 and 255 and press enter:")

fmt.Print ("\nRed> ")
red := ValueToNumber(reader, 255)

fmt.Print ("\nGreen> ")
green := ValueToNumber(reader, 255)

fmt.Print ("\nBlue> ")
blue := ValueToNumber(reader, 255)
fmt.Println()

return [3]byte{red, green, blue}
}

func FindCompliment(color [3]byte) [3]byte{
color[0] = 255 - color[0]
color[1] = 255 - color[1]
color[2] = 255 - color[2]
return color
}

func ExampleLine(color [3]byte) string {
compliment := FindCompliment(color)
color_text := ExpandColorText(color)
compliment_text := ExpandColorText(compliment)

return fmt.Sprintf ("%s->%s\n", color_text, compliment_text)
}

/******************************************************************************/
/* Command Tasks */

func main() {

Check failure on line 103 in color.go

View workflow job for this annotation

GitHub Actions / Build

other declaration of main

Check failure on line 103 in color.go

View workflow job for this annotation

GitHub Actions / Build

other declaration of main

Check failure on line 103 in color.go

View workflow job for this annotation

GitHub Actions / Build

other declaration of main
verbose := flag.Bool("verbose", false, "verbose")
flag.Parse()

app_data.verbose = *verbose

color := AskForNumbers()
fmt.Printf(ExampleLine(color))
}
60 changes: 21 additions & 39 deletions console.go
Original file line number Diff line number Diff line change
@@ -1,84 +1,66 @@
// This command will report on the width and/or hight of the console

package main

import ("fmt"
/*"bufio"
"os"
"io"
"bytes"*/
import (
"fmt"
"flag"
"syscall"
"unsafe"
)

/****/
/******************************************************************************/
// MARK: Structures

type winsize struct {

Check failure on line 15 in console.go

View workflow job for this annotation

GitHub Actions / Build

other declaration of winsize

Check failure on line 15 in console.go

View workflow job for this annotation

GitHub Actions / Build

other declaration of winsize
Row uint16
Col uint16
Xpixel uint16
Ypixel uint16
}

const (
ESC_SAVE_SCREEN = "?47h"
ESC_RESTORE_SCREEN = "?47l"

ESC_SAVE_CURSOR = "s"
ESC_RESTORE_CURSOR = "u"

ESC_BOLD_ON = "1m"
ESC_BOLD_OFF = "0m"

ESC_CURSOR_ON = "?25h"
ESC_CURSOR_OFF = "?25l"

ESC_CLEAR_SCREEN = "2J"
ESC_CLEAR_LINE = "2K"
)
/******************************************************************************/
// MARK: - Functions

func getWidth() uint {
func GetWidth() uint {
ws := &winsize{}
retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
uintptr(syscall.Stdin),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))

if int(retCode) == -1 {
panic(errno)
}
return uint(ws.Col)
}

func getHeight() uint {
func GetHeight() uint {
ws := &winsize{}
retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
uintptr(syscall.Stdin),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))

if int(retCode) == -1 {
panic(errno)
}
return uint(ws.Row)
}


/****/

/******************************************************************************/
// MARK: - Application

func main() {

Check failure on line 52 in console.go

View workflow job for this annotation

GitHub Actions / Build

main redeclared in this block
//args := os.Args

bothMode := flag.Bool("both", true, "display both width and height")
heightMode := flag.Bool("height", false, "height mode")
widthMode := flag.Bool("width", false, "width mode")
heightMode := flag.Bool("height", false, "Height mode")
widthMode := flag.Bool("width", false, "Width mode")
adjust := flag.Int("adjust", 0, "Value to add to height or width")

flag.Parse()

if *bothMode {
fmt.Printf("%dx%d\n", getWidth(), getHeight())
} else if *heightMode {//only first and last
fmt.Printf("%d\n", getHeight())
if *heightMode {
fmt.Printf("%d\n", GetHeight() + uint(*adjust))
} else if *widthMode {
fmt.Printf("%d\n", getWidth())
fmt.Printf("%d\n", GetWidth() + uint(*adjust))
} else {
fmt.Printf("%dx%d\n", GetWidth(), GetHeight())
}
}
Loading

0 comments on commit 5697e94

Please sign in to comment.