~indicator-applet-developers/hud/phablet

« back to all changes in this revision

Viewing changes to tools/hud-cli.c

  • Committer: Tarmac
  • Author(s): Ted Gould
  • Date: 2013-03-25 16:54:06 UTC
  • mfrom: (351.2.4 no-more-cursing)
  • Revision ID: tarmac-20130325165406-qjbz521z2fnziioe
Remove curses from hud-cli.

Approved by Antti Kaijanmäki, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
#include <glib.h>
23
23
#include <gio/gunixinputstream.h>
24
24
#include <gtk/gtk.h>
25
 
#include <unistd.h>
26
 
#include <stdio.h>
27
 
#include <readline/readline.h>
28
 
#include <readline/history.h>
29
 
#include <stdlib.h>
30
 
#include <curses.h>
31
25
 
32
26
#include <hud-client.h>
33
27
 
34
28
 
35
29
static void print_suggestions(const char * query);
36
 
static void update(char *string);
37
 
void sighandler(int);
38
30
 
39
 
WINDOW *twindow = NULL;
40
 
int use_curses = 0;
41
31
static HudClientQuery * client_query = NULL;
42
32
 
43
33
int
47
37
        g_type_init ();
48
38
#endif
49
39
 
50
 
        int single_char;
51
 
        int pos = 0;
52
 
        
53
 
        char *line = (char*) malloc(256 * sizeof(char));
54
 
 
55
 
        signal(SIGINT, sighandler);
56
 
 
57
 
        // reading from pipe
58
 
        if (!isatty (STDIN_FILENO) ) {
59
 
                size_t a;
60
 
                int r;
61
 
                r = getline (&line, &a, stdin);
62
 
        
63
 
                if ( r == -1 ){
64
 
                        perror("Error reading from pipe");
65
 
                        exit(EXIT_FAILURE);     
66
 
                }
67
 
 
68
 
                // get rid of newline
69
 
                if( line[r-1] == '\n' )
70
 
                        line[r-1] = '\0';       
71
 
        
72
 
                printf("\nsearch token: %s\n", line);
73
 
                print_suggestions( line );
74
 
        }
75
 
        // read command line parameter - hud-cli "search string"
76
 
        else if( argc > 1 ){
77
 
                printf("\nsearch token: %s\n", argv[1]);
78
 
                print_suggestions( argv[1] );
79
 
        }
80
 
        // interactive mode
81
 
        else{
82
 
                use_curses = 1;
83
 
                
84
 
                twindow = initscr();            
85
 
                cbreak();
86
 
                keypad(stdscr, TRUE);   
87
 
                noecho();
88
 
                
89
 
                /* initialize the query screen */
90
 
                update( "" );
91
 
 
92
 
                /* interactive shell interface */
93
 
                while( 1 ){
94
 
                
95
 
                        single_char = getch();          
96
 
                        /* need to go left in the buffer */
97
 
                        if ( single_char == KEY_BACKSPACE ){
98
 
                                /* don't go too far left */
99
 
                                if( pos > 0 ){
100
 
                                        pos--;
101
 
                                        line[pos] = '\0';
102
 
                                        update( line );
103
 
                                }
104
 
                                else
105
 
                                        ; /* we are at the beginning of the buffer already */
106
 
                        }
107
 
                        /* ENTER will trigger the action for the first selected suggestion */
108
 
                        else if ( single_char == '\n' ){
109
 
 
110
 
                                /* FIXME: execute action on RETURN */
111
 
                                break;
112
 
                        }
113
 
                        /* add character to the buffer and terminate string */
114
 
                        else if ( single_char != KEY_RESIZE ) {
115
 
                                if ( pos < 256 -1 ){ // -1 for \0
116
 
                                        line[pos] = single_char;
117
 
                                        line[pos+1] = '\0';
118
 
                                        pos++;
119
 
                                        update( line );
120
 
                                }
121
 
                                else {
122
 
                                        
123
 
                                }
124
 
                        }
125
 
                        else{
126
 
                                // nothing to do
127
 
                        }
128
 
                }
129
 
                endwin();
130
 
        }
131
 
        
132
 
        free(line);
 
40
        const gchar * search = "";
 
41
 
 
42
        if (argc == 2) {
 
43
                search = argv[1];
 
44
        }
 
45
 
 
46
        printf("\nsearch token: %s\n", search);
 
47
        print_suggestions(search);
133
48
 
134
49
        g_clear_object(&client_query);
135
50
 
136
51
        return 0;
137
52
}
138
53
 
139
 
static void 
140
 
update( char *string ){
141
 
        
142
 
        werase(twindow);
143
 
        mvwprintw(twindow, 7, 10, "Search: %s", string);
144
 
 
145
 
        print_suggestions( string );
146
 
        
147
 
        // move cursor back to input line
148
 
        wmove( twindow, 7, 10 + 8 + strlen(string) );
149
 
 
150
 
        refresh();
151
 
}
152
 
 
153
54
static void
154
55
wait_for_sync_notify (GObject * object, GParamSpec * pspec, gpointer user_data)
155
56
{
161
62
static gboolean
162
63
wait_for_sync (DeeModel * model)
163
64
{
 
65
        g_return_val_if_fail(DEE_IS_SHARED_MODEL(model), FALSE);
 
66
 
164
67
        if (dee_shared_model_is_synchronized(DEE_SHARED_MODEL(model))) {
165
68
                return TRUE;
166
69
        }
192
95
        DeeModelIter * iter = NULL;
193
96
        int i = 0;
194
97
        for (iter = dee_model_get_first_iter(model); !dee_model_is_last(model, iter); iter = dee_model_next(model, iter), i++) {
195
 
                const gchar * suggestion = hud_client_query_results_get_command_name(client_query, iter);
196
 
                if( use_curses)
197
 
                        mvwprintw(twindow, 9 + i, 15, "%s", suggestion);
198
 
                else{
199
 
                        gchar * clean_line = NULL;
200
 
                        pango_parse_markup(suggestion, -1, 0, NULL, &clean_line, NULL, NULL);
201
 
                        printf("\t%s\n", clean_line);
202
 
                        free(clean_line);
203
 
                }
 
98
                const gchar * suggestion = dee_model_get_string(model, iter, 1);
204
99
 
 
100
                gchar * clean_line = NULL;
 
101
                pango_parse_markup(suggestion, -1, 0, NULL, &clean_line, NULL, NULL);
 
102
                printf("\t%s\n", clean_line);
 
103
                g_free(clean_line);
205
104
        }
206
105
 
207
106
        return;
208
107
}
209
 
 
210
 
void sighandler(int signal){
211
 
        endwin();
212
 
        g_clear_object(&client_query);
213
 
        exit(EXIT_SUCCESS);
214
 
}