Skip to content
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

Clock position #57

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ 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:

123
456
789
53 changes: 48 additions & 5 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 @@ -563,13 +598,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 +623,11 @@ 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\n"
" 123\n"
" 456\n"
" 789\n");
exit(EXIT_SUCCESS);
break;
case 'i':
Expand Down Expand Up @@ -644,6 +683,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
2 changes: 2 additions & 0 deletions ttyclock.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ typedef struct
long delay;
Bool blink;
long nsdelay;
int position;
} option;

/* Clock geometry */
Expand Down Expand Up @@ -125,6 +126,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