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 qDebug() << "c:" << m_cursor
.count();
44 if (m_cursor
.count() == 0)
46 QTextStream
out(stdout
);
50 QTextStream
&patchOut
= Logger::standardOut();
51 patchOut
<< m_cursor
.currentText();
55 bool askAgain
= false;
57 int count
= m_cursor
.count();
58 QString allowed
= m_cursor
.allowedOptions();
59 QString question
= prompt
+" ("+ QString::number(currentIndex
) +"/"+
60 (count
== -1 ? "?" : QString::number(count
)) +") ["+ allowed
+ "], or ? for help: ";
62 QChar reply
= askSingleChar(question
);
63 if (reply
.unicode() != '?' && reply
.unicode() != 'h' && allowed
.indexOf(reply
) < 0)
64 reply
= 0x00DF; // to generate an 'invalid response' message.
66 switch (reply
.toAscii()) {
67 case 'j': currentIndex
= m_cursor
.forward(InterviewCursor::ItemScope
, false); break;
69 if (currentIndex
== 1) // at start.
72 currentIndex
= m_cursor
.back();
74 case 'y': m_cursor
.setResponse(true);
75 currentIndex
= m_cursor
.forward(); break;
76 case 'n': m_cursor
.setResponse(false);
77 currentIndex
= m_cursor
.forward(); break;
78 case 'f': m_cursor
.setResponse(true, InterviewCursor::BlockScope
);
79 currentIndex
= m_cursor
.forward(InterviewCursor::BlockScope
); break;
80 case 's': m_cursor
.setResponse(false, InterviewCursor::BlockScope
);
81 currentIndex
= m_cursor
.forward(InterviewCursor::BlockScope
); break;
82 case 'a': m_cursor
.setResponse(true, InterviewCursor::FullRange
);
84 case 'd': return true;
85 case 'q': return false;
86 case 'h': // fall through
88 out
<< m_cursor
.helpMessage();
93 m_cursor
.forceCount();
97 out
<< "Invalid response, try again please." << endl
;
102 if (! m_cursor
.isValid())
107 QChar
Interview::askSingleChar(const QString
&prompt
, const QString
&allowedCharacters
)
109 QTextStream
out(stdout
);
110 QTextStream
in(stdin
);
112 struct termios oldT
, newT
;
115 newT
.c_lflag
&= ~ECHO
; // echo off
116 newT
.c_lflag
&= ~ICANON
; //one char at a time
123 tcsetattr(0, TCSANOW
, &newT
);
128 if (answer
[0] == 93) // aka esc, used for things like 'arrow up'
129 in
.read(1); // after an esc there always is a second character.
130 } while(answer
.length() == 0 || !allowedCharacters
.isEmpty() && !allowedCharacters
.contains(answer
[0]));
132 tcsetattr(0, TCSANOW
, &oldT
);
133 out
<< answer
<< endl
;
138 QString
Interview::ask(const QString
&prompt
)
140 QTextStream
out(stdout
);
141 QTextStream
in(stdin
);
144 QString answer
= in
.readLine();