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
&command
)
37 bool Interview::start()
41 const QString prompt
= "Shall I "+ m_command
+" this change?";
43 QTextStream
out(stdout
);
47 QTextStream
&patchOut
= Logger::standardOut();
48 patchOut
<< m_cursor
.currentText();
52 bool askAgain
= false;
54 int count
= m_cursor
.count();
55 QString allowed
= m_cursor
.allowedOptions();
56 QString question
= prompt
+" ("+ QString::number(currentIndex
) +"/"+
57 (count
== -1 ? "?" : QString::number(count
)) +") ["+ allowed
+ "], or ? for help: ";
59 QChar reply
= askSingleChar(question
);
60 if (reply
.unicode() != '?' && reply
.unicode() != 'h' && allowed
.indexOf(reply
) < 0)
61 reply
= 0x00DF; // to generate an 'invalid response' message.
63 switch (reply
.toAscii()) {
64 case 'j': currentIndex
= m_cursor
.forward(InterviewCursor::ItemScope
, false); break;
66 if (currentIndex
== 1) // at start.
69 currentIndex
= m_cursor
.back();
71 case 'y': m_cursor
.setResponse(true);
72 currentIndex
= m_cursor
.forward(); break;
73 case 'n': m_cursor
.setResponse(false);
74 currentIndex
= m_cursor
.forward(); break;
75 case 'f': m_cursor
.setResponse(true, InterviewCursor::BlockScope
);
76 currentIndex
= m_cursor
.forward(InterviewCursor::BlockScope
); break;
77 case 's': m_cursor
.setResponse(false, InterviewCursor::BlockScope
);
78 currentIndex
= m_cursor
.forward(InterviewCursor::BlockScope
); break;
79 case 'a': m_cursor
.setResponse(true, InterviewCursor::FullRange
);
81 case 'd': return true;
82 case 'q': return false;
83 case 'h': // fall through
85 out
<< m_cursor
.helpMessage();
90 m_cursor
.forceCount();
94 out
<< "Invalid response, try again please." << endl
;
99 if (! m_cursor
.isValid())
104 QChar
Interview::askSingleChar(const QString
&prompt
, const QString
&allowedCharacters
)
106 QTextStream
out(stdout
);
107 QTextStream
in(stdin
);
109 struct termios oldT
, newT
;
112 newT
.c_lflag
&= ~ECHO
; // echo off
113 newT
.c_lflag
&= ~ICANON
; //one char at a time
120 tcsetattr(0, TCSANOW
, &newT
);
125 if (answer
[0] == 93) // aka esc, used for things like 'arrow up'
126 in
.read(1); // after an esc there always is a second character.
127 } while(answer
.length() == 0 || !allowedCharacters
.isEmpty() && !allowedCharacters
.contains(answer
[0]));
129 tcsetattr(0, TCSANOW
, &oldT
);
130 out
<< answer
<< endl
;
135 QString
Interview::ask(const QString
&prompt
)
137 QTextStream
out(stdout
);
138 QTextStream
in(stdin
);
141 QString answer
= in
.readLine();