Fix detecting of binary file status for new files
[vng.git] / src / Interview.cpp
blob66fca3ae5072b8093e505165cd7ab5f7ec470aba
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 &prompt)
31 : m_cursor(cursor),
32 m_prompt(prompt),
33 m_usePager(true)
37 bool Interview::start()
39 if (! m_usePager)
40 Logger::stopPager();
41 int currentIndex = 1;
42 if (m_cursor.count() == 0)
43 return true;
44 QTextStream out(stdout);
45 while(true) {
46 if (m_usePager)
47 Logger::startPager();
48 QTextStream &patchOut = Logger::standardOut();
49 patchOut << m_cursor.currentText();
50 patchOut.flush();
51 Logger::stopPager();
53 bool askAgain = false;
54 do {
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.
63 askAgain = false;
64 switch (reply.toAscii()) {
65 case 'j': currentIndex = m_cursor.forward(InterviewCursor::ItemScope, false); break;
66 case 'k':
67 if (currentIndex == 1) // at start.
68 askAgain = true;
69 else
70 currentIndex = m_cursor.back();
71 break;
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);
81 return true;
82 case 'd': return true;
83 case 'q': return false;
84 case 'h': // fall through
85 case '?':
86 out << m_cursor.helpMessage();
87 out.flush();
88 askAgain = true;
89 break;
90 case 'c':
91 m_cursor.forceCount();
92 askAgain = true;
93 break;
94 default:
95 out << "Invalid response, try again please." << endl;
96 askAgain = true;
97 break;
99 } while(askAgain);
100 if (! m_cursor.isValid())
101 return true;
105 QChar Interview::askSingleChar(const QString &prompt, const QString &allowedCharacters)
107 QTextStream out(stdout);
108 QTextStream in(stdin);
109 #ifndef Q_OS_WIN
110 struct termios oldT, newT;
111 tcgetattr(0, &oldT);
112 newT = oldT;
113 newT.c_lflag &= ~ECHO; // echo off
114 newT.c_lflag &= ~ICANON; //one char at a time
115 #endif
117 out << prompt;
118 out.flush();
120 #ifndef Q_OS_WIN
121 tcsetattr(0, TCSANOW, &newT);
122 #endif
123 QString answer;
124 do {
125 answer = in.read(1);
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])));
129 #ifndef Q_OS_WIN
130 tcsetattr(0, TCSANOW, &oldT);
131 out << answer << endl;
132 #endif
133 return answer[0];
136 QString Interview::ask(const QString &prompt)
138 QTextStream out(stdout);
139 QTextStream in(stdin);
140 out << prompt;
141 out.flush();
142 QString answer = in.readLine();
143 return answer;