From: <ny...@us...> - 2007-01-03 13:38:48
|
Revision: 232 http://svn.sourceforge.net/pmplib/?rev=232&view=rev Author: nyaochi Date: 2007-01-03 05:38:48 -0800 (Wed, 03 Jan 2007) Log Message: ----------- Make console_posix.c work. More test needed. Modified Paths: -------------- trunk/pmplib/frontend/easypmp/cui/Makefile.am trunk/pmplib/frontend/easypmp/cui/console_posix.c trunk/pmplib/frontend/easypmp/cui/main.c Modified: trunk/pmplib/frontend/easypmp/cui/Makefile.am =================================================================== --- trunk/pmplib/frontend/easypmp/cui/Makefile.am 2007-01-03 10:49:31 UTC (rev 231) +++ trunk/pmplib/frontend/easypmp/cui/Makefile.am 2007-01-03 13:38:48 UTC (rev 232) @@ -20,6 +20,8 @@ $(getopt) \ util.c \ util.h \ + console_posix.c \ + console.h \ main.c easypmp_CPPFLAGS = -I$(srcdir)/../common Modified: trunk/pmplib/frontend/easypmp/cui/console_posix.c =================================================================== --- trunk/pmplib/frontend/easypmp/cui/console_posix.c 2007-01-03 10:49:31 UTC (rev 231) +++ trunk/pmplib/frontend/easypmp/cui/console_posix.c 2007-01-03 13:38:48 UTC (rev 232) @@ -1,7 +1,7 @@ /* * Miscellaneous utilities. * - * Copyright (c) 2005-2006 Nyaochi + * Copyright (c) 2005-2006 Martin Ellis * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -45,6 +45,8 @@ #include "console.h" +#define HAVE_TTY + #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) @@ -95,7 +97,7 @@ * * @param unused */ -void window_size_changed(int unused) +static void window_size_changed(int unused) { static struct winsize wsize; if (ioctl(1, TIOCGWINSZ, &wsize) != -1) { @@ -109,12 +111,22 @@ #endif/*CAN_GET_WIN_SIZE*/ +static int is_tty(FILE *fp) +{ + int fd = fileno(fp); + if (0 < fd && fd <= POSSIBLE_TTYS && fd_is_tty[fd]) { + return 1; + } else { + return 0; + } +} + /** * Determines whether stdin, stdout and stderr are associated with a * TTY, and update fd_is_tty accordingly. Also sets up * window_size_changed as a signal handler. */ -void display_init() +int console_init() { #ifdef HAVE_TTY int i; @@ -126,6 +138,7 @@ window_size_changed(0); #endif/*CAN_GET_WIN_SIZE*/ #endif/*HAVE_TTY*/ + return 0; } /** @@ -136,14 +149,19 @@ * This is followed by a carriage return character, to return the * cursor to the start of the line. */ -void clear_line(FILE *fp) +int console_clearln(FILE *fp) { - /* fmt needs 4 chars (%, -, s, \r) + - room for window_width as string (max. 65535) + - null terminator */ - char fmt[10]; - sprintf(fmt, "%%-%us\r", window_width); - fprintf(fp, fmt, ""); + if (is_tty(fp)) { + /* fmt needs 4 chars (%, -, s, \r) + + room for window_width as string (max. 65535) + + null terminator */ + char fmt[10]; + sprintf(fmt, "%%-%us\r", window_width); + fprintf(fp, fmt, ""); + return 0; + } else { + return 1; + } } /** @@ -161,28 +179,22 @@ * @param line the UCS-2 encoded string to display * @param min_width minimum number of characters to print */ -void display_line(FILE *fp, const ucs2char_t* const line, - unsigned int min_width) +int console_println(FILE *fp, const ucs2char_t* line, int min_width) { - unsigned int length = ucs2len(line); + int length = ucs2len(line); const ucs2char_t* line_to_print; - int writing_to_tty; - /* Check if writing to a terminal. If so, clear the current line. */ - int fd = fileno(fp); - if (0 < fd && fd <= POSSIBLE_TTYS && fd_is_tty[fd]) { - clear_line(fp); - writing_to_tty = 1; - } else { - writing_to_tty = 0; - } + /* Check if writing to a terminal. */ + int writing_to_tty = is_tty(fp); if(!writing_to_tty) { /* Write the whole line to the file, add \n. */ - fprints(fp, "%s\r\n", line); + fprints(fp, "%s\n", line); + return length; } else if (length <= window_width) { /* There's enough room to show the whole line. */ fprints(fp, "%s\r", line); + return length; } else { /* Length of the longest string we might display: */ @@ -194,6 +206,7 @@ ucs2char_t *truncated_line = ucs2calloc(sizeof(ucs2char_t) * (trunc_length+1)); ucs2ncpy(truncated_line, line, trunc_length); fprints(fp, "%s\r", truncated_line); + return trunc_length; } } Modified: trunk/pmplib/frontend/easypmp/cui/main.c =================================================================== --- trunk/pmplib/frontend/easypmp/cui/main.c 2007-01-03 10:49:31 UTC (rev 231) +++ trunk/pmplib/frontend/easypmp/cui/main.c 2007-01-03 13:38:48 UTC (rev 232) @@ -113,7 +113,7 @@ easypmp_progress_num_str(fpo, param_int+1, filepath_skippath(param_str)); break; case EASYPMPDBP_GMI|EASYPMPSP_END: - clear_line(fpo); + console_clearln(fpo); fprintf(fpo, " %d files were imported\n", param_int); fprintf(fpo, "\n"); break; @@ -329,11 +329,11 @@ fprintf(fpo, "\n"); memset(&musics, 0, sizeof(musics)); - display_init(); + console_init(); if ((opt.verb & MODE_DATABASE) || ((opt.verb & MODE_PLAYLIST) && (opt.verb & MODE_PLAYLIST_FIND))) { fprintf(fpo, "Enumerating music files\n"); easypmp_enumerate_music(&musics, pmp, &opt, easypmp_enumerate_progress, NULL); - clear_line(fpo); + console_clearln(fpo); fprintf(fpo, " %d music files were found\n", musics.num_elements); fprintf(fpo, "\n"); } @@ -342,7 +342,7 @@ if (opt.verb & MODE_PLAYLIST) { fprintf(fpe, "Enumerating playlist files\n"); easypmp_enumerate_playlist(&playlists, pmp, &opt, easypmp_enumerate_progress, NULL); - clear_line(fpo); + console_clearln(fpo); fprintf(fpo, " %d music files were found\n", playlists.num_elements); fprintf(fpo, "\n"); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |