2 * This file is part of the vng project
3 * Copyright (C) 2008 Thomas Zander <tzander@trolltech.com>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "Interview.h"
20 #include "InterviewCursor.h"
24 #include <QTextStream>
30 Interview::Interview(InterviewCursor
&cursor
, const QString
&prompt
)
37 bool Interview::start()
42 if (m_cursor
.count() == 0)
44 QTextStream
out(stdout
);
48 QTextStream
&patchOut
= Logger::standardOut();
49 patchOut
<< m_cursor
.currentText();
53 bool askAgain
= false;
55 int count
= m_cursor
.count();
56 QString allowed
= m_cursor
.allowedOptions();
57 QString question
= m_prompt
+QLatin1String(" (")+ QString::number(currentIndex
) +QLatin1String("/")+
58 (count
== -1 ? QLatin1String("?") : QString::number(count
)) +QLatin1String(") [")+ allowed
+ QLatin1String("], or ? for help: ");
60 QChar reply
= askSingleChar(question
);
61 if (reply
.unicode() != '?' && reply
.unicode() != 'h' && allowed
.indexOf(reply
) < 0)
62 reply
= 0x00DF; // to generate an 'invalid response' message.
64 switch (reply
.toAscii()) {
65 case 'j': currentIndex
= m_cursor
.forward(InterviewCursor::ItemScope
, false); break;
67 if (currentIndex
== 1) // at start.
70 currentIndex
= m_cursor
.back();
72 case 'y': m_cursor
.setResponse(true);
73 currentIndex
= m_cursor
.forward(); break;
74 case 'n': m_cursor
.setResponse(false);
75 currentIndex
= m_cursor
.forward(); break;
76 case 'f': m_cursor
.setResponse(true, InterviewCursor::BlockScope
);
77 currentIndex
= m_cursor
.forward(InterviewCursor::BlockScope
); break;
78 case 's': m_cursor
.setResponse(false, InterviewCursor::BlockScope
);
79 currentIndex
= m_cursor
.forward(InterviewCursor::BlockScope
); break;
80 case 'a': m_cursor
.setResponse(true, InterviewCursor::FullRange
);
82 case 'd': return true;
83 case 'q': return false;
84 case 'h': // fall through
86 out
<< m_cursor
.helpMessage();
91 m_cursor
.forceCount();
95 out
<< "Invalid response, try again please." << endl
;
100 if (! m_cursor
.isValid())
105 QChar
Interview::askSingleChar(const QString
&prompt
, const QString
&allowedCharacters
)
107 QTextStream
out(stdout
);
108 QTextStream
in(stdin
);
110 struct termios oldT
, newT
;
113 newT
.c_lflag
&= ~ECHO
; // echo off
114 newT
.c_lflag
&= ~ICANON
; //one char at a time
121 tcsetattr(0, TCSANOW
, &newT
);
126 if (answer
[0] == 93) // aka esc, used for things like 'arrow up'
127 in
.read(1); // after an esc there always is a second character.
128 } while(answer
.length() == 0 || (!allowedCharacters
.isEmpty() && !allowedCharacters
.contains(answer
[0])));
130 tcsetattr(0, TCSANOW
, &oldT
);
131 out
<< answer
<< endl
;
136 QString
Interview::ask(const QString
&prompt
)
138 QTextStream
out(stdout
);
139 QTextStream
in(stdin
);
142 QString answer
= in
.readLine();