Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gagliardetto/pausable
Pause your long-running apps from your keyboard
https://github.com/gagliardetto/pausable
Last synced: about 2 months ago
JSON representation
Pause your long-running apps from your keyboard
- Host: GitHub
- URL: https://github.com/gagliardetto/pausable
- Owner: gagliardetto
- Created: 2020-05-14T09:27:20.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-05-14T09:46:59.000Z (over 4 years ago)
- Last Synced: 2024-10-12T01:31:09.062Z (3 months ago)
- Language: Go
- Size: 1.95 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Install
```bash
go get -u github.com/gagliardetto/pausable
```## Why I wrote it
Sometimes I run programs that do a lot of tasks.
Sometimes also I need to pause the execution of those tasks because of
some reason (too much load, need to go to sleep, etc.).So I wrote `pausable` to be able to pause the execution of the the tasks.
I currently use `pausable` coupled with `github.com/gagliardetto/keyable`,
which let me to pause my programs (CLI stuff) from the keyboard.## Usage
```golang
package mainimport (
"fmt"
"os"
"time""github.com/eiannone/keyboard"
"github.com/gagliardetto/keyable"
"github.com/gagliardetto/pausable"
)func main() {
pauser := pausable.New()// Print some indications on usage:
fmt.Println("Press P to pause/play; Press S to see stats; press ESC or CTRL+C to quit; press CTRL+F to force-quit")// Create a new Keyable object and add callbacks
// for the various characters and key combinations:
kb := keyable.New()// onStat will be the callback for information for your program
// (progress, etc.)
onStat := func() {
fmt.Println("some stats here")
}
// onQuit is a callback for when you quit the program:
onQuit := func() {
fmt.Println("Exiting...")
// TODO: clean up, send exit signals, etc.
os.Exit(0)
}// Add the callbacks:
kb.
OnChar(
onStat,
'i',
).
OnChar(
func() {
paused := pauser.Toggle()
if paused {
fmt.Println("pause")
} else {
fmt.Println("play")
}
},
'p',
).
OnKey(
onQuit,
keyboard.KeyEsc, keyboard.KeyCtrlC,
).
OnKey(
func() {
fmt.Println("FORCE-QUITTING...")
os.Exit(1)
},
keyboard.KeyCtrlF,
)
// Start the keyboard listener:
err := kb.Start()
if err != nil {
panic(err)
}
// Don't forget to stop it!
defer kb.Stop()// This is some multi-task job you're running:
for {
// Wait for green-light (it will block if the pausable is paused, until it is un-paused):
pauser.Wait()// Execute your thing:
RunTask()
}
}func RunTask() error {
fmt.Println("Task running at", time.Now().Format("15:04:05"))
time.Sleep(time.Second)
return nil
}```