Bash scripta za alarm

Da li neko zna kako se izračunava vreme do alarma na osnovu trenutnog i željenog vremena alarma?
Primer sada je 15:32:33 a alarm treba da se aktivira u 20:00:00 :clock330:
Kako da izračunam, u bash-u ili javascript-u, za koliko bi trebao da se aktivira alarm?

Imam ovu ideju za yad (yet another dialog) UI koji koristi --html dialog html/css/JavaScript kao frontend a bash kao bekend.

Ne poznajem najbolje javascript pa sam pronašao jedan na ovoj stranici Cut & Paste JavaScript Alarm clock
JavaScript sam izmenio tako da kada se klikne na “set alarm” napravi jedan link (ali ne pokaže na stranici) i aktivira pomoću Dom2 event-a. Tačnije emuliranjem levog tastera miša na taj link. :slight_smile: To je neohodno da bi Yad ispisao postavljeno vreme na stdout-u.
Tu je i jedan script koji sprečava pojavu kontekst menija.

Ovo je frontend, kao jedan html fajl:
set.html

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

<!--CSS for the page-->
<style>
.body, .html {
  overflow-y: hidden;
  cursor: default;
  #background-color: #CECECE;
}

#jsalarmclock{
font-weight: bold;
font-size: 14px;
}

#jsalarmclock div{
margin-bottom: 0.8em;
}

#jsalarmclock div.leftcolumn{
float: left;
width: 150px;
font-size: 14px;
clear: left;
}

#jsalarmclock span{
margin-right: 10px;
}

</style>

<!--Script to prevent right click menu-->
<script type="text/javascript">
    if (document.addEventListener) { // IE >= 9; other browsers
        document.addEventListener('contextmenu', function(e) {
            //here you do nothing
            e.preventDefault();
        }, false);
    } else { // IE < 9
        document.attachEvent('oncontextmenu', function() {
            alert("You've tried to open context menu");
            window.event.returnValue = false;
        });
    }
</script>


<script type="text/javascript">

/***********************************************

* JavaScript Alarm Clock- by JavaScript Kit (www.javascriptkit.com)
* This notice must stay intact for usage
* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and 100s more

***********************************************/

var jsalarm={
	padfield:function(f){
		return (f<10)? "0"+f : f
	},
	showcurrenttime:function(){
		var dateobj=new Date()
		var ct=this.padfield(dateobj.getHours())+":"+this.padfield(dateobj.getMinutes())+":"+this.padfield(dateobj.getSeconds())
		this.ctref.innerHTML=ct
		this.ctref.setAttribute("title", ct)
	},
	init:function(){
		var dateobj=new Date()
		this.ctref=document.getElementById("jsalarm_ct")
		this.submitref=document.getElementById("submitbutton")
		this.submitref.onclick=function(){
			jsalarm.setalarm()
			this.value="Alarm Set"
			this.disabled=true
			return false
		}
		var selections=document.getElementsByTagName("select")
		this.hourselect=selections[0]
		this.minuteselect=selections[1]
		this.secondselect=selections[2]
		for (var i=0; i<60; i++){
			if (i<24) //If still within range of hours field: 0-23
			this.hourselect[i]=new Option(this.padfield(i), this.padfield(i), false, dateobj.getHours()==i)
			this.minuteselect[i]=new Option(this.padfield(i), this.padfield(i), false, dateobj.getMinutes()==i)
			this.secondselect[i]=new Option(this.padfield(i), this.padfield(i), false, dateobj.getSeconds()==i)

		}
		jsalarm.showcurrenttime()
		jsalarm.timer=setInterval(function(){jsalarm.showcurrenttime()}, 1000)
	},
	setalarm:function(){
		this.hourwake=this.hourselect.options[this.hourselect.selectedIndex].value
		this.minutewake=this.minuteselect.options[this.minuteselect.selectedIndex].value
		this.secondwake=this.secondselect.options[this.secondselect.selectedIndex].value
                var a = document.createElement('a');
                var linkText = document.createTextNode("Go");
                a.appendChild(linkText);
                a.title = "fake link";
                a.href = this.hourselect.options[this.hourselect.selectedIndex].value+":"+this.minuteselect.options[this.minuteselect.selectedIndex].value+":"+this.secondselect.options[this.secondselect.selectedIndex].value;
                // DOM 2 Events
                var dispatchMouseEvent = function(target, var_args) {
                  var e = document.createEvent("MouseEvents");
                  // If you need clientX, clientY, etc., you can call
                  // initMouseEvent instead of initEvent
                  e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1));
                  target.dispatchEvent(e);
                };
                dispatchMouseEvent(a, 'mouseover', true, true);
                dispatchMouseEvent(a, 'mousedown', true, true);
                dispatchMouseEvent(a, 'click', true, true);
                dispatchMouseEvent(a, 'mouseup', true, true);
		this.hourselect.disabled=true
		this.minuteselect.disabled=true
		this.secondselect.disabled=true
	}
}

