Skip to content
kekse1Public

About

Best JavaScript `sleep` for the command line

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

236 Commits

Folders and files

Repository files navigation


asleep

My own, better sleep replacement (see man 1 sleep).


Pure Vanilla JavaScript - without any dependency (except the Node.js JavaScript interpreter).



Index






Example screenshots

🐭 The really (almost..) latest/current version, v3.2.2! Latest/current one!
🐭 This syntax is now possible (as of v3.1.1). And it got even more features.. New possibilities..
🐭 Here's the newest (now older) screenshot w/ only the `--help` view, v2.4.11. Only the --help view
🐭 And here's the screenshot of nearly the newest version, v2.4.6. Also visible this time: the progress (bar) itself (w/ enabled, new `--color`)! Screenshot of nearly the newest version
🐭 This is an example screenshot of v2.3.0, so a bit older release. The latest/current one had some more changes (see screenshot above). Oldest example screenshot
🐭 This last one is the *oldest* screenshot of all, v2.1.1. The oldest screenshot



Download / Source Code

ZERO dependencies.


Tip

The code really runs without any polyfill or stuff. This is contrarian to my common practice.. but.. dunno.



Bugs

If you find any, please contact me! THANK YOU!



Usage

See --help / -h / -?.


Basically, call this script with one or multiple parameter(s) with time(s) that'll get parsed (see Math.time.parse()). It just "waits" for the end.

Additionally you can also use --print / -p and/or --progress / -P.

If you abort the timeout via SIGINT (using <Ctrl>+<c>), it'll return with exit code (1). Exit code (2) if time sum is negative. And the other ones can be found in the source code..

Tip

The time parser also accepts negative parts for subtraction (beneath floating points), e.g. +4m-0.5m (equals 210000 milliseconds, so 3 minutes, 30 seconds).



Units

As you can see in the script itself (look for the Math.time.{parse,render}() functions), we're supporting here the following units and it's abbreviations (you can use after every value (e.g. 5m-0.5m)):

  • ms milliseconds
  • s seconds (the default unit, when no such suffix is set)
  • m minutes
  • h hours
  • d days
  • o months
  • y years

Tip

YES; you can use floating point numbers, e.g. 0.5m for 30 seconds.

Tip

AND: you can also use - and + for a bit of arithmetics, e.g. 1m-10s for 50 seconds..


Clock

This is the latest feature, inspired by the at command: clock times (for today or tomorrow time targets)!

I developed it in the backup file clock.test.js, and there's also this screenshot with the TEST CASES (so you can get a feeling for my syntax).

Only one possible example (the easiest one) of the new @ syntax is shown in my latest screenshot.



Timer

For best efficiency I'm using (at least one) setTimeout() (a big main loop would be worse); Since the maximum amount of time is limitted (see my (global) MAX_TIME, which is in JavaScript always (((2 ** 32) / 2) - 1)); so I also implemented some 'workaround' for it, so the theoretical limit is practically (much) higher.

And that's really better than consuming CPU time via loop or stuff! ;-)



Parameters

See the --help / -h / -? output (now with additional infos).

New in the --offset / -o parameter: now also w/ strings w/ % percent suffix, so it'll calculate the offset as a percentage of the real time to count.

New also: the Boolean parameter types can be easily enabled w/ just the getopt KEY (without any parameter) - but now they can also have these values (for disabling it, mostly):

  • yes/on/true
  • no/off/false


//TODO/...



Installation

I assume you've got root access to your Linux machine?

You can install this script it in several ways. I'd recommend to do it this way.

  • Copy the script iself to /usr/local/bin/asleep.
  • Add an alias like alias sleep="asleep -pPc".

For the alias you could, for instance, just set the alias command in a file like /etc/profile.d/asleep.sh. It should (actually) automatically be called on login.

The -pP parameters in the alias above are optional. But I like it that way! :-)


If you only got user permissions on your Linux box, you can also use this script, but you need to add the path to it to your local $PATH variable - wherever it is (same for the alias).



You only need to have an installed Node.js (the JavaScript interpreter, for the server-side). So maybe you're also interested in my make-nodejs.sh?



Exports and Extensions

The script extends some global modules:

  • Math
  • console
  • String
  • Date
  • global

At this moment I'm too lazy too search for any other export I'm maybe doing. So look for yourself if needed at all (TODO?).



My really tiny getopt interpretation

Because I really wanted to eliminate any dependency, I decided to use my own, very little getopt interpretation (especially made for this script). It's really limited, but supports everything we really need and wish, including multiple short parameters (like -pP), plus the necessary values for some less getopt-ions, and, of course, the mandatory -- parameter 'stop sign'.



You're welcome!

Any feature idea is welcome! If you have one, please contact me!




Contact


Copyright and License

The Copyright is (c) Sebastian Kucharczyk, and it's licensed under the MIT (also known as 'X' or 'X11' license).

Favicon

About

Best JavaScript `sleep` for the command line

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages