Skip to content

Clock position #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ usage : tty-clock [-iuvsScbtrahDBxn] [-C [0-7]] [-f format] [-d delay] [-a nsdel
-B Enable blinking colon
-d delay Set the delay between two redraws of the clock. Default 1s.
-a nsdelay Additional delay between two redraws in nanoseconds. Default 0ns.
-p [1-9] Position of clock in terminal: 1-9 starting top-left and ending bottom-right.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"starting top-left and ending bottom-right"... how do we get to "bottom left"? :)

i guess we want to say something like "starting clockwise from the top-left" or something like that.

should we make 0 be "center" and remove the other flag? ;)

73 changes: 67 additions & 6 deletions ttyclock.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ init(void)
}
clearok(ttyclock->datewin, True);

set_center(ttyclock->option.center);
if (ttyclock->option.center)
set_center(ttyclock->option.center);
else
if (ttyclock->option.position)
set_position(ttyclock->option.position);

nodelay(stdscr, True);

Expand Down Expand Up @@ -375,7 +379,10 @@ set_second(void)

clock_move(ttyclock->geo.x, (ttyclock->geo.y - y_adj), new_w, ttyclock->geo.h);

set_center(ttyclock->option.center);
if (ttyclock->option.center)
set_center(ttyclock->option.center);
else
if (ttyclock->option.position)

return;
}
Expand All @@ -396,6 +403,34 @@ set_center(Bool b)
return;
}

void
set_position(int p)
{
int LINE, COL;

ttyclock->option.rebound = False;

if (p >= 1 && p <= 3)
LINE = 0;
if (p >= 4 && p <= 6)
LINE = (LINES / 2) - (ttyclock->geo.h / 2);
if (p >= 7 && p <= 9)
LINE = LINES - ttyclock->geo.h - 2;

if (p == 1 || p == 4 || p == 7)
COL = 0;
if (p == 2 || p == 5 || p == 8)
COL = (COLS / 2) - (ttyclock->geo.w / 2);
if (p == 3 || p == 6 || p == 9)
COL = COLS - ttyclock->geo.w;

clock_move(LINE,
COL,
ttyclock->geo.w,
ttyclock->geo.h);

}

void
set_box(Bool b)
{
Expand Down Expand Up @@ -423,6 +458,9 @@ void
key_event(void)
{
int i, c;
int p = 0;
char nums[11] = {'"','!','@','#','$','%','^','&','*','(','\0'};
wchar_t gbp = L'£';

struct timespec length = { ttyclock->option.delay, ttyclock->option.nsdelay };

Expand All @@ -448,7 +486,10 @@ key_event(void)
}


switch(c = wgetch(stdscr))
c = wgetch(stdscr);
if (c == gbp)
c = '#';
switch(c)
{
case KEY_UP:
case 'k':
Expand Down Expand Up @@ -523,6 +564,21 @@ key_event(void)
set_box(!ttyclock->option.box);
break;

case '"':
case '!':
case '@':
case '#':
case '$':
case '%':
case '^':
case '&':
case '*':
case '(':
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do we use shift here? this is bound to fail depending on the keyboard mapping... why not just use the normal digits?

while ( p < 9 && nums[p] != c ) p++;
if (c == '"')
p = 2;
set_position(p);
break;
default:
nanosleep(&length, NULL);
for(i = 0; i < 8; ++i)
Expand Down Expand Up @@ -563,13 +619,13 @@ main(int argc, char **argv)

atexit(cleanup);

while ((c = getopt(argc, argv, "iuvsScbtrhBxnDC:f:d:T:a:")) != -1)
while ((c = getopt(argc, argv, "iuvsScbtrhBxnDC:f:d:T:a:p:")) != -1)
{
switch(c)
{
case 'h':
default:
printf("usage : tty-clock [-iuvsScbtrahDBxn] [-C [0-7]] [-f format] [-d delay] [-a nsdelay] [-T tty] \n"
printf("usage : tty-clock [-iuvsScbtrahDBxn] [-C [0-7]] [-f format] [-d delay] [-a nsdelay] [-T tty] [-p [1-9]] \n"
" -s Show seconds \n"
" -S Screensaver mode \n"
" -x Show box \n"
Expand All @@ -588,7 +644,8 @@ main(int argc, char **argv)
" -D Hide date \n"
" -B Enable blinking colon \n"
" -d delay Set the delay between two redraws of the clock. Default 1s. \n"
" -a nsdelay Additional delay between two redraws in nanoseconds. Default 0ns.\n");
" -a nsdelay Additional delay between two redraws in nanoseconds. Default 0ns.\n"
" -p [1-9] Position of clock in terminal: 1-9 starting top-left and ending bottom-right.\n");
exit(EXIT_SUCCESS);
break;
case 'i':
Expand Down Expand Up @@ -644,6 +701,10 @@ main(int argc, char **argv)
case 'x':
ttyclock->option.box = True;
break;
case 'p':
if(atol(optarg) > 0 && atol(optarg) < 10)
ttyclock->option.position = atol(optarg);
break;
case 'T': {
struct stat sbuf;
if (stat(optarg, &sbuf) == -1) {
Expand Down
3 changes: 3 additions & 0 deletions ttyclock.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <ncurses.h>
#include <unistd.h>
#include <getopt.h>
#include <wchar.h>

/* Macro */
#define NORMFRAMEW 35
Expand Down Expand Up @@ -84,6 +85,7 @@ typedef struct
long delay;
Bool blink;
long nsdelay;
int position;
} option;

/* Clock geometry */
Expand Down Expand Up @@ -125,6 +127,7 @@ void set_second(void);
void set_center(Bool b);
void set_box(Bool b);
void key_event(void);
void set_position(int p);

/* Global variable */
ttyclock_t *ttyclock;
Expand Down