</script>
</head>
<body class="body">

 <form action="" method="">
  <div id="jsalarmclock">
  <div><div class="leftcolumn">Current Time:</div> <span id="jsalarm_ct" style="letter-spacing: 2px"></span></div>
  <div><div class="leftcolumn">Set Alarm:</div> <span><select></select> Hour</span> <span><select></select> Minutes</span> <span><select></select> Seconds</span></div>
  <div>
  <input type="submit" value="Set Alarm!" id="submitbutton" />
  </div>
</form>


<script type="text/javascript">

jsalarm.init()

</script>
</body>
</html>

Bash skript je jednostavan yad --html dijalog.Skript proverava u while petlji da li je ulaz u formatu 00:00:00. (čas minuti sekunda)
Samo treba promeniti putanju do html fajla u ovoj promenljivoj YURI="/home/misko/Documents/set-alarm.html"

set-time.sh

#!/bin/bash

# Change this!
YURI="/home/misko/Documents/set-alarm.html"

# Main window
function _yad_ui(){
    # Set the line buffering with stdbuf
    stdbuf -oL -eL yad --title="Set Alarm" --width=550 --height=150 \
    --html --uri="${YURI}" --no-buttons --print-uri 2>&1 & echo $!
}

# Main Function
function _do_stuff(){
    # fetch yad process id
    read YAD_PID

    # read yad stdout line by line
    while read -r line
    do  
        if [[ "${line##*/}" == [0-9][0-9]:[0-9][0-9]:[0-9][0-9] ]]; then
            kill -SIGUSR1 $YAD_PID
            echo "Alarm set to: ${line##*/}"
            # Here we set alarm, somehow
        fi
    done
}

_yad_ui | _do_stuff & wait $! 2>/dev/null

exit 0

Ovako bi trebalo da radi. Imam Yad 0.42

3 Likes

Ево за ђиђискрипт

function timeDiff() {
     var alarmSetAt = new Date( "Jan 1, 2019 11:00:00" );
     var alarmToGoOff = new Date( "Jan 1, 2019 12:15:10" );

     var res = Math.abs(alarmSetAt - alarmToGoOff) / 1000;
     
     var daysDiff = Math.floor(res / 86400);
     var hoursDiff = Math.floor(res / 3600) % 24;        
     var minutesDiff = Math.floor(res / 60) % 60;
     var secondsDiff = res % 60;

     var timeleft = `${hoursDiff}:${minutesDiff}:${secondsDiff}`;
     console.log(timeleft);
}

PS
Шта користиш за прављење гифова?

Post Scriptum Rursus
На коју фору си тако лепо исписао код? Мени испаде ово једнобојно и јадно и гадно. Верујем да има везе са set.html, ал мени нешто неће.

1 Like

Hvala puno, probaću kasnije funkciju.

Za gifove koristim Peek. GitHub - phw/peek: Simple animated GIF screen recorder with an easy to use interface

I ova platforma podržava BBcode BBCode tags reference.

[code]
function some_function(){
     echo "BBcode"
}
[/code]
1 Like