We don't want vng executable to be placed inside Vng.app/Contents/MacOS on Mac OS X.
[vng.git] / Interview.cpp
blobbf76368a8c9f082e356b2031f956273847d9f2bb
1 /*
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"
21 #include "Logger.h"
23 #include <QDebug>
24 #include <QTextStream>
26 #ifndef Q_OS_WIN
27 #include <termios.h>
28 #endif
30 Interview::Interview(InterviewCursor &cursor, const QString &command)
31 : m_cursor(cursor),
32 m_command(command),
33 m_usePager(true)
37 bool Interview::start()
39 if (! m_usePager)
40 Logger::stopPager();
41 const QString prompt = "Shall I "+ m_command +" this change?";
42 int currentIndex = 1;
43 QTextStream out(stdout);
44 while(true) {
45 if (m_usePager)
46 Logger::startPager();
47 QTextStream &patchOut = Logger::standardOut();
48 patchOut << m_cursor.currentText();
49 patchOut.flush();
50 Logger::stopPager();
52 bool askAgain = false;
53 do {
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.
62 askAgain = false;
63 switch (reply.toAscii()) {
64 case 'j': currentIndex = m_cursor.forward(InterviewCursor::ItemScope, false); break;
65 case 'k':
66 if (currentIndex == 1) // at start.
67 askAgain = true;
68 else
69 currentIndex = m_cursor.back();
70 break;
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);
80 return true;
81 case 'd': return true;
82 case 'q': return false;
83 case 'h': // fall through
84 case '?':
85 out << m_cursor.helpMessage();
86 out.flush();
87 askAgain = true;
88 break;
89 case 'c':
90 m_cursor.forceCount();
91 askAgain = true;
92 break;
93 default:
94 out << "Invalid response, try again please." << endl;
95 askAgain = true;
96 break;
98 } while(askAgain);
99 if (! m_cursor.isValid())
100 return true;
104 QChar Interview::askSingleChar(const QString &prompt, const QString &allowedCharacters)
106 QTextStream out(stdout);
107 QTextStream in(stdin);
108 #ifndef Q_OS_WIN
109 struct termios oldT, newT;
110 tcgetattr(0, &oldT);
111 newT = oldT;
112 newT.c_lflag &= ~ECHO; // echo off
113 newT.c_lflag &= ~ICANON; //one char at a time
114 #endif
116 out << prompt;
117 out.flush();
119 #ifndef Q_OS_WIN
120 tcsetattr(0, TCSANOW, &newT);
121 #endif
122 QString answer;
123 do {
124 answer = in.read(1);
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]));
128 #ifndef Q_OS_WIN
129 tcsetattr(0, TCSANOW, &oldT);
130 out << answer << endl;
131 #endif
132 return answer[0];
135 QString Interview::ask(const QString &prompt)
137 QTextStream out(stdout);
138 QTextStream in(stdin);
139 out << prompt;
140 out.flush();
141 QString answer = in.readLine();
142 return answer;