gozenity

package module
v0.0.0-...-23cb37e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2024 License: MIT Imports: 10 Imported by: 1

README

gozenity : Simple gozenity wrapper in go

Overview

GoDoc Code Climate Go Report Card Sourcegraph

gozenity lets you interact with gozenity from go.

Install

go get git.sr.ht/~jcmuller/gozenity

Examples

See godoc

Simple input
input, err := gozenity.Entry("Enter something:", "placeholder value")

if err != nil {
    log.Panic("Something happened: ", err)
}

fmt.Println(input)

Author

@jcmuller

License

MIT.

Documentation

Overview

Package gozenity is a simple wrapper for zenity) in Go.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Calendar

func Calendar(prompt string, defaultDate time.Time) (date string, err error)

Calendar picks a date

Example
package main

import (
	"fmt"
	"time"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	args := time.Now()
	entry, err := gozenity.Calendar("Please select 4/12/2018", args)

	if err != nil {
		panic(err)
	}

	fmt.Println(entry)
}
Output:

04/12/2018

func ColorSelection

func ColorSelection(prompt, initial string, showPalette bool) (color string, err error)

ColorSelection pops up a color selection dialog

Example
package main

import (
	"fmt"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	color, err := gozenity.ColorSelection("Choose green:", "green", false)

	if err != nil {
		panic(err)
	}

	fmt.Println(color)
}
Output:

rgb(0,128,0)
Example (Second)
package main

import (
	"fmt"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	color, err := gozenity.ColorSelection("Choose green:", "green", true)

	if err != nil {
		panic(err)
	}

	fmt.Println(color)
}
Output:

rgb(0,128,0)

func DirectorySelection

func DirectorySelection(prompt string) (files []string, err error)

DirectorySelection opens a file selector

Example
package main

import (
	"fmt"
	"strings"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	dirs, err := gozenity.DirectorySelection("Choose that guy")

	if err != nil {
		panic(err)
	}

	fmt.Println(strings.Join(dirs, " "))
}
Output:

dir1 dir2

func Entry

func Entry(prompt, placeholder string) (entry string, err error)

Entry asks for input

Example
package main

import (
	"fmt"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	entry, err := gozenity.Entry("Please type an answer (expecting 'Foo'):", "Placeholder")

	if err != nil {
		panic(err)
	}

	fmt.Println(entry)
}
Output:

Foo

func Error

func Error(prompt string) (err error)

Error errors errors

Example
package main

import (
	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	err := gozenity.Error("Something turrible happened :(.")
	if err != nil {
		panic(err)
	}
}
Output:

func FileSelection

func FileSelection(prompt string, filtersMap map[string][]string) (files []string, err error)

FileSelection opens a file selector

Example
package main

import (
	"fmt"
	"strings"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	files, err := gozenity.FileSelection("Choose that guy", nil)

	if err != nil {
		panic(err)
	}

	fmt.Println(strings.Join(files, " "))
}
Output:

file1 file2
Example (Second)
package main

import (
	"fmt"
	"strings"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	filters := map[string][]string{
		`Go files`:       {`*.go`},
		`Markdown files`: {`*.md`, `*.markdown`},
	}

	files, err := gozenity.FileSelection("Choose that guy", filters)

	if err != nil {
		panic(err)
	}

	fmt.Println(strings.Join(files, " "))
}
Output:

file1 file2

func Info

func Info(prompt string) (err error)

Info informs information

Example
package main

import (
	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	gozenity.Info("This thing happened.")
}
Output:

func List

func List(prompt string, options ...string) (selection string, err error)

List pops up the menu

Example
package main

