Strip trailing blanks from record message
[vng.git] / Interview.cpp
blobc8845a3ceeccd4d8a82993a6beccc24945347ead
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 qDebug() << "c:" << m_cursor.count();
44 if (m_cursor.count() == 0)
45 return true;
46 QTextStream out(stdout);
47 while(true) {
48 if (m_usePager)
49 Logger::startPager();
50 QTextStream &patchOut = Logger::standardOut();
51 patchOut << m_cursor.currentText();
52 patchOut.flush();
53 Logger::stopPager();
55 bool askAgain = false;
56 do {
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.
65 askAgain = false;
66 switch (reply.toAscii()) {
67 case 'j': currentIndex = m_cursor.forward(InterviewCursor::ItemScope, false); break;
68 case 'k':
69 if (currentIndex == 1) // at start.
70 askAgain = true;
71 else
72 currentIndex = m_cursor.back();
73 break;
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);
83 return true;
84 case 'd': return true;
85 case 'q': return false;
86 case 'h': // fall through
87 case '?':
88 out << m_cursor.helpMessage();
89 out.flush();
90 askAgain = true;
91 break;
92 case 'c':
93 m_cursor.forceCount();
94 askAgain = true;
95 break;
96 default:
97 out << "Invalid response, try again please." << endl;
98 askAgain = true;
99 break;
101 } while(askAgain);
102 if (! m_cursor.isValid())
103 return true;
107 QChar Interview::askSingleChar(const QString &prompt, const QString &allowedCharacters)
109 QTextStream out(stdout);
110 QTextStream in(stdin);
111 #ifndef Q_OS_WIN
112 struct termios oldT, newT;
113 tcgetattr(0, &oldT);
114 newT = oldT;
115 newT.c_lflag &= ~ECHO; // echo off
116 newT.c_lflag &= ~ICANON; //one char at a time
117 #endif
119 out << prompt;
120 out.flush();
122 #ifndef Q_OS_WIN
123 tcsetattr(0, TCSANOW, &newT);
124 #endif
125 QString answer;
126 do {
127 answer = in.read(1);
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]));
131 #ifndef Q_OS_WIN
132 tcsetattr(0, TCSANOW, &oldT);
133 out << answer << endl;
134 #endif
135 return answer[0];
138 QString Interview::ask(const QString &prompt)
140 QTextStream out(stdout);
141 QTextStream in(stdin);
142 out << prompt;
143 out.flush();
144 QString answer = in.readLine();
145 return answer;