import (
	"fmt"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {

	output, err := gozenity.List(
		"Choose an option:",
		"One word",
		"Two",
		"Three things",
	)

	if err != nil {
		if err, ok := err.(*gozenity.EmptySelectionError); !ok {
			fmt.Println(fmt.Errorf("Error getting output: %s", err))
		}
	}

	fmt.Println(output)
}
Output:

Two

func Notification

func Notification(prompt string) (err error)

Notification notifies notifiees

func Password

func Password(prompt string) (password string, err error)

Password asks for a password

Example
package main

import (
	"fmt"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	password, err := gozenity.Password("Enter password:")

	if err != nil {
		panic(err)
	}

	fmt.Println(password)
}
Output:

hunter2

func Progress

func Progress(prompt string, progress <-chan int) (err error)

Progress shows a pogress bar modal

Example
package main

import (
	"time"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	progress := make(chan int)

	go gozenity.Progress("We're doing that", progress)

	for i := 0; i <= 100; i++ {
		progress <- i
		time.Sleep(time.Millisecond * 50)
	}

}
Output:

func Question

func Question(prompt string) (answer bool, err error)

Question asks for answer

Example
package main

import (
	"fmt"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	answer, err := gozenity.Question("Who? Answer 'her'.")

	if err != nil {
		panic(err)
	}

	fmt.Println(answer)
}
Output:

true

func Scale

func Scale(prompt string, args *ScaleArgs) (answer int, err error)

Scale shows a nice scale

Example
package main

import (
	"fmt"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	val, err := gozenity.Scale(
		"Select a value",
		&gozenity.ScaleArgs{Initial: 30, Max: 100},
	)

	if err != nil {
		panic(err)
	}

	fmt.Println(val)
}
Output:

23

func TextInfo

func TextInfo(prompt string, args *TextInfoArgs) (text string, err error)

TextInfo shows a webview

Example
package main

import (
	"fmt"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	args := &gozenity.TextInfoArgs{
		Editable: true,
	}

	_, err := gozenity.TextInfo("Do something here:", args)

	if err != nil {
		fmt.Println(err)
	}

}
Output:

One of Filename, Text or URL need to be supplied
Example (Fifth)
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	tmpfile, err := ioutil.TempFile("", "filenametext")
	if err != nil {
		log.Fatal(err)
	}
	defer os.Remove(tmpfile.Name()) // clean up

	if _, err = tmpfile.WriteString("Hello, world!"); err != nil {
		log.Fatal(err)
	}
	if err := tmpfile.Close(); err != nil {
		log.Fatal(err)
	}

	args := &gozenity.TextInfoArgs{
		Editable: true,
		Filename: tmpfile.Name(),
	}

	string, err := gozenity.TextInfo("Do something here:", args)

	if err != nil {
		panic(err)
	}

	fmt.Println(string)
}
Output:

Hello, world!
Example (Fourth)
package main

import (
	"fmt"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	args := &gozenity.TextInfoArgs{
		Editable: true,
		URL:      `https://google.com`,
	}

	string, err := gozenity.TextInfo("Do something here:", args)

	if err != nil {
		panic(err)
	}

	fmt.Println(string)
}
Output:

Example (Second)
package main

import (
	"fmt"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	args := &gozenity.TextInfoArgs{
		Editable: true,
		Text: `Hello, worldly
worlded world.`,
	}

	string, err := gozenity.TextInfo("Do something here:", args)

	if err != nil {
		panic(err)
	}

	fmt.Println(string)

}
Output:

Hello, worldly
worlded world.
Example (Seventh)
package main

import (
	"fmt"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	args := &gozenity.TextInfoArgs{
		Editable: true,
		Text: `Hello, worldly
worlded world.`,
		URL: "https://goobar.com",
	}

	_, err := gozenity.TextInfo("Do something here:", args)

	if err != nil {
		fmt.Println(err)
	}

}
Output:

Only one of Filename, Text and URL can be supplied
Example (Sixth)
package main

import (
	"fmt"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	args := &gozenity.TextInfoArgs{
		Editable: true,
		Filename: "/tmp/foobar.txt",
	}

	_, err := gozenity.TextInfo("Do something here:", args)

	fmt.Println(err)

}
Output:

stat /tmp/foobar.txt: no such file or directory
Example (Third)
package main

import (
	"fmt"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	args := &gozenity.TextInfoArgs{
		Checkbox: "Agree?",
		Editable: true,
		Text: `Hello, worldly
worlded world.`,
	}

	string, err := gozenity.TextInfo("Do something here:", args)

	if err != nil {
		panic(err)
	}

	fmt.Println(string)

}
Output:

Hello, worldly
worlded world.

func UsernameAndPassword

func UsernameAndPassword(prompt string) (password, username string, err error)

UsernameAndPassword asks for a username and password

Example
package main

import (
	"fmt"

	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	password, username, err := gozenity.UsernameAndPassword("Enter password:")

	if err != nil {
		panic(err)
	}

	fmt.Printf("%s\n%s\n", username, password)
}
Output:

user
hunter2

func Warning

func Warning(prompt string) (err error)

Warning warns about warnings

Example
package main

import (
	"git.sr.ht/~jcmuller/gozenity"
)

func main() {
	err := gozenity.Warning("This thing happened. Bad.")

	if err != nil {
		panic(err)
	}

}
Output:

Types

type EmptySelectionError

type EmptySelectionError struct{}

EmptySelectionError is returned if there is no selection

func (*EmptySelectionError) Error

func (e *EmptySelectionError) Error() string

type Gozenity

type Gozenity struct {
	// contains filtered or unexported fields
}

Gozenity holds the structure of this thing

func New

func New(prompt string, arguments ...string) *Gozenity

New returns an instance of a Gozenity

type ScaleArgs

type ScaleArgs struct {
	Initial int
	Step    int
	Min     int
	Max     int
}

ScaleArgs are the options for Scale

type TextInfoArgs

type TextInfoArgs struct {
	Checkbox string
	Editable bool
	Filename string
	Text     string
	URL      string
}

TextInfoArgs represents options for text info

func (*TextInfoArgs) Parse

func (tia *TextInfoArgs) Parse() (args []string, err error)

Parse returns a slice of strings usable for a text info

